2014-02-15 81 views
0

我做一個簡單的應用程序,啓動一個活動以及計數有多少開始,暫停...方法被調用,並且可以啓動另一個活動,我得到這個在logcat的錯誤,當我嘗試aplication在模擬器:了java.lang.RuntimeException:無法實例活動ComponentInfo:2個活動

02-07 02:00:35.896: W/dalvikvm(1225): threadid=1: thread exiting with uncaught exception (group=0x40a71930) 
02-07 02:00:36.123: E/AndroidRuntime(1225): FATAL EXCEPTION: main 
02-07 02:00:36.123: E/AndroidRuntime(1225): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{course.labs.activitylab/course.labs.activitylab.ActivityOne}: java.lang.NullPointerException 
02-07 02:00:36.123: E/AndroidRuntime(1225):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2106) 
02-07 02:00:36.123: E/AndroidRuntime(1225):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230) 
02-07 02:00:36.123: E/AndroidRuntime(1225):  at android.app.ActivityThread.access$600(ActivityThread.java:141) 
02-07 02:00:36.123: E/AndroidRuntime(1225):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234) 
02-07 02:00:36.123: E/AndroidRuntime(1225):  at android.os.Handler.dispatchMessage(Handler.java:99) 
02-07 02:00:36.123: E/AndroidRuntime(1225):  at android.os.Looper.loop(Looper.java:137) 
02-07 02:00:36.123: E/AndroidRuntime(1225):  at android.app.ActivityThread.main(ActivityThread.java:5041) 
02-07 02:00:36.123: E/AndroidRuntime(1225):  at java.lang.reflect.Method.invokeNative(Native Method) 
02-07 02:00:36.123: E/AndroidRuntime(1225):  at java.lang.reflect.Method.invoke(Method.java:511) 
02-07 02:00:36.123: E/AndroidRuntime(1225):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793) 
02-07 02:00:36.123: E/AndroidRuntime(1225):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560) 
02-07 02:00:36.123: E/AndroidRuntime(1225):  at dalvik.system.NativeStart.main(Native Method) 
02-07 02:00:36.123: E/AndroidRuntime(1225): Caused by: java.lang.NullPointerException 
02-07 02:00:36.123: E/AndroidRuntime(1225):  at android.content.ContextWrapper.getResources(ContextWrapper.java:89) 
02-07 02:00:36.123: E/AndroidRuntime(1225):  at android.view.ContextThemeWrapper.getResources(ContextThemeWrapper.java:78) 
02-07 02:00:36.123: E/AndroidRuntime(1225):  at android.view.View.<init>(View.java:3226) 
02-07 02:00:36.123: E/AndroidRuntime(1225):  at android.view.View.<init>(View.java:3281) 
02-07 02:00:36.123: E/AndroidRuntime(1225):  at android.widget.TextView.<init>(TextView.java:583) 
02-07 02:00:36.123: E/AndroidRuntime(1225):  at android.widget.TextView.<init>(TextView.java:578) 
02-07 02:00:36.123: E/AndroidRuntime(1225):  at android.widget.TextView.<init>(TextView.java:574) 
02-07 02:00:36.123: E/AndroidRuntime(1225):  at course.labs.activitylab.ActivityOne.<init>(ActivityOne.java:38) 
02-07 02:00:36.123: E/AndroidRuntime(1225):  at java.lang.Class.newInstanceImpl(Native Method) 
02-07 02:00:36.123: E/AndroidRuntime(1225):  at java.lang.Class.newInstance(Class.java:1319) 
02-07 02:00:36.123: E/AndroidRuntime(1225):  at android.app.Instrumentation.newActivity(Instrumentation.java:1054) 
02-07 02:00:36.123: E/AndroidRuntime(1225):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2097) 
02-07 02:00:36.123: E/AndroidRuntime(1225):  ... 11 more 

這是AndoidManifest.xml:

