我搜索了stackoverflow,发现了很多类似的问题仍然不适用于我的。所以,我的MainActivity的布局在 res / layout / main.xml :
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TextView
android:id="@+id/week_summary"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
style="@style/PaddedTextView"
/>
<Button
style="?stMainScreenPlusButton"
android:onClick="openAddActivityScreen"/>
</LinearLayout>
MainActivity:
package com.example.theapp;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;
public class MainActivity extends Activity {
/**
* Called when the activity is first created.
*/
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
updateWeekSummary();
}
public void openAddActivityScreen(View view) {
Intent intent = new Intent(this, AddActivity.class);
startActivity(intent);
}
private void updateWeekSummary() {
TextView summaryView = (TextView) findViewById(R.id.week_summary);
int week_event_count = BasicStats.countAllForLastWeek();
String week_summary;
if (week_event_count > 0) {
week_summary = String.format(getString(R.string.week_summary_fmt), week_event_count);
} else {
week_summary = getString(R.string.week_summary_empty);
}
summaryView.setText(week_summary);
}
}
问题是在updateWeekSummary()方法中,summaryView变量始终为NULL。我发现了类似错误的一些常见来源: 1)不要在Activity的onCreate方法的第一行调用super.onCreate() 2)调用findViewById()后调用setContentView() 3)拼写错误的android:id属性。
这些似乎都不是这样,我现在完全迷失了。
UPD。根据要求,LogCat输出:
03-22 15:33:55.629 29404-29404 / com.example.theapp W / dalvikvm: threadid = 1:线程退出,未捕获异常(组= 0x41626e18) 03-22 15:33:55.669 29404-29404 / com.example.theapp E / AndroidRuntime: 致命异议:主要 过程:com.example.theapp,PID:29404 java.lang.RuntimeException:无法启动活动ComponentInfo {com.example.theapp / com.example.theapp.MainActivity}: 显示java.lang.NullPointerException 在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2596) 在android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2653) 在android.app.ActivityThread.access $ 800(ActivityThread.java:156) 在android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1355) 在android.os.Handler.dispatchMessage(Handler.java:102) 在android.os.Looper.loop(Looper.java:157) 在android.app.ActivityThread.main(ActivityThread.java:5872) at java.lang.reflect.Method.invokeNative(Native Method) 在java.lang.reflect.Method.invoke(Method.java:515) 在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:1069) 在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:885) at dalvik.system.NativeStart.main(Native Method) 引起:java.lang.NullPointerException at com.example.theapp.MainActivity.updateWeekSummary(MainActivity.java:36) at com.example.theapp.MainActivity.onCreate(MainActivity.java:18) 在android.app.Activity.performCreate(Activity.java:5312) 在android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1111) 在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2552) 在android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2653) 在android.app.ActivityThread.access $ 800(ActivityThread.java:156) 在android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1355) 在android.os.Handler.dispatchMessage(Handler.java:102) 在android.os.Looper.loop(Looper.java:157) 在android.app.ActivityThread.main(ActivityThread.java:5872) at java.lang.reflect.Method.invokeNative(Native Method) 在java.lang.reflect.Method.invoke(Method.java:515) 在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:1069) 在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:885) 在dalvik.system.NativeStart.main(本地方法)