【问题标题】:Moving from one ListView Activity to another ListView Activiy从一个 ListView Activity 移动到另一个 ListView Activity
【发布时间】:2011-04-15 11:42:05
【问题描述】:

我通过创建此类在 Android 中使用ListView

public class HBSListView extends ListActivity;

当我点击列表中的项目时,我想转到下一个ListActivity,显示上一个列表中点击项目的相关详细信息。

lv.setOnItemClickListener( new OnItemClickListener() {
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {              
        @SuppressWarnings("unchecked")
        HashMap<String, String> o = (HashMap<String, String>) lv.getItemAtPosition(position);                   

        Intent a = new Intent(iTeam.Ufinder.Application.HBS.HBSDetailView.class.getName()); 
        a.putExtra("store_id", o.get("id")); 
        startActivity(a); 

        // When I use above code it is not working. I want to pass ID also.

        // This works but i do not know how to pass ID this way.
        // startActivity(new Intent("iTeam.Ufinder.Application.HBSDETAILVIEW"));
    }
});

public class HBSDetailView extends ListActivity这是我要移动的班级。

主要文件

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
  package="iTeam.Ufinder.Application"
  android:versionCode="1"
  android:versionName="1.0">
<uses-sdk android:minSdkVersion="10" />

<application android:icon="@drawable/icon" android:label="@string/app_name">
    <activity android:name=".Startup"
              android:label="@string/app_name">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
     <activity android:name=".main"
              android:label="@string/app_name">
        <intent-filter>
            <action android:name="iTeam.Ufinder.Application.CLEARSCREEN" />
            <category android:name="android.intent.category.DEFAULT" />
        </intent-filter>
    </activity>
    <activity android:name="iTeam.Ufinder.Application.MANAGEMENT.Management"
              android:label="@string/app_name">
        <intent-filter>
            <action android:name="iTeam.Ufinder.Application.MANAGEMENT" />
            <category android:name="android.intent.category.DEFAULT" />
        </intent-filter>
    </activity>
    <activity android:name="iTeam.Ufinder.Application.HBS.HBSListView"
              android:label="@string/app_name">
        <intent-filter>
            <action android:name="iTeam.Ufinder.Application.HBSLISTVIEW" />
            <category android:name="android.intent.category.DEFAULT" />
        </intent-filter>
    </activity>
    <activity android:name="iTeam.Ufinder.Application.HBS.HBSDetailView"
              android:label="@string/app_name">
        <intent-filter>
            <action android:name="iTeam.Ufinder.Application.HBSDETAILVIEW" />
            <category android:name="android.intent.category.DEFAULT" />
        </intent-filter>
    </activity>

</application>
<uses-permission android:name="android.permission.INTERNET" />

这是Exception

04-15 13:04:43.901: 错误/AndroidRuntime(1417): java.lang.RuntimeException:无法实例化活动 组件信息{iTeam.Ufinder.Application/iTeam.Ufinder.Application.HBS.HBSDetailView}: java.lang.NullPointerException

【问题讨论】:

  • 当我通过编写此代码运行我的项目时,它会生成错误并强制关闭应用程序。 Intent a = new Intent(iTeam.Ufinder.Application.HBS.HBSDetailView.class.getName()); a.putExtra("store_id", o.get("id"));开始活动(一);但是当我使用这种方式去新的listActivity时,它可以工作,但在这种情况下,我无法在最后一个屏幕上获得点击列表项的ID!我需要一些方法来使用当前列表活动中选定列表项的 ID 转到下一个列表活动。
  • 查看logcat,stacktrace一定有异常,如果你强制关闭。
  • 好的,我遇到了这个异常,无法实例化活动。它是空点异常。

标签: android android-intent android-listview nullpointerexception


【解决方案1】:

当你像这样创建 Intent 时

Intent a = new Intent(iTeam.Ufinder.Application.HBS.HBSDetailView.class.getName()); 

您使用类名的操作名称创建它。您需要像这样创建 Intent:

Intent a = new Intent(HBSListView.this, iTeam.Ufinder.Application.HBS.HBSDetailView.class); 

区别在于调用签名。第一个是String 类型的一个参数。它代表使用指定操作创建Intent。第二个是关于 ContextClass 参数。用于创建Intent,在指定的上下文中调用指定的类。

还要检查o 不为空。

编辑

好吧,如果你想以这种方式开始活动......

Intent a = new Intent("iTeam.Ufinder.Application.HBSDETAILVIEW");
a.putExtra("store_id", o.get("id")); 
startActivity(a); 

上面的代码必须按照你的预期工作......

【讨论】:

  • 构造函数 Intent(new AdapterView.OnItemClickListener(){}, Class) 未定义。
  • 哦,我错了...不是this 第一个参数。使用NameOfYourCurrectActivity.this
  • 我是 android 开发的新手,我对 Intent() 没有深入的了解;如果我使用您建议的方式,它会生成 java 错误。
  • 仍然会抛出无法实例化活动的运行时异常。 Intent intent = new Intent(HBSListView.this, iTeam.Ufinder.Application.HBS.HBSDetailView.class);开始活动(意图);此时我只想开始我的新列表活动,如果我这样做,我可以开始 startActivity(new Intent("iTeam.Ufinder.Application.HBSDETAILVIEW"));但是这样我可以传递所选列表项的 ID。
  • 否是不工作,同样的例外。是扩展 Activity/ListActivity 的问题吗?因为我正在扩展 ListActivity。 public class HBSDetailView extends ListActivity { } 它应该可以工作 :( 我需要更多知识!
【解决方案2】:
Bundle bundle = new Bundle();
ListAdapter adapter = (new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,list1));
list.setAdapter(adapter);

list.setOnItemClickListener(new OnItemClickListener() {
    @Override
    public void onItemClick(AdapterView<?> arg0, View v,int position, long id) {
        HashMap<String, String> o = (HashMap<String, String>) lv.getItemAtPosition(position);

        if (position == 0) {
            bundle.putString("store_id", o.get("id"));
            Intent inten = new Intent("Activity name which is you must define in manifest file"); 
            inten.putExtras(bundle);

            startActivity(inten);
        }

        if(position==1) {
            //write your code for when second item of listview clicked
        }

        .....

    }
});

将您的Activity 添加到清单文件中:

<activity
    android:name=".className"
    android:label="@string/app_name" >
    <intent-filter>
        <action android:name="com.test.ACTIVITY" />
        //use this name when you want to run this activty

        <category android:name="android.intent.category.DEFAULT" />
    </intent-filter>
</activity>

【讨论】:

  • 我想这就是你想把它放到你的 listview 类中
猜你喜欢
  • 2015-09-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-01-07
  • 1970-01-01
  • 1970-01-01
  • 2017-12-28
  • 2016-01-18
相关资源
最近更新 更多