我做了一个简单的应用程序,启动一个活动并计算调用了多少个启动,暂停...方法,并且可以启动另一个活动,当我在模拟器中尝试应用程序时,我在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
这是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,我不知道为什么这段代码不能正常使用。
答案 0 :(得分: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
。
答案 1 :(得分:0)
只需创建TextView
的对象当您在oncreate
中使用这些对象时,您应该只在名为instance of TextView
的类中创建ActivityOne
。
TextView mTvCreate;
TextView mTvStart ;
TextView mTvResume ;
TextView mTvRestart;
希望这有帮助。
答案 2 :(得分:0)
尝试在ActivityOne.java
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);