使用startActivity调用第二个活动导致崩溃

时间:2015-03-24 22:05:04

标签: android android-activity

这似乎是一个常见问题,但可用的解决方案并未解决我的具体情况。我添加了第二个活动,我从第一个活动中使用以下代码调用:

        Intent intent = new Intent(MainActivity.this, timerActivity.class);
            intent.putExtra("tmrPredTime", PredTime);
            startActivity(intent);
        return true;

这是我的Android清单

    <?xml version="1.0" encoding="utf-8"?>

<application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >
    <activity
        android:name=".MainActivity"
        android:label="@string/app_name"
        android:screenOrientation="portrait"
        android:windowSoftInputMode="adjustPan|adjustResize" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>

    <activity
        android:name=".timerActivity"
        android:label="@string/title_activity_timer" >
    </activity>
</application>

在“startActivity(intent)”上,应用程序崩溃并显示以下logcat输出。

    03-24 15:58:43.825    7005-7005/com.byu.iainhunter.iku E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: com.byu.iainhunter.iku, PID: 7005
java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.byu.iainhunter.iku/com.byu.iainhunter.iku.timerActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'android.view.View android.view.Window.findViewById(int)' on a null object reference
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2225)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2388)
        at android.app.ActivityThread.access$800(ActivityThread.java:148)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1292)
        at android.os.Handler.dispatchMessage(Handler.java:102)
        at android.os.Looper.loop(Looper.java:135)
        at android.app.ActivityThread.main(ActivityThread.java:5312)
        at java.lang.reflect.Method.invoke(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:372)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:901)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:696)
 Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'android.view.View android.view.Window.findViewById(int)' on a null object reference
        at android.app.Activity.findViewById(Activity.java:2081)
        at com.byu.iainhunter.iku.timerActivity.<init>(timerActivity.java:16)
        at java.lang.reflect.Constructor.newInstance(Native Method)
        at java.lang.Class.newInstance(Class.java:1572)
        at android.app.Instrumentation.newActivity(Instrumentation.java:1088)
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2215)
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2388)
    at android.app.ActivityThread.access$800(ActivityThread.java:148)
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1292)
    at android.os.Handler.dispatchMessage(Handler.java:102)
    at android.os.Looper.loop(Looper.java:135)
    at android.app.ActivityThread.main(ActivityThread.java:5312)
    at java.lang.reflect.Method.invoke(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:372)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:901)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:696)

timerActivity代码

    package com.byu.iainhunter.iku;
    import android.os.Bundle;
    import android.support.v7.app.ActionBarActivity;
    import android.view.Menu;
    import android.view.MenuItem;
    public class timerActivity extends ActionBarActivity {
//final TextView tbTest = (TextView) findViewById(R.id.txtTime);
//final Button buttonStart = (Button)findViewById(R.id.btnStart);

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

    double PredTime = getIntent().getDoubleExtra("tmrPredTime", 0);
    System.out.println("PredTime: " + PredTime);


    //buttonStart.setOnTouchListener(new OnTouchListener() {

    //    @Override
    //    public boolean onTouch(View v, MotionEvent event) {
    //        tbTest.setText("Your Text");
    //        return true;
    //    }
    //});
}


@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.menu_timer, 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();

    //noinspection SimplifiableIfStatement
    if (id == R.id.action_settings) {

        return true;
    }

    return super.onOptionsItemSelected(item);
}

}

1 个答案:

答案 0 :(得分:0)

您在此处发布了一个主要的错误,即发布与您的错误消息不符的代码!您通过注释掉findViewById()行来解决问题,从而发布了不再导致错误的代码。要真正解决您的问题,请在onCreate()方法中移动它们。

/* Declare them here, but don't attempt to lookup, as that is
   not legal until onCreate() is called */

TextView tbTest;
Button buttonStart;

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

    /* only once onCreate() has been called do you have
       a fully initialized Activity instance in which
       it is legal to call methods like findViewById() */

    tbTest = (TextView) findViewById(R.id.txtTime);
    buttonStart = (Button)findViewById(R.id.btnStart);


    double PredTime = getIntent().getDoubleExtra("tmrPredTime", 0);
    System.out.println("PredTime: " + PredTime);