<?xml version="1.0" encoding="utf-8"?> 
<manifest xmlns:android="http://schemas.android.com/apk/res/android" 
    package="course.labs.activitylab" 
    android:versionCode="1" 
    android:versionName="1.0" > 

    <uses-sdk 
     android:minSdkVersion="8" 
     android:targetSdkVersion="18" /> 

    <application 
     android:allowBackup="true" 
     android:icon="@drawable/ic_launcher" 
     android:label="@string/app_name" 
     android:theme="@style/AppTheme" > 
     <activity 
      android:name="course.labs.activitylab.ActivityOne" 
      android:label="@string/app_name" > 
      <intent-filter> 
       <action android:name="android.intent.action.MAIN" /> 

       <category android:name="android.intent.category.LAUNCHER" /> 
      </intent-filter> 
     </activity> 
     <activity 
      android:name="course.labs.activitylab.ActivityTwo" 
      android:label="@string/title_activity_activity_two" > 
     </activity> 
    </application> 

</manifest> 

這是ActivityOne:

package course.labs.activitylab; 

import android.app.Activity; 
import android.content.Intent; 
import android.os.Bundle; 
import android.util.Log; 
import android.view.View; 
import android.view.View.OnClickListener; 
import android.widget.Button; 
import android.widget.TextView; 

public class ActivityOne extends Activity { 

    private static final String RESTART_KEY = "restart"; 
    private static final String RESUME_KEY = "resume"; 
    private static final String START_KEY = "start"; 
    private static final String CREATE_KEY = "create"; 
    private static final String STOP_KEY = "stop"; 
    private static final String PAUSE_KEY = "pause"; 
    private static final String DESTROY_KEY = "destroy"; 

    // String for LogCat documentation 
    private final static String TAG = "Lab-ActivityOne";//mirar lo que sale en el log 


    // Lifecycle counters 
    int mCreate = 0; 
    int mStart = 0; 
    int mResume = 0; 
    int mRestart = 0; 
    // TODO: 
    // Create counter variables for onCreate(), onRestart(), onStart() and 
    // onResume(), called mCreate, etc. 
    // You will need to increment these variables' values when their 
    // corresponding lifecycle methods get called 
    TextView mTvCreate = new TextView(this); 
    TextView mTvStart = new TextView(this); 
    TextView mTvResume = new TextView(this); 
    TextView mTvRestart = new TextView(this); 


    // TODO: Create variables for each of the TextViews, called 
     // mTvCreate, etc. 

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

     // TODO: Assign the appropriate TextViews to the TextView variables 
     // Hint: Access the TextView by calling Activity's findViewById() 
     // textView1 = (TextView) findViewById(R.id.textView1); 
     mTvCreate = (TextView) findViewById(R.id.create); 
     mTvStart = (TextView) findViewById(R.id.start); 
     mTvResume = (TextView) findViewById(R.id.resume); 
     mTvRestart = (TextView) findViewById(R.id.restart); 

     Log.i(TAG, CREATE_KEY); 


     Button launchActivityTwoButton = (Button) findViewById(R.id.bLaunchActivityTwo); 
     launchActivityTwoButton.setOnClickListener(new OnClickListener() { 

      @Override 
      public void onClick(View v) { 
       // TODO: 
       // Launch Activity Two 
       // Hint: use Context's startActivity() method 

       // Create an intent stating which Activity you would like to start 
       Intent myIntent = new Intent(ActivityOne.this, ActivityTwo.class); 

       // Launch the Activity using the intent 
       ActivityOne.this.startActivity(myIntent); 

      } 
     }); 

     // Check for previously saved state 
     if (savedInstanceState != null) { 

      // TODO: 
      // Restore value of counters from saved state 
      // Only need 4 lines of code, one for every count variable 
      mCreate = savedInstanceState.getInt("create"); 
      mResume = savedInstanceState.getInt("resume"); 
      mStart = savedInstanceState.getInt("start"); 
      mRestart = savedInstanceState.getInt("restart"); 



     } 

     // TODO: Emit LogCat message 
     Log.i(TAG, CREATE_KEY); 

