0

I know there are many other questions like this, but I read them all and can´t find an answer for my problem.

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>

How you can see, all my Activites are defined in my Manifest.xml. There are also no abstract classes. Can´t find the problem.

My MainActivity:

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

What else could be the problem?

edit:

xml of MainActivity:

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

XML of NeuesKind-Activity:

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

EDIT 2: It seems to be a problem with

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

because when I move this declaration, the error is in this line.

4

3 回答 3

4

尝试

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

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

于 2013-05-04T06:54:32.840 回答
2

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

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

由于您在初始化之前尝试使用 listview,因此您会收到空指针异常

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

      lv.setAdapter(adapter);
于 2013-05-04T07:03:54.907 回答
0

检查游标 c 是否返回 null。如果是这种情况,那么您的空指针将位于:

int count = c.getCount();
于 2013-05-04T06:52:25.873 回答