无法实例化活动Componentinfo

时间:2013-05-04 06:44:00

标签: android nullpointerexception android-manifest

我知道还有很多其他类似的问题,但是我读了所有这些问题并且找不到问题的答案。

05-04 08:38:40.742: E/AndroidRuntime(18186): FATAL EXCEPTION: main
05-04 08:38:40.742: E/AndroidRuntime(18186): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.example.unserekinder/com.example.unserekinder.MainActivity}: java.lang.NullPointerException
05-04 08:38:40.742: E/AndroidRuntime(18186):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2106)
05-04 08:38:40.742: E/AndroidRuntime(18186):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
05-04 08:38:40.742: E/AndroidRuntime(18186):    at android.app.ActivityThread.access$600(ActivityThread.java:141)
05-04 08:38:40.742: E/AndroidRuntime(18186):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
05-04 08:38:40.742: E/AndroidRuntime(18186):    at android.os.Handler.dispatchMessage(Handler.java:99)
05-04 08:38:40.742: E/AndroidRuntime(18186):    at android.os.Looper.loop(Looper.java:137)
05-04 08:38:40.742: E/AndroidRuntime(18186):    at android.app.ActivityThread.main(ActivityThread.java:5041)
05-04 08:38:40.742: E/AndroidRuntime(18186):    at java.lang.reflect.Method.invokeNative(Native Method)
05-04 08:38:40.742: E/AndroidRuntime(18186):    at java.lang.reflect.Method.invoke(Method.java:511)
05-04 08:38:40.742: E/AndroidRuntime(18186):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
05-04 08:38:40.742: E/AndroidRuntime(18186):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
05-04 08:38:40.742: E/AndroidRuntime(18186):    at dalvik.system.NativeStart.main(Native Method)
05-04 08:38:40.742: E/AndroidRuntime(18186): Caused by: java.lang.NullPointerException
05-04 08:38:40.742: E/AndroidRuntime(18186):    at android.app.Activity.findViewById(Activity.java:1839)
05-04 08:38:40.742: E/AndroidRuntime(18186):    at com.example.unserekinder.MainActivity.<init>(MainActivity.java:34)
05-04 08:38:40.742: E/AndroidRuntime(18186):    at java.lang.Class.newInstanceImpl(Native Method)
05-04 08:38:40.742: E/AndroidRuntime(18186):    at java.lang.Class.newInstance(Class.java:1319)
05-04 08:38:40.742: E/AndroidRuntime(18186):    at android.app.Instrumentation.newActivity(Instrumentation.java:1054)
05-04 08:38:40.742: E/AndroidRuntime(18186):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2097)

的Manifest.xml:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.unserekinder"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="17" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="com.example.unserekinder.MainActivity"
            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="com.example.unserekinder.NeuesKind"
            android:label="@string/title_activity_neues_kind" >
        </activity>
    </application>

</manifest>

您如何看待,我的所有活动都在Manifest.xml中定义。也没有抽象类。找不到问题。

我的主要活动:

package com.example.unserekinder;

...

public class MainActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        //System.out.println("%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%");
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        listview_fuellen();

        Button b = (Button) findViewById(R.id.btNeuesKind);
        b.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                Intent i = new Intent(MainActivity.this, NeuesKind.class);
                startActivity(i);

            }
        });
    }
    DBHelper db = new DBHelper(this);
    ListView lv = (ListView) findViewById(R.id.lvKinder);
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.activity_main, menu);
        return true;
    }

    public void listview_fuellen(){
        Cursor c = db.select();
        int count = c.getCount();
        String values[] = new String[count+1];
        int i = 0;

        while(c.moveToNext())
        { 
            values[i]= c.getString(c.getColumnIndex("name"));
            i++;
        }


        ArrayAdapter<String> adapter = new ArrayAdapter<String>(getBaseContext(),
                  android.R.layout.activity_list_item, android.R.id.text1,values);

        lv.setAdapter(adapter);
    }
}

还有什么问题?

修改

主要活动的xml:

<RelativeLayout 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"
    tools:context=".MainActivity" >

    <Button
        android:id="@+id/btNeuesKind"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_alignParentRight="true"
        android:layout_marginBottom="19dp"
        android:layout_marginRight="36dp"
        android:text="Neues Kind" />

     <ListView
        android:id="@+id/lvKinder"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true" >
    </ListView>


</RelativeLayout>

NeuesKind-Activity的XML:

<RelativeLayout 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"
    tools:context=".NeuesKind" >

    <EditText
        android:id="@+id/etName"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentRight="true"
        android:layout_alignParentTop="true"
        android:layout_marginTop="44dp"
        android:ems="10" >

        <requestFocus />
    </EditText>

    <Button
        android:id="@+id/btSaveKind"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentRight="true"
        android:layout_marginBottom="29dp"
        android:text="Kind anlegen" />

    <RadioButton
        android:id="@+id/rbweiblich"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="20dp"
        android:text="Weiblich" />

    <RadioButton
        android:id="@+id/rbmaennlich"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_centerVertical="true"
        android:text="Männlich" />

    <EditText
        android:id="@+id/etDatum"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/rbweiblich"
        android:layout_alignParentRight="true"
        android:layout_below="@+id/etName"
        android:layout_marginTop="28dp"
        android:ems="10"
        android:inputType="date" />

</RelativeLayout>

编辑2:

似乎是一个问题
ListView lv = (ListView) findViewById(R.id.lvKinder);

因为当我移动此声明时,错误就在这一行。

3 个答案:

答案 0 :(得分:4)

尝试

 DBHelper db = new DBHelper(this); 
 ListView lv = (ListView) findViewById(R.id.lvKinder);   
 listview_fuellen();  

您必须首先实例化列表视图和数据库,因为您在listview_fuellen()中使用它。

答案 1 :(得分:2)

你需要在调用listview_fuellen()之前初始化listview;

ListView lv = (ListView) findViewById(R.id.lvKinder);   
listview_fuellen();  

由于您在初始化之前尝试使用listview,因此您将获得空指针异常

您正尝试在初始化之前将适配器设置为listview

      lv.setAdapter(adapter);

答案 2 :(得分:0)

检查游标c是否返回null。如果是这种情况,那么你将使用nullpointer:

int count = c.getCount();