     // TODO: 
     // Update the appropriate count variable 
     // Update the user interface via the displayCounts() method 
     mCreate+=1; 
     displayCounts(); 


    } 

    // Lifecycle callback overrides 

    @Override 
    public void onStart() { 
     super.onStart(); 

     // TODO: Emit LogCat message 
     Log.i(TAG, START_KEY); 

     // TODO: 
     // Update the appropriate count variable 
     // Update the user interface 
     mStart+=1; 
     displayCounts(); 
    } 

    @Override 
    public void onResume() { 
     super.onResume(); 

     // TODO: Emit LogCat message 
     Log.i(TAG, RESUME_KEY); 

     // TODO: 
     // Update the appropriate count variable 
     // Update the user interface 
     mResume+=1; 
     displayCounts(); 
    } 

    @Override 
    public void onPause() { 
     super.onPause(); 

     // TODO: Emit LogCat message 
     Log.i(TAG,PAUSE_KEY); 
    } 

    @Override 
    public void onStop() { 
     super.onStop(); 

     // TODO: Emit LogCat message 
     Log.i(TAG, STOP_KEY); 
    } 

    @Override 
    public void onRestart() { 
     super.onRestart(); 

     // TODO: Emit LogCat message 
     Log.i(TAG, RESTART_KEY); 

     // TODO: 
     // Update the appropriate count variable 
     // Update the user interface 
     mRestart+=1; 
     displayCounts(); 

    } 

    @Override 
    public void onDestroy() { 
     super.onDestroy(); 

     // TODO: Emit LogCat message 
     Log.i(TAG, DESTROY_KEY); 

    } 

    @Override 
    public void onSaveInstanceState(Bundle savedInstanceState) { 
     // TODO: 
     // Save state information with a collection of key-value pairs 
     // 4 lines of code, one for every count variable 
     savedInstanceState.putInt("onCreate", mCreate); 
     savedInstanceState.putInt("onStart", mStart); 
     savedInstanceState.putInt("onResume", mResume); 
     savedInstanceState.putInt("onRestart", mRestart); 





    } 

    // Updates the displayed counters 
    public void displayCounts() { 

     mTvCreate.setText("onCreate() calls: " + mCreate); 
     mTvStart.setText("onStart() calls: " + mStart); 
     mTvResume.setText("onResume() calls: " + mResume); 
     mTvRestart.setText("onRestart() calls: " + mRestart); 

    } 
} 

這是ActivityTwo:

package course.labs.activitylab; 

import android.app.Activity; 
import android.content.Intent; 
import android.os.Bundle; 
import android.util.Log; 
import android.view.View; 
import android.view.View.OnClickListener; 
import android.widget.Button; 
import android.widget.TextView; 

public class ActivityTwo extends Activity { 

    private static final String RESTART_KEY = "restart"; 
    private static final String RESUME_KEY = "resume"; 
    private static final String START_KEY = "start"; 
    private static final String CREATE_KEY = "create"; 
    private static final String PAUSE_KEY = "pause"; 
    private static final String DESTROY_KEY = "destroy"; 
    private static final String STOP_KEY = "stop"; 

    // String for LogCat documentation 
    private final static String TAG = "Lab-ActivityTwo"; 

    // Lifecycle counters 
    int mCreate = 0; 
    int mStart = 0; 
    int mResume = 0; 
    int mRestart = 0; 
    // TODO: 
    // Create counter variables for onCreate(), onRestart(), onStart() and 
    // onResume(), called mCreate, etc. 
    // You will need to increment these variables' values when their 
    // corresponding lifecycle methods get called 
    TextView mTvCreate; 
    TextView mTvStart; 
    TextView mTvResume; 
    TextView mTvRestart; 


    // TODO: Create variables for each of the TextViews, called 
     // mTvCreate, etc. 


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

     // TODO: Assign the appropriate TextViews to the TextView variables 
     // Hint: Access the TextView by calling Activity's findViewById() 
     // textView1 = (TextView) findViewById(R.id.textView1); 
     mTvCreate = (TextView) findViewById(R.id.create); 
     mTvStart = (TextView) findViewById(R.id.start); 
     mTvResume = (TextView) findViewById(R.id.resume); 
     mTvRestart = (TextView) findViewById(R.id.restart); 




     Button closeButton = (Button) findViewById(R.id.bClose); 
     closeButton.setOnClickListener(new OnClickListener() { 

      @Override 
      public void onClick(View v) { 

       // TODO: 
       // This function closes Activity Two 
       // Hint: use Context's finish() method 

       finish(); 

      } 
     }); 

     // Check for previously saved state 
     if (savedInstanceState != null) { 

      // TODO: 
      // Restore value of counters from saved state 
      // Only need 4 lines of code, one for every count variable 
      mCreate = savedInstanceState.getInt("create"); 
      mResume = savedInstanceState.getInt("resume"); 
      mStart = savedInstanceState.getInt("start"); 
      mRestart = savedInstanceState.getInt("restart"); 


     } 

