2012-11-11 145 views
0

因此,我一直在關注開始android的wrox書,我似乎無法確定爲什麼我的程序在活動之間切換時崩潰。當我點擊編輯框和按鈕之外的中心d_pad時發生崩潰。Android:當在活動之間切換時崩潰

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開發。

回答

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(...);由於上述原因。

+0

Absouletly不得不交換xml文件才能正常工作。奇怪的是,爲什麼一本教程書會將所需的小部件放在錯誤的XML文件中。 –