Android:在活动之间切换时崩溃

时间:2012-11-11 18:19:01

标签: android android-activity

所以我一直在关注启动android的wrox书我似乎无法确定为什么我的程序在活动之间切换时崩溃。当我点击EditBox和Button之外的中心d_pad时发生崩溃。

The error logcat describe is as follows.
11-11 11:03:50.700: W/dalvikvm(334): threadid=1: thread exiting with uncaught exception (group=0x40015560)
11-11 11:03:50.949: E/AndroidRuntime(334): FATAL EXCEPTION: main
11-11 11:03:50.949: E/AndroidRuntime(334): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.activites/com.example.activites.Activity2}: java.lang.NullPointerException
11-11 11:03:50.949: E/AndroidRuntime(334):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1647)
11-11 11:03:50.949: E/AndroidRuntime(334):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
11-11 11:03:50.949: E/AndroidRuntime(334):  at android.app.ActivityThread.access$1500(ActivityThread.java:117)
11-11 11:03:50.949: E/AndroidRuntime(334):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
11-11 11:03:50.949: E/AndroidRuntime(334):  at android.os.Handler.dispatchMessage(Handler.java:99)
11-11 11:03:50.949: E/AndroidRuntime(334):  at android.os.Looper.loop(Looper.java:123)
11-11 11:03:50.949: E/AndroidRuntime(334):  at android.app.ActivityThread.main(ActivityThread.java:3683)
11-11 11:03:50.949: E/AndroidRuntime(334):  at java.lang.reflect.Method.invokeNative(Native Method)
11-11 11:03:50.949: E/AndroidRuntime(334):  at java.lang.reflect.Method.invoke(Method.java:507)
11-11 11:03:50.949: E/AndroidRuntime(334):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
11-11 11:03:50.949: E/AndroidRuntime(334):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
11-11 11:03:50.949: E/AndroidRuntime(334):  at dalvik.system.NativeStart.main(Native Method)
11-11 11:03:50.949: E/AndroidRuntime(334): Caused by: java.lang.NullPointerException
11-11 11:03:50.949: E/AndroidRuntime(334):  at com.example.activites.Activity2.onCreate(Activity2.java:17)
11-11 11:03:50.949: E/AndroidRuntime(334):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
11-11 11:03:50.949: E/AndroidRuntime(334):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1611)
11-11 11:03:50.949: E/AndroidRuntime(334):  ... 11 more

这是xml文件

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" >

    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="Please enter you name" />
<EditText
    android:id="@+id/txt_username"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    />
<Button
    android:id="@+id/btn_OK"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="OK" />

</LinearLayout>

以下是两个相互交互的类。首先是主要的

package com.example.activites;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.KeyEvent;
import android.widget.Toast;

public class MainActivity extends Activity {
    String tag = "Events";
    int request_Code = 1;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.activity_main);
        Log.d(tag, "In the onCreate() event");
    }

//  @Override
//  public boolean onCreateOptionsMenu(Menu menu) {
//      getMenuInflater().inflate(R.menu.activity_main, menu);
//      return true;
//  }

    public boolean onKeyDown(int keyCode, KeyEvent event)
    {
        if (keyCode== KeyEvent.KEYCODE_DPAD_CENTER)
        {
            startActivityForResult(new Intent("com.example.ACTIVITY2"),request_Code);
        }
        return false;

    }

    public void onActivityResult(int requestCode, int resultCode, Intent data)
    {
        if(requestCode == request_Code){
            if(resultCode==RESULT_OK){
                Toast.makeText(this, data.getData().toString(), Toast.LENGTH_SHORT).show();
            }
        }
    }
}

第二课

package com.example.activites;
import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
public class Activity2 extends Activity {

    public void onCreate(Bundle savedInstanceState){
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity2);
        Button btn = (Button) findViewById(R.id.btn_OK);


    btn.setOnClickListener (new View.OnClickListener(){
        public void onClick (View view){
            Intent data = new Intent();
            EditText txt_username = (EditText) findViewById(R.id.txt_username);

            data.setData(Uri.parse(txt_username.getText().toString()));
            setResult(RESULT_OK, data);
            finish();
        }
    });
}
}

这是清单文件

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.activites"
    android:versionCode="1"
    android:debuggable="true"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="15" />

    <application
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name" >
        <activity
            android:name=".MainActivity"
            android:label="@string/title_activity_main" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity
            android:name=".Activity2"
            android:label="Activity 2" >
            <intent-filter>
                <action android:name="com.example.ACTIVITY2" />

                <category android:name="android.intent.category.DEFAULT" />
            </intent-filter>
        </activity>

    </application>
</manifest>

再次感谢您的帮助/我很惊讶这些Android应用程序来自.net开发的不同调试方式。

1 个答案:

答案 0 :(得分:3)

我猜您的activity2布局没有ID为btn_OK的按钮,因此btnnull

Button btn = (Button) findViewById(R.id.btn_OK);
btn.setOnClickListener( ... ); // Crash here with an NPE

findViewById()只能找到传递给setContentView()的当前布局中的视图,否则会返回null


这是重要的LogCat信息:

Caused by: java.lang.NullPointerException
    at com.example.activites.Activity2.onCreate(Activity2.java:17)

注意最后一位Activity2.java:17,即发生错误的文件和行号。我猜是:btn.setOnClickListener( ... );由于上述原因。