Java 无法实例化活动Android

Java 无法实例化活动Android,java,android,eclipse,android-activity,Java,Android,Eclipse,Android Activity,你好,我刚刚开始使用Android。请帮忙,阅读所有关于这个的帖子,没有什么能帮上忙的。尝试了清单中命名活动的所有组合。此应用程序显示应用程序的生命周期 日志 活动二 package com.example.lab1; import android.app.Activity; import android.content.Context; import android.os.Bundle; import android.util.Log; import android.view.View; i

你好,我刚刚开始使用Android。请帮忙,阅读所有关于这个的帖子,没有什么能帮上忙的。尝试了清单中命名活动的所有组合。此应用程序显示应用程序的生命周期

日志

活动二

package com.example.lab1;

import android.app.Activity;
import android.content.Context;
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";

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

    // Lifecycle counters

    // TODO:
    int mCreate=0;
    int mResume=0;
    int mRestart=0;
    int mStart=0;
    TextView textView1 = new TextView(this);
    TextView textView2 = new TextView(this);
    TextView textView3 = new TextView(this);
    TextView textView4 = new TextView(this);


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

        textView1 = (TextView) findViewById(R.id.create);
        textView2 = (TextView) findViewById(R.id.start);
        textView3 = (TextView) findViewById(R.id.resume);
        textView4 = (TextView) findViewById(R.id.restart);





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

            @Override
            public void onClick(View v) {

              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_KEY);
            mResume=savedInstanceState.getInt(RESUME_KEY);
            mRestart=savedInstanceState.getInt(RESTART_KEY);
            mStart=savedInstanceState.getInt(START_KEY);


        }

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


        // TODO:
        mCreate++;
        displayCounts();


        // TODO:
        // Update the appropriate count variable
        // Update the user interface via the displayCounts() method




    }

    // Lifecycle callback methods overrides

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

        // TODO: Emit LogCat message


        // TODO:
        // Update the appropriate count variable
        // Update the user interface
        // TODO: Emit LogCat message
        Log.i(TAG,"OnStart");


        // TODO:
        mStart++;
        displayCounts();


    }

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

        // TODO: Emit LogCat message


        // TODO:
        // Update the appropriate count variable
        // Update the user interface
        Log.i(TAG,"OnResume");


        // TODO:
        mResume++;
        displayCounts();




    }

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

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


    }

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

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


    }

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

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


        // TODO:
        mRestart++;
        displayCounts();




    }

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

        Log.i(TAG,"OnDestroy");

    }

    @Override
    public void onSaveInstanceState(Bundle savedInstanceState) {

        // TODO:
        savedInstanceState.putInt(RESTART_KEY,mRestart);
        savedInstanceState.putInt(RESUME_KEY,mResume);
        savedInstanceState.putInt(START_KEY,mStart);
        savedInstanceState.putInt(CREATE_KEY,mCreate);







    }

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

        textView1.setText("onCreate() calls: " + mCreate);
        textView2.setText("onStart() calls: " + mStart);
        textView3.setText("onResume() calls: " + mResume);
        textView4.setText("onRestart() calls: " + mRestart);

    }
}

我自己对此有点陌生,所以我可能会说些废话


我将在textView1->textView4上删除成员初始化,并将其移动到setContentView之后。接下来,我将在super.OnCreate上放置一个断点,看看它是否到达那里。如果每行执行一步,直到看到空指针为止

这不是一个问题。请解释你正在尝试做什么以及你尝试了什么。对不起,我发现了问题。错误的TextView初始化.Thx,必须是TextView textView1=null;我认为问题在于,主视图只有在使用setContentView扩展XML之后才能设置。在setContentView之后创建TextView(如我在回答中所说)
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.lab1"
    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="MainActivity"
            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="ActivityTwo"
            android:label="@string/app_name" >
        </activity>

    </application>