     // TODO: Emit LogCat message 
     Log.i(TAG, CREATE_KEY); 


     // TODO: 
     // Update the appropriate count variable 
     // Update the user interface via the displayCounts() method 
     mCreate+=1; 
     displayCounts(); 



    } 

    // Lifecycle callback methods overrides 

    @Override 
    public void onStart() { 
     super.onStart(); 

     // TODO: Emit LogCat message 
     Log.i(TAG, START_KEY); 

     // TODO: 
     // Update the appropriate count variable 
     // Update the user interface 
     mStart+=1; 
     displayCounts(); 


    } 

    @Override 
    public void onResume() { 
     super.onResume(); 

     // TODO: Emit LogCat message 
     Log.i(TAG, RESUME_KEY); 

     // TODO: 
     // Update the appropriate count variable 
     // Update the user interface 
     mResume+=1; 
     displayCounts(); 



    } 

    @Override 
    public void onPause() { 
     super.onPause(); 

     // TODO: Emit LogCat message 
     Log.i(TAG,PAUSE_KEY); 


    } 

    @Override 
    public void onStop() { 
     super.onStop(); 

     // TODO: Emit LogCat message 
     Log.i(TAG, STOP_KEY); 


    } 

    @Override 
    public void onRestart() { 
     super.onRestart(); 

     // TODO: Emit LogCat message 
     Log.i(TAG, RESTART_KEY); 

     // TODO: 
     // Update the appropriate count variable 
     // Update the user interface 
     mRestart+=1; 
     displayCounts(); 


    } 

    @Override 
    public void onDestroy() { 
     super.onDestroy(); 

     // TODO: Emit LogCat message 
     Log.i(TAG, DESTROY_KEY); 
    } 

    @Override 
    public void onSaveInstanceState(Bundle savedInstanceState) { 

     // TODO: 
     // Save counter state information with a collection of key-value pairs 
     // 4 lines of code, one for every count variable 

     savedInstanceState.putInt("create", mCreate); 
     savedInstanceState.putInt("start", mStart); 
     savedInstanceState.putInt("resume", mResume); 
     savedInstanceState.putInt("restart", mRestart); 




    } 

    // Updates the displayed counters 
    public void displayCounts() { 

     mTvCreate.setText("onCreate() calls: " + mCreate); 
     mTvStart.setText("onStart() calls: " + mStart); 
     mTvResume.setText("onResume() calls: " + mResume); 
     mTvRestart.setText("onRestart() calls: " + mRestart); 

    } 

} 

我與Android beggining,我不知道爲什麼這個代碼不走的罰款。

回答

2

你只需要聲明的變量,實例變量

TextView mTvCreate; 
    TextView mTvStart; 
    TextView mTvResume; 
    TextView mTvRestart; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_two); 
    mTvCreate = (TextView) findViewById(R.id.create); 
    mTvStart = (TextView) findViewById(R.id.start); 
    mTvResume = (TextView) findViewById(R.id.resume); 
    mTvRestart = (TextView) findViewById(R.id.restart); 

你不需要下面

TextView mTvCreate = new TextView(this); 
    // NUllPointerException coz context is null 
    TextView mTvStart = new TextView(this); 
    TextView mTvResume = new TextView(this); 
    TextView mTvRestart = new TextView(this); 

即使你需要初始化的TextView作爲上述背景下唯一的活動後可用被建造。所以在這種情況下,你需要在onCreate之內移動它。

+0

非常感謝你現在工作正常 – user3312612

0

正如你在oncreate使用這些剛剛創建的TextView的對象,你應該只在名爲ActivityOne類創建instance of TextView

TextView mTvCreate; 
TextView mTvStart ; 
TextView mTvResume ; 
TextView mTvRestart; 

希望這會有所幫助。

0

嘗試在你的

TextView mTvCreate; 
TextView mTvStart; 
TextView mTvResume; 
TextView mTvRestart; 

替換爲這個

TextView mTvCreate = new TextView(this); 
TextView mTvStart = new TextView(this); 
TextView mTvResume = new TextView(this); 
TextView mTvRestart = new TextView(this); 
相關問題