setadapter()方法中的NullpointerException

时间:2014-05-09 05:55:58

标签: android listview nullpointerexception

使用listview时遇到NullPointerException。 我已经初始化了我的适配器,我的listview和我的课程数组。

如果我的怀疑听起来含糊不清,请道歉。我是android的新手,无法找出错误

AboutActivity.java

public class AboutActivity extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

     setContentView(R.layout.activity_about);

     String[] lessons = new String[] { "Android Introduction","Android Setup/Installation","Android Hello World","Android Layouts/Viewgroups","Android Activity & Lifecycle","Intents in Android"};

     ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1,lessons);
     ListView listview = (ListView)findViewById(R.id.ListView);
     listview.setAdapter(adapter);
}

fragment_about.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"
    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="com.example.chatclient.AboutActivity$PlaceholderFragment" >

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

    </ListView>

</RelativeLayout>

logcat的:

05-09 01:41:59.029: E/AndroidRuntime(1579): FATAL EXCEPTION: main
05-09 01:41:59.029: E/AndroidRuntime(1579): Process: com.example.chatclient, PID: 1579
05-09 01:41:59.029: E/AndroidRuntime(1579): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.chatclient/com.example.chatclient.AboutActivity}: java.lang.NullPointerException
05-09 01:41:59.029: E/AndroidRuntime(1579):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2195)
05-09 01:41:59.029: E/AndroidRuntime(1579):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
05-09 01:41:59.029: E/AndroidRuntime(1579):     at android.app.ActivityThread.access$800(ActivityThread.java:135)
05-09 01:41:59.029: E/AndroidRuntime(1579):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
05-09 01:41:59.029: E/AndroidRuntime(1579):     at android.os.Handler.dispatchMessage(Handler.java:102)
05-09 01:41:59.029: E/AndroidRuntime(1579):     at android.os.Looper.loop(Looper.java:136)
05-09 01:41:59.029: E/AndroidRuntime(1579):     at android.app.ActivityThread.main(ActivityThread.java:5017)
05-09 01:41:59.029: E/AndroidRuntime(1579):     at java.lang.reflect.Method.invokeNative(Native Method)
05-09 01:41:59.029: E/AndroidRuntime(1579):     at java.lang.reflect.Method.invoke(Method.java:515)
05-09 01:41:59.029: E/AndroidRuntime(1579):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
05-09 01:41:59.029: E/AndroidRuntime(1579):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
05-09 01:41:59.029: E/AndroidRuntime(1579):     at dalvik.system.NativeStart.main(Native Method)
05-09 01:41:59.029: E/AndroidRuntime(1579): Caused by: java.lang.NullPointerException
05-09 01:41:59.029: E/AndroidRuntime(1579):     at com.example.chatclient.AboutActivity.onCreate(AboutActivity.java:29)
05-09 01:41:59.029: E/AndroidRuntime(1579):     at android.app.Activity.performCreate(Activity.java:5231)
05-09 01:41:59.029: E/AndroidRuntime(1579):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
05-09 01:41:59.029: E/AndroidRuntime(1579):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2159)
05-09 01:41:59.029: E/AndroidRuntime(1579):     ... 11 more

4 个答案:

答案 0 :(得分:3)

你应该改变这个

setContentView(R.layout.activity_about);

setContentView(R.layout.fragment_about);

这是因为ListView文件中的fragment_about.xml,您会在ListView文件中找到activity_about.xml

ListView listview = (ListView)findViewById(R.id.ListView); //Cause NULL Pointer Exception

答案 1 :(得分:1)

错误:

Activity,您正在致电:

setContentView(R.layout.activity_about);  

这会显示布局activity_about.xml,而您在fragment_about.xml内创建了自己的观看次数。它会导致 NullPointerException ,因为系统无法找到视图:

findViewById(R.id.ListView); // this 'id' (R.id.ListView) cannot be find

解决方案:

activity_about.xml内创建视图或更改目标布局,如下所示:

setContentView(R.layout.fragment_about);  

答案 2 :(得分:0)

你得到空指针,因为你正在使用占位符,即用于UI的片段和在活动中编写java代码。以下是解决方案。

1.setContentView(R.layout.fragment_about);
2. change your activity like below: 

public class AboutActivity extends ActionBarActivity {

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

    if (savedInstanceState == null) {
        getSupportFragmentManager().beginTransaction()
                .add(R.id.container, new PlaceholderFragment()).commit();
    }
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {

    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();
    if (id == R.id.action_settings) {
        return true;
    }
    return super.onOptionsItemSelected(item);
}

/**
 * A placeholder fragment containing a simple view.
 */
public static class PlaceholderFragment extends Fragment {
    ArrayAdapter<String> adapter;
    public PlaceholderFragment() {
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.fragment_main, container,
                false);

         String[] lessons = new String[] { "Android Introduction","Android Setup/Installation","Android Hello World","Android Layouts/Viewgroups","Android Activity & Lifecycle","Intents in Android"};

         adapter = new ArrayAdapter<String>(getActivity(), android.R.layout.simple_list_item_1,lessons);
         ListView listview = (ListView)rootView.findViewById(R.id.ListView);
         listview.setAdapter(adapter);
        return rootView;
    }
}

}

答案 3 :(得分:0)

尝试替换此行:

setContentView(R.layout.activity_about);

使用:

setContentView(R.layout.fragment_about);

我希望这有帮助!