简体   繁体   中英

<Android>Unable to start activity ComponentInfo

I have a problem in my Application. I'll be grateful if You can help me to do something with it because I'm a beginer in Android programming and I have no idea what's wrong with this. This is my log:

01-20 19:02:42.452: E/AndroidRuntime(8834): FATAL EXCEPTION: main
01-20 19:02:42.452: E/AndroidRuntime(8834):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2070)
01-20 19:02:42.452: E/AndroidRuntime(8834):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2095)
01-20 19:02:42.452: E/AndroidRuntime(8834):     at android.app.ActivityThread.access$600(ActivityThread.java:135)
01-20 19:02:42.452: E/AndroidRuntime(8834):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1201)
01-20 19:02:42.452: E/AndroidRuntime(8834):     at android.os.Handler.dispatchMessage(Handler.java:99)
01-20 19:02:42.452: E/AndroidRuntime(8834):     at android.os.Looper.loop(Looper.java:137)
01-20 19:02:42.452: E/AndroidRuntime(8834):     at android.app.ActivityThread.main(ActivityThread.java:4849)
01-20 19:02:42.452: E/AndroidRuntime(8834):     at java.lang.reflect.Method.invokeNative(Native Method)
01-20 19:02:42.452: E/AndroidRuntime(8834):     at java.lang.reflect.Method.invoke(Method.java:511)
01-20 19:02:42.452: E/AndroidRuntime(8834):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:795)
01-20 19:02:42.452: E/AndroidRuntime(8834):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:562)
01-20 19:02:42.452: E/AndroidRuntime(8834):     at dalvik.system.NativeStart.main(Native Method)
01-20 19:02:42.452: E/AndroidRuntime(8834): Caused by: java.lang.NullPointerException
01-20 19:02:42.452: E/AndroidRuntime(8834):     at com.example.locateme.SavedActivity.fillListViewData(SavedActivity.java:34)
01-20 19:02:42.452: E/AndroidRuntime(8834):     at com.example.locateme.SavedActivity.initListView(SavedActivity.java:26)
01-20 19:02:42.452: E/AndroidRuntime(8834):     at com.example.locateme.SavedActivity.onCreate(SavedActivity.java:22)
01-20 19:02:42.452: E/AndroidRuntime(8834):     at android.app.Activity.performCreate(Activity.java:5244)
01-20 19:02:42.452: E/AndroidRuntime(8834):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1082)
01-20 19:02:42.452: E/AndroidRuntime(8834):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2034)
01-20 19:02:42.452: E/AndroidRuntime(8834):     ... 11 more

And this is my functions from errors from activity file:

    @Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_saved);
    initListView();
}

private void initListView() {
    fillListViewData();
}

private void fillListViewData() {
    locationDbAdapter = new LocationDbAdapter(getApplicationContext());
    locationDbAdapter.open();
    getAllLocations();
    listAdapter = new LocationList(this, locs);
    locationlist.setAdapter(listAdapter);
}

And my layout:
saved_activity.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@android:color/black"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".SavedActivity" >

<ListView
    android:id="@+id/locationlist"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_weight="1" >
</ListView>

Read WHOLE stack trace next time, not the first line. And you got there:

01-20 19:02:42.452: E/AndroidRuntime(8834): Caused by: java.lang.NullPointerException
01-20 19:02:42.452: E/AndroidRuntime(8834):     at com.example.locateme.SavedActivity.fillListViewData(SavedActivity.java:34)

So look at your SavedActvity.java, line 34 and fix use of uninitialized variable.

locationlist is not initialized

locationlist= (ListView)findViewById(R.id.locationlist);

Make sure your activity_saved.xml has a listview with id locationlist .

不应该是setContentView(R.layout.saved_activity);

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