2 u014221263 u014221263 于 2014.03.19 14:25 提问

为什么eclipse android 程序一打开就直接停止运行

本人刚开始学习android,跟着书写了个程序,这个程序是在一个activity中的button跳转到另一个activity中,编译完没有问题,但是在手机上运行的时候却是意外停止,

这是第一个activity的代码:

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.Toast;

public class MainActivity extends Activity {

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


    Button brn1=(Button)findViewById(R.id.btn1);
    brn1.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            Intent intent=new Intent();
            intent.setClass(MainActivity.this, other.class);
            startActivity(intent);
        }
    });

这是第二个Activity的代码:

  import android.app.Activity;

import android.os.Bundle;
import android.widget.TextView;

public class other extends Activity{

/* (non-Javadoc)
 * @see android.app.Activity#onCreate(android.os.Bundle)
 */
@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub

    super.onCreate(savedInstanceState);
    setContentView(R.layout.other);
    TextView mytextview =(TextView)findViewById(R.id.textView0);
    mytextview.setText(R.string.other);
}

}

logcat:

03-19 06:36:01.544: E/AndroidRuntime(1579): FATAL EXCEPTION: main
03-19 06:36:01.544: E/AndroidRuntime(1579): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.qwe/com.example.qwe.MainActivity}: java.lang.NullPointerException
03-19 06:36:01.544: E/AndroidRuntime(1579): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180)
03-19 06:36:01.544: E/AndroidRuntime(1579): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
03-19 06:36:01.544: E/AndroidRuntime(1579): at android.app.ActivityThread.access$600(ActivityThread.java:141)
03-19 06:36:01.544: E/AndroidRuntime(1579): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
03-19 06:36:01.544: E/AndroidRuntime(1579): at android.os.Handler.dispatchMessage(Handler.java:99)
03-19 06:36:01.544: E/AndroidRuntime(1579): at android.os.Looper.loop(Looper.java:137)
03-19 06:36:01.544: E/AndroidRuntime(1579): at android.app.ActivityThread.main(ActivityThread.java:5041)
03-19 06:36:01.544: E/AndroidRuntime(1579): at java.lang.reflect.Method.invokeNative(Native Method)
03-19 06:36:01.544: E/AndroidRuntime(1579): at java.lang.reflect.Method.invoke(Method.java:511)
03-19 06:36:01.544: E/AndroidRuntime(1579): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
03-19 06:36:01.544: E/AndroidRuntime(1579): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
03-19 06:36:01.544: E/AndroidRuntime(1579): at dalvik.system.NativeStart.main(Native Method)
03-19 06:36:01.544: E/AndroidRuntime(1579): Caused by: java.lang.NullPointerException
03-19 06:36:01.544: E/AndroidRuntime(1579): at com.example.qwe.MainActivity.onCreate(MainActivity.java:69)
03-19 06:36:01.544: E/AndroidRuntime(1579): at android.app.Activity.performCreate(Activity.java:5104)
03-19 06:36:01.544: E/AndroidRuntime(1579): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
03-19 06:36:01.544: E/AndroidRuntime(1579): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144)
03-19 06:36:01.544: E/AndroidRuntime(1579): ... 11 more

1个回答

lihuozhiling
lihuozhiling   2014.03.20 18:51

这是典型的空指针异常呀 建议你跟一下debug 看看那个对象没有实例化 不过看情况应该是ActivityThread.java

Csdn user default icon
上传中...
上传图片
插入图片
准确详细的回答,更有利于被提问者采纳,从而获得C币。复制、灌水、广告等回答会被删除,是时候展现真正的技术了!