0

我开始活动时收到错误消息。这是我的活动:

public class DebtList extends Activity {

    String debtNames1, debtNames2, debtNames3, debtNames4, debtNames5 = null; 
    Double debtAmount1, debtAmount2, debtAmount3, debtAmount4, debtAmount5;
    Double debtRate1, debtRate2, debtRate3, debtRate4, debtRate5;
    Integer debtTerm1, debtTerm2, debtTerm3, debtTerm4, debtTerm5;

    //Name Displays
    TextView debtName1 = (TextView) findViewById(R.id.dispName1); //logcat states this is the location of the error
    TextView debtName2 = (TextView) findViewById(R.id.dispName2);
    TextView debtName3 = (TextView) findViewById(R.id.dispName3);
    TextView debtName4 = (TextView) findViewById(R.id.dispName4);
    TextView debtName5 = (TextView) findViewById(R.id.dispName5);

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

这是 logcat 的副本:

10-20 05:59:15.014: D/AndroidRuntime(389): Shutting down VM
10-20 05:59:15.014: W/dalvikvm(389): threadid=1: thread exiting with uncaught exception (group=0x4001d800)
10-20 05:59:15.094: D/dalvikvm(389): GC_FOR_MALLOC freed 5706 objects / 305808 bytes in 68ms
10-20 05:59:15.094: E/AndroidRuntime(389): FATAL EXCEPTION: main
10-20 05:59:15.094: E/AndroidRuntime(389): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{biz.midl.debttracking/biz.midl.debttracking.DebtList}: java.lang.NullPointerException
10-20 05:59:15.094: E/AndroidRuntime(389):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2585)
10-20 05:59:15.094: E/AndroidRuntime(389):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
10-20 05:59:15.094: E/AndroidRuntime(389):  at android.app.ActivityThread.access$2300(ActivityThread.java:125)
10-20 05:59:15.094: E/AndroidRuntime(389):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
10-20 05:59:15.094: E/AndroidRuntime(389):  at android.os.Handler.dispatchMessage(Handler.java:99)
10-20 05:59:15.094: E/AndroidRuntime(389):  at android.os.Looper.loop(Looper.java:123)
10-20 05:59:15.094: E/AndroidRuntime(389):  at android.app.ActivityThread.main(ActivityThread.java:4627)
10-20 05:59:15.094: E/AndroidRuntime(389):  at java.lang.reflect.Method.invokeNative(Native Method)
10-20 05:59:15.094: E/AndroidRuntime(389):  at java.lang.reflect.Method.invoke(Method.java:521)
10-20 05:59:15.094: E/AndroidRuntime(389):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
10-20 05:59:15.094: E/AndroidRuntime(389):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
10-20 05:59:15.094: E/AndroidRuntime(389):  at dalvik.system.NativeStart.main(Native Method)
10-20 05:59:15.094: E/AndroidRuntime(389): Caused by: java.lang.NullPointerException
10-20 05:59:15.094: E/AndroidRuntime(389):  at android.app.Activity.findViewById(Activity.java:1637)
10-20 05:59:15.094: E/AndroidRuntime(389):  at biz.midl.debttracking.DebtList.<init>(DebtList.java:16)
10-20 05:59:15.094: E/AndroidRuntime(389):  at java.lang.Class.newInstanceImpl(Native Method)
10-20 05:59:15.094: E/AndroidRuntime(389):  at java.lang.Class.newInstance(Class.java:1429)
10-20 05:59:15.094: E/AndroidRuntime(389):  at android.app.Instrumentation.newActivity(Instrumentation.java:1021)
10-20 05:59:15.094: E/AndroidRuntime(389):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2577)

我尝试做 final TextView debtName1 = ..... 它提供了相同的错误消息。

请让我知道可以采取哪些措施来减轻错误。

4

1 回答 1

3

所有五个 TextView 分配都必须onCreate 函数内。

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

    //Name Displays
    TextView debtName1 = (TextView) findViewById(R.id.dispName1);
    TextView debtName2 = (TextView) findViewById(R.id.dispName2);
    TextView debtName3 = (TextView) findViewById(R.id.dispName3);
    TextView debtName4 = (TextView) findViewById(R.id.dispName4);
    TextView debtName5 = (TextView) findViewById(R.id.dispName5);

    // Some other stuff ..
    }

在创建对象(您的 DebtList 活动)时,会发生全局级别的变量分配。到那时,xml 尚未膨胀(发生在 setContentView(..) 中),因此该 xml 视图 (R.id.dispNameX) 的 id 不会屈服于真实对象。因此出现 NullPointerException。

于 2013-10-20T06:20:56.660 回答