0

所以我一直在关注关于开始 android 的 wrox 书,我似乎无法弄清楚为什么我的程序在活动之间切换时会崩溃。当我单击编辑框和按钮之外的中心 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 开发的不同之处。

4

1 回答 1

3

我猜你的activity2布局没有带有 id 的 Button 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( ... );出于上述原因。

于 2012-11-11T18:21:00.830 回答