我是Android开发的新手。我正在创建一个应用程序,显示列表视图中的地址列表。它有两个活动。 DashboardGridActivity是一个Main活动(此活动在GridView中显示图标)。第二个是在DashboardGridActivity的onItemClick函数中调用的DisplayActivity(此活动从XML读取数据并在列表视图中显示)。以下是我的DisplayActivity.java文件:
public class DisplayActivity extends Activity {
ListView listView;
private String tag_name;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_display);
listView = (ListView) findViewById(R.id.list);
Intent intent = getIntent();
if(intent!= null)
{
//int imageId = intent.getIntExtra("DashboardImage",R.drawable.apartments);
tag_name = intent.getStringExtra("DashItemName");
}
List<NameAddress> nameAddressList = null;
try {
XMLPullParserHandler parser = new XMLPullParserHandler(tag_name);
nameAddressList = parser.parse(getAssets().open("data.xml"));
ArrayAdapter<NameAddress> adapter =
new ArrayAdapter<NameAddress>(this,R.layout.list_item, nameAddressList);
listView.setAdapter(adapter);
} catch (IOException e) {
e.printStackTrace();
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.display, menu);
return true;
}
}
来自DashboardGridActivity.java文件的onItemClick functionn
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
Intent intent = new Intent(this, DisplayActivity.class);
ViewHolder holder= (ViewHolder) view.getTag();
DashboardItems temp = (DashboardItems) holder.myItem.getTag();
//intent.putExtra("DashboardImage",temp.imageID);
intent.putExtra("DashItemName",temp.itemName);
startActivity(intent);
}
当我尝试运行此应用程序时,它会在GridView中显示图标,但是当我点击图标时它会显示&#34;不幸的是应用程序停止了#34;。它还提供了以下LogCat
logcat的:
04-11 13:34:20.695: I/Choreographer(1155): Skipped 183 frames! The application may be doing too much work on its main thread.
04-11 13:34:23.085: W/InputEventReceiver(1155): Attempted to finish an input event but the input event receiver has already been disposed.
04-11 13:34:29.995: D/AndroidRuntime(1155): Shutting down VM
04-11 13:34:29.995: W/dalvikvm(1155): threadid=1: thread exiting with uncaught exception (group=0xb2a2dba8)
04-11 13:34:30.075: E/AndroidRuntime(1155): FATAL EXCEPTION: main
04-11 13:34:30.075: E/AndroidRuntime(1155): Process: com.asm.offcampusresources, PID: 1155
04-11 13:34:30.075: E/AndroidRuntime(1155): java.lang.NullPointerException
04-11 13:34:30.075: E/AndroidRuntime(1155): at com.asm.offcampusresources.DashboardGridActivity.onItemClick(DashboardGridActivity.java:55)
04-11 13:34:30.075: E/AndroidRuntime(1155): at android.widget.AdapterView.performItemClick(AdapterView.java:299)
04-11 13:34:30.075: E/AndroidRuntime(1155): at android.widget.AbsListView.performItemClick(AbsListView.java:1113)
04-11 13:34:30.075: E/AndroidRuntime(1155): at android.widget.AbsListView$PerformClick.run(AbsListView.java:2904)
04-11 13:34:30.075: E/AndroidRuntime(1155): at android.widget.AbsListView$3.run(AbsListView.java:3638)
04-11 13:34:30.075: E/AndroidRuntime(1155): at android.os.Handler.handleCallback(Handler.java:733)
04-11 13:34:30.075: E/AndroidRuntime(1155): at android.os.Handler.dispatchMessage(Handler.java:95)
04-11 13:34:30.075: E/AndroidRuntime(1155): at android.os.Looper.loop(Looper.java:136)
04-11 13:34:30.075: E/AndroidRuntime(1155): at android.app.ActivityThread.main(ActivityThread.java:5017)
04-11 13:34:30.075: E/AndroidRuntime(1155): at java.lang.reflect.Method.invokeNative(Native Method)
04-11 13:34:30.075: E/AndroidRuntime(1155): at java.lang.reflect.Method.invoke(Method.java:515)
04-11 13:34:30.075: E/AndroidRuntime(1155): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
04-11 13:34:30.075: E/AndroidRuntime(1155): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
04-11 13:34:30.075: E/AndroidRuntime(1155): at dalvik.system.NativeStart.main(Native Method)
04-11 13:34:33.635: I/Process(1155): Sending signal. PID: 1155 SIG: 9
请建议我解决此问题的方法。
答案 0 :(得分:0)
将此代码用于onItemClickListener
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
Intent intent = new Intent(this, DisplayActivity.class);
DashboardItems temp = (DashboardItems) adapterView.getItemAtPosition(i);
//intent.putExtra("DashboardImage",temp.imageID);
intent.putExtra("DashItemName",temp.itemName);
startActivity(intent);
}