</manifest>
package com.example.lab1;

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 MainActivity 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";

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

    // Lifecycle counters

    // TODO:

    int mCreate=0;
    int mResume=0;
    int mRestart=0;
    int mStart=0;
    TextView textView1 = new TextView(this);
    TextView textView2 = new TextView(this);
    TextView textView3 = new TextView(this);
    TextView textView4 = 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.create);
        textView2 = (TextView) findViewById(R.id.start);
        textView3 = (TextView) findViewById(R.id.resume);
        textView4 = (TextView) findViewById(R.id.restart);





        Button launchActivityTwoButton = new Button(this);
        launchActivityTwoButton = (Button) findViewById(R.id.bLaunchActivityTwo);
        launchActivityTwoButton.setOnClickListener(new OnClickListener()
        {
            @Override
            public void onClick(View view)
            {
                // TODO:
                // Launch Activity Two
                // Hint: use Context's startActivity() method

                // Create an intent stating which Activity you would like to start


                // Launch the Activity using the intent
                Intent myIntent=new Intent(view.getContext(),ActivityTwo.class);
                startActivity(myIntent);

            }
        });






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

            mCreate=savedInstanceState.getInt(CREATE_KEY);
            mResume=savedInstanceState.getInt(RESUME_KEY);
            mRestart=savedInstanceState.getInt(RESTART_KEY);
            mStart=savedInstanceState.getInt(START_KEY);


        }

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


        // TODO:
        mCreate++;
        //displayCounts();



    }

    // Lifecycle callback overrides

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

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


        // TODO:
        mStart++;
        //displayCounts();


    }

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

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


        // TODO:
        mResume++;
        displayCounts();


    }

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

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

    }

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

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


    }

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

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


        // TODO:
        mRestart++;
        displayCounts();



    }

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

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


    }

    @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(RESTART_KEY,mRestart);
        savedInstanceState.putInt(RESUME_KEY,mResume);
        savedInstanceState.putInt(START_KEY,mStart);
        savedInstanceState.putInt(CREATE_KEY,mCreate);





    }

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

        textView1.setText("onCreate() calls: " + mCreate);
        textView2.setText("onStart() calls: " + mStart);
        textView3.setText("onResume() calls: " + mResume);
        textView4.setText("onRestart() calls: " + mRestart);

    }
}
package com.example.lab1;

import android.app.Activity;
import android.content.Context;
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";

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

    // Lifecycle counters

    // TODO:
    int mCreate=0;
    int mResume=0;
    int mRestart=0;
    int mStart=0;
    TextView textView1 = new TextView(this);
    TextView textView2 = new TextView(this);
    TextView textView3 = new TextView(this);
    TextView textView4 = new TextView(this);


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

        textView1 = (TextView) findViewById(R.id.create);
        textView2 = (TextView) findViewById(R.id.start);
        textView3 = (TextView) findViewById(R.id.resume);
        textView4 = (TextView) findViewById(R.id.restart);





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

            @Override
            public void onClick(View v) {

              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_KEY);
            mResume=savedInstanceState.getInt(RESUME_KEY);
            mRestart=savedInstanceState.getInt(RESTART_KEY);
            mStart=savedInstanceState.getInt(START_KEY);


        }

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


        // TODO:
        mCreate++;
        displayCounts();


        // TODO:
        // Update the appropriate count variable
        // Update the user interface via the displayCounts() method




    }

    // Lifecycle callback methods overrides

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

        // TODO: Emit LogCat message


        // TODO:
        // Update the appropriate count variable
        // Update the user interface
        // TODO: Emit LogCat message
        Log.i(TAG,"OnStart");


        // TODO:
        mStart++;
        displayCounts();


    }

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

        // TODO: Emit LogCat message


        // TODO:
        // Update the appropriate count variable
        // Update the user interface
        Log.i(TAG,"OnResume");


        // TODO:
        mResume++;
        displayCounts();




    }

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

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


    }

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

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


    }

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

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


        // TODO:
        mRestart++;
        displayCounts();




    }

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

        Log.i(TAG,"OnDestroy");

    }

    @Override
    public void onSaveInstanceState(Bundle savedInstanceState) {

        // TODO:
        savedInstanceState.putInt(RESTART_KEY,mRestart);
        savedInstanceState.putInt(RESUME_KEY,mResume);
        savedInstanceState.putInt(START_KEY,mStart);
        savedInstanceState.putInt(CREATE_KEY,mCreate);







    }

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

        textView1.setText("onCreate() calls: " + mCreate);
        textView2.setText("onStart() calls: " + mStart);
        textView3.setText("onResume() calls: " + mResume);
        textView4.setText("onRestart() calls: " + mRestart);

    }
}