. lang。RuntimeException:无法实例化活动组件信息:2活动。

时间:2022-12-12 20:50:51

I do a simple application that starts one activity and counts how many start,pause... methods was called, and can start another activity, and I get this error in Logcat when I try the aplication in the emulator:

我做一个简单的应用程序,它启动一个活动,计算有多少开始,暂停…方法被调用,并且可以启动另一个活动,当我在模拟器中尝试应用程序时,我在Logcat中得到这个错误:

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

This is the AndoidManifest.xml:

这是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>

This is ActivityOne:

这是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);

    }
}

And this is ActivityTwo:

这是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);

    }

}

I'm beggining with Android and I have no idea why this code doesn't go fine.

我对Android一窍不通,我不知道为什么这段代码不正常。

3 个解决方案

#1


2  

You only need to declare the variables as instance variables

您只需要将变量声明为实例变量。

    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);

You don't need the below

你不需要下面的。

    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);

Even if you need to initialize textview as above context is available only after activity is created. So in that case you need to move it inside onCreate.

即使您需要初始化textview,也只能在创建活动之后才可用。在这种情况下,你需要在onCreate中移动它。

#2


0  

Try to Replace this in your ActivityOne.java

请尝试在您的ActivityOne.java中替换它。

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

with

TextView mTvCreate = new TextView(this);
TextView mTvStart = new TextView(this);
TextView mTvResume = new TextView(this);
TextView mTvRestart = new TextView(this);

#3


0  

Just create the object of TextView As you are using these in oncreate you should only create the instance of TextView in the class named ActivityOne.

在oncreate中创建TextView的对象时,应该只在名为ActivityOne的类中创建TextView的实例。

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

Hope this helps.

希望这个有帮助。

#1


2  

You only need to declare the variables as instance variables

您只需要将变量声明为实例变量。

    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);

You don't need the below

你不需要下面的。

    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);

Even if you need to initialize textview as above context is available only after activity is created. So in that case you need to move it inside onCreate.

即使您需要初始化textview,也只能在创建活动之后才可用。在这种情况下,你需要在onCreate中移动它。

#2


0  

Try to Replace this in your ActivityOne.java

请尝试在您的ActivityOne.java中替换它。

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

with

TextView mTvCreate = new TextView(this);
TextView mTvStart = new TextView(this);
TextView mTvResume = new TextView(this);
TextView mTvRestart = new TextView(this);

#3


0  

Just create the object of TextView As you are using these in oncreate you should only create the instance of TextView in the class named ActivityOne.

在oncreate中创建TextView的对象时,应该只在名为ActivityOne的类中创建TextView的实例。

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

Hope this helps.

希望这个有帮助。