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

问题描述:

这似乎是一个常见问题,但可用的解决方案尚未解决我的具体情况。我还说,我从第一次打电话用下面的代码第二个活动:用startActivity调用第二个活动导致崩溃

 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(意图)“,应用程序崩溃与下面的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); 
} 

}

+0

显示'timerActivity'的代码,出现这个问题。从日志看来,你在构造函数中调用'findViewById()',你不能那样做。 – Tomik 2015-03-24 22:21:06

+0

我无法在评论中填入timerActivity的所有代码,但我在那里使用了findViewById()。我会搞砸了,看看我得到了什么。 – user2428818 2015-03-24 22:29:56

+0

当我删除timerActivity中的findViewById行时,一切都很好,但我需要在timerActivity中声明一个按钮以供使用。我如何申报?我曾经使用过:final Button buttonStart =(Button)findViewById(R.id.btnStart); – user2428818 2015-03-24 22:38:34

你已经张贴在这里的代码不符合您的错误消息犯了大忌!您通过注释掉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); 
+0

照顾我所有的烦恼。感谢您的帮助。 – user2428818 2015-03-25 14:01:17