如何处理Android Studio中的空点异常 [英] How Do I Deal With The Null Point Exception In Android Studio
本文介绍了如何处理Android Studio中的空点异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
package com.example.dell.alc;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.TextView;
public class MainActivity extends ActionBarActivity {
public static final String TAG = "Lifecycle_activity";
public static int createCount = 0;
public static int startCount = 0;
public static int restartCount = 0;
public static int resumeCount = 0;
public static int pauseCount = 0;
public static int stopCount = 0;
public static int destroyCount = 0;
TextView tv,tv1,tv2,tv3,tv4,tv5,tv6;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
createCount++;
Log.d(TAG,"The number of times the Activity created: " + createCount);
if(createCount>=1){
TextView tv = (TextView) findViewById(R.id.onCreate);
tv.setText("Start count:" + createCount);
}
setContentView(R.layout.activity_main);
}
@Override
protected void onStart() {
super.onStart();
startCount++;
Log.d(TAG,"The number of times the Activity started: " + startCount);
if(startCount>=1){
TextView tv1 = (TextView) findViewById(R.id.onStart);
tv1.setText("Start count:" + startCount);
}
}
@Override
protected void onRestart() {
super.onRestart();
startCount++;
Log.d(TAG,"The number of times the Activity Restarted: " + startCount);
if(restartCount>1){
TextView tv2 = (TextView) findViewById(R.id.onRestart);
tv2.setText("Restart count:" + restartCount);
}
}
@Override
protected void onStop() {
super.onStop();
stopCount++;
Log.d(TAG,"The number of times the Activity stopped: " + stopCount);
if(stopCount>=1){
tv3 = (TextView) findViewById(R.id.onStop);
tv3.setText("stop count:" + stopCount);
}
}
@Override
protected void onResume() {
super.onResume();
resumeCount++;
Log.d(TAG,"The number of times the Activity is resumed: " + resumeCount);
if(resumeCount>=1){
TextView tv4 = (TextView) findViewById(R.id.onResume);
tv4.setText("Resume count:" + resumeCount);
}
}
@Override
protected void onPause() {
super.onPause();
startCount++;
Log.d(TAG,"The number of times the Activity paused: " + pauseCount);
if(pauseCount>=0){
TextView tv5 = (TextView) findViewById(R.id.onPause);
tv5.setText("pause count:" + pauseCount);
}
}
@Override
protected void onDestroy() {
super.onDestroy();
destroyCount++;
Log.d(TAG, "The number of times the Activity created: " + destroyCount);
if (destroyCount >= 0) {
TextView tv6 = (TextView) findViewById(R.id.onDestroy);
tv6.setText("destroy count:" + destroyCount);
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
推荐答案
02-28 05:37:49.581 1120-1120 / com.example。 dell.alc D / dalvikvm:延迟启用CheckJNI
02-28 05:37:49.749 1120-1120 / com.example.dell.alc D / Lifecycle_activity:活动创建的次数: 1
02-28 05:37:49.749 1120-1120 / com.example.dell.alc D / AndroidRuntime:关闭VM
02-28 05:37: 49.749 1120-1120 / com.example.dell.alc W / dalvikvm:threadid = 1:线程退出未捕获的异常(组= 0xa4bd3648)
02-28 05:37:49.749 1120-1120 / com.example.dell.alc E / AndroidRuntime:FATAL EXCEPTION:main
java.lang.RuntimeException:无法启动活动ComponentInfo {com.example.dell.alc / com.example.dell.alc .MainActivity}:java.lang.NullPointerException
在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2211)
在android.app.ActivityThread.handleLaunchActivity(Ac tivityThread.java:2261)
在android.app.ActivityThread.access
02-28 05:37:49.581 1120-1120/com.example.dell.alc D/dalvikvm﹕ Late-enabling CheckJNI
02-28 05:37:49.749 1120-1120/com.example.dell.alc D/Lifecycle_activity﹕ The number of times the Activity created: 1
02-28 05:37:49.749 1120-1120/com.example.dell.alc D/AndroidRuntime﹕ Shutting down VM
02-28 05:37:49.749 1120-1120/com.example.dell.alc W/dalvikvm﹕ threadid=1: thread exiting with uncaught exception (group=0xa4bd3648)
02-28 05:37:49.749 1120-1120/com.example.dell.alc E/AndroidRuntime﹕ FATAL EXCEPTION: main
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.dell.alc/com.example.dell.alc.MainActivity}: java.lang.NullPointerException
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2211)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2261)
at android.app.ActivityThread.access
600(ActivityThread.java:141)
at android.app.ActivityThread
600(ActivityThread.java:141)
at android.app.ActivityThread
H.handleMessage(ActivityThread.java:1256)
在android.os.Handler.dispatchMessage(Handler.java:99)
在android.os.Looper.loop(Looper.java:137)
在android.app.ActivityThread.main(ActivityThread.java:5103)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:525)
在com.android.internal.os.ZygoteInit
H.handleMessage(ActivityThread.java:1256)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:5103)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:525)
at com.android.internal.os.ZygoteInit
这篇关于如何处理Android Studio中的空点异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文