我尝试运行我的应用程序,但是在setAdapter(listAdapter)操作中遇到空指针异常。
主要活动Java代码
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Assigning main activity name for drawing
setContentView(R.layout.activity_time);
// Find the ListView resource and assign to ListView Object created
ExpandableListView mainListView = (ExpandableListView) findViewById(R.id.textClock);
// Initialize adapter with data by parsing JSON
listAdapter = new TwocolumnAdapter(this, parseJSONString(readFile()), headersList);
//Saving the reference to prepare new adapter when reload is needed
object = this;
// Set the ArrayAdapter as the ListView's adapter.
mainListView.setAdapter(listAdapter);
}
主要活动XML
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="fill_parent" android:layout_height="fill_parent" tools:context=".Time" android:background="@color/accent_material_dark" android:orientation="vertical">
<ExpandableListView android:layout_width="fill_parent" android:layout_height="wrap_content" android:id="@+id/textClock" android:layout_gravity="center_vertical" android:format24Hour="@android:string/yes" />
我搜索了几个SO帖子,交叉检查了ID等,尝试了所有解决方案,但无法解决这个问题。
任何建议或指示都受到高度赞赏。
错误记录
12-18 00:36:59.609 19893-19893/sudharshanapps.clock E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: sudharshanapps.clock, PID: 19893
java.lang.RuntimeException: Unable to start activity ComponentInfo{sudharshanapps.clock/sudharshanapps.clock.Time}: java.lang.NullPointerException
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2198)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2257)
at android.app.ActivityThread.access$800(ActivityThread.java:139)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1210)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5086)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.NullPointerException
at sudharshanapps.clock.Time.onCreate(Time.java:264)
at android.app.Activity.performCreate(Activity.java:5248)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1110)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2162)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2257)
at android.app.ActivityThread.access$800(ActivityThread.java:139)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1210)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5086)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
at dalvik.system.NativeStart.main(Native Method)
答案 0 :(得分:1)
看起来问题是Layout-v17文件夹中的activity_time.xml。这是将用于API 17及更高版本的布局。看起来在这种情况下它实际上是加载这个xml而不是你期望的那个。尝试删除它或使其与active_time.xml的主要版本相同
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical">
<ListView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="@+id/mainListView" >
</ListView>
</LinearLayout>