简体   繁体   中英

Android: FATAL EXCEPTION: main

I am new to android development. I am creating a app which shows list of addresses in a list view. It has two activities. DashboardGridActivity is a Main activity(This activity shows icons in GridView). Second is DisplayActivity(This activity reads data from XML and displays in a list view)called in onItemClick function of DashboardGridActivity. Following is my DisplayActivity.java file:

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;
  }
}

onItemClick functionn from DashboardGridActivity.java file

@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);
}

When I am trying to run this app, it shows Icons in GridView, but when I click on icon it says "Unfortunately app has stopped". It also gives following 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

Please suggest me a way to resolve this problem.

Use this code for your 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);
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM