您好我刚开始使用Android。请帮忙,阅读有关此内容的所有帖子,没什么好帮助的。尝试清单中的所有组合命名活动。此应用程序显示应用程序的生命周期。
日志
02-11 01:07:36.538: W/dalvikvm(20921): threadid=1: thread exiting with uncaught exception (group=0x40ab6228)
02-11 01:07:36.558: E/AndroidRuntime(20921): FATAL EXCEPTION: main
02-11 01:07:36.558: E/AndroidRuntime(20921): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.example.lab1/com.example.lab1.MainActivity}: java.lang.NullPointerException
02-11 01:07:36.558: E/AndroidRuntime(20921): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2121)
02-11 01:07:36.558: E/AndroidRuntime(20921): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2240)
02-11 01:07:36.558: E/AndroidRuntime(20921): at android.app.ActivityThread.access$600(ActivityThread.java:139)
02-11 01:07:36.558: E/AndroidRuntime(20921): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1262)
02-11 01:07:36.558: E/AndroidRuntime(20921): at android.os.Handler.dispatchMessage(Handler.java:99)
02-11 01:07:36.558: E/AndroidRuntime(20921): at android.os.Looper.loop(Looper.java:156)
02-11 01:07:36.558: E/AndroidRuntime(20921): at android.app.ActivityThread.main(ActivityThread.java:4977)
02-11 01:07:36.558: E/AndroidRuntime(20921): at java.lang.reflect.Method.invokeNative(Native Method)
02-11 01:07:36.558: E/AndroidRuntime(20921): at java.lang.reflect.Method.invoke(Method.java:511)
02-11 01:07:36.558: E/AndroidRuntime(20921): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
02-11 01:07:36.558: E/AndroidRuntime(20921): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
02-11 01:07:36.558: E/AndroidRuntime(20921): at dalvik.system.NativeStart.main(Native Method)
02-11 01:07:36.558: E/AndroidRuntime(20921): Caused by: java.lang.NullPointerException
02-11 01:07:36.558: E/AndroidRuntime(20921): at android.content.ContextWrapper.getResources(ContextWrapper.java:81)
02-11 01:07:36.558: E/AndroidRuntime(20921): at android.view.View.<init>(View.java:2723)
02-11 01:07:36.558: E/AndroidRuntime(20921): at android.view.View.<init>(View.java:2771)
02-11 01:07:36.558: E/AndroidRuntime(20921): at android.widget.TextView.<init>(TextView.java:504)
02-11 01:07:36.558: E/AndroidRuntime(20921): at android.widget.TextView.<init>(TextView.java:494)
02-11 01:07:36.558: E/AndroidRuntime(20921): at android.widget.TextView.<init>(TextView.java:489)
02-11 01:07:36.558: E/AndroidRuntime(20921): at com.example.lab1.MainActivity.<init>(MainActivity.java:30)
02-11 01:07:36.558: E/AndroidRuntime(20921): at java.lang.Class.newInstanceImpl(Native Method)
02-11 01:07:36.558: E/AndroidRuntime(20921): at java.lang.Class.newInstance(Class.java:1319)
02-11 01:07:36.558: E/AndroidRuntime(20921): at android.app.Instrumentation.newActivity(Instrumentation.java:1039)
02-11 01:07:36.558: E/AndroidRuntime(20921): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2112)
02-11 01:07:36.558: E/AndroidRuntime(20921): ... 11 more
清单
<?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>
MainActivity
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);
}
}
ActivityTwo
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);
}
}
答案 0 :(得分:1)
我自己有点新鲜所以我可以说垃圾....
我将摆脱textView1上的成员初始化 - &gt; textView4并将其移动到setContentView之后。接下来我会在super.OnCreate上放置一个断点,看看它是否到达那里。如果你单行执行每一行,直到看到空指针。