在使用列表視圖遭遇一個NullPointerException。 我已經初始化我的適配器,我的listview和我的課程數組以及。的NullPointerException在setadapter()mehtod
道歉,如果我的疑問,聽起來可能有點模糊。我是一個新手,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
的[什麼是空指針異常,以及如何解決?(http://stackoverflow.com/questions/218384/what-is-a-null-pointer-exception-and-how可能重複-do-i-fix-it) –
你的代碼在我的最後工作正常。你能否請你重新檢查一下,讓我知道它是否仍然在你的末端崩潰? – sUndeep
@Jonathon萊因哈特,這是一個非常普遍的問題,這是特定的。不是重複的,儘管修復部分可能與reverse_web要求的答案有一些重疊。 – rekaszeru