Android教程thenewboston nullpointer异常崩溃

时间:2012-10-29 00:21:16

标签: java android crash nullpointerexception

我在Android开发的“新波士顿”这个系列中,我被困在第49个视频,因为我得到一个错误(我认为是NullPointerException),我无法弄清楚如何解决这个问题。应用程序的测试是在eclipse的android模拟器5554:droidX上完成的。

package bogdy2p.ro.clicknet.web;

 import android.app.Activity;
 import android.os.Bundle;
 import android.view.View;
 import android.view.View.OnClickListener;
 import android.widget.Button;
 import android.widget.RadioGroup;
 import android.widget.RadioGroup.OnCheckedChangeListener;
 import android.widget.TextView;

   public class OpenedClass extends Activity implements OnClickListener,OnCheckedChangeListener {

TextView question, test;
Button returnData;
RadioGroup selectionList;
String gotBread;
String setData;

@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.send);
    initialize();
    Bundle gotBasket = getIntent().getExtras();
    gotBread = gotBasket.getString("key");
    question.setText(gotBread);
}

private void initialize() {
    // TODO Auto-generated method stub
    question = (TextView) findViewById(R.id.tvQuestion);
    test = (TextView) findViewById(R.id.tvText);
    returnData = (Button) findViewById(R.id.bReturn);
    returnData.setOnClickListener(this);
    selectionList = (RadioGroup) findViewById(R.id.rgAnswers);
    selectionList.setOnCheckedChangeListener(this);
}

public void onClick(View arg0) {
    // TODO Auto-generated method stub
}

public void onCheckedChanged(RadioGroup arg0, int arg1) {
    // TODO Auto-generated method stub
    switch (arg1) {
    case R.id.rCrazy:
        setData = "Probably right!";
        break;

    case R.id.rSexy:
        setData = "Definetly right!";
        break;

    case R.id.rBoth:
        setData = "Spot On!";
        break;
    }
    test.setText(setData);
}
   }

LogCat来自Eclipse:

       10-29 00:43:45.111: E/AndroidRuntime(637): FATAL EXCEPTION: main
       10-29 00:43:45.111: E/AndroidRuntime(637): java.lang.RuntimeException: Unable to               start activity ComponentInfo{bogdy2p.ro.clicknet.web/bogdy2p.ro.clicknet.web.OpenedClass}: java.lang.NullPointerException
        10-29 00:43:45.111: E/AndroidRuntime(637):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2059)
        10-29 00:43:45.111: E/AndroidRuntime(637):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2084)
        10-29 00:43:45.111: E/AndroidRuntime(637):  at android.app.ActivityThread.access$600(ActivityThread.java:130)
        10-29 00:43:45.111: E/AndroidRuntime(637):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1195)
        10-29 00:43:45.111: E/AndroidRuntime(637):  at android.os.Handler.dispatchMessage(Handler.java:99)
        10-29 00:43:45.111: E/AndroidRuntime(637):  at android.os.Looper.loop(Looper.java:137)
        10-29 00:43:45.111: E/AndroidRuntime(637):  at android.app.ActivityThread.main(ActivityThread.java:4745)
         10-29 00:43:45.111: E/AndroidRuntime(637):     at java.lang.reflect.Method.invokeNative(Native Method)
        10-29 00:43:45.111: E/AndroidRuntime(637):  at java.lang.reflect.Method.invoke(Method.java:511)
        10-29 00:43:45.111: E/AndroidRuntime(637):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
         10-29 00:43:45.111: E/AndroidRuntime(637):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
         10-29 00:43:45.111: E/AndroidRuntime(637):     at dalvik.system.NativeStart.main(Native Method)
         10-29 00:43:45.111: E/AndroidRuntime(637): Caused by: java.lang.NullPointerException
         10-29 00:43:45.111: E/AndroidRuntime(637):     at bogdy2p.ro.clicknet.web.OpenedClass.onCreate(OpenedClass.java:27)
         10-29 00:43:45.111: E/AndroidRuntime(637):     at android.app.Activity.performCreate(Activity.java:5008)
         10-29 00:43:45.111: E/AndroidRuntime(637):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1079)
         10-29 00:43:45.111: E/AndroidRuntime(637):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2023)
         10-29 00:43:45.111: E/AndroidRuntime(637):     ... 11 more

Manifest.Xml:

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="bogdy2p.ro.clicknet.web"
android:versionCode="1"
android:versionName="1.0" >

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

<uses-permission android:name="android.permission.SET_WALLPAPER" />

<application
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >
    <activity
        android:name=".Splash"
        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=".MainActivity"
        android:label="@string/title_activity_main" >
        <intent-filter>
            <action android:name="bogdy2p.ro.clicknet.web.STARTINGPOINT" />

            <category android:name="android.intent.category.DEFAULT" />
        </intent-filter>
    </activity>
    <activity
        android:name=".Menu"
        android:label="@string/title_activity_main" >
        <intent-filter>
            <action android:name="bogdy2p.ro.clicknet.web.MENU" />

            <category android:name="android.intent.category.DEFAULT" />
        </intent-filter>
    </activity>
    <activity
        android:name=".TextPlay"
        android:label="@string/app_name" >
    </activity>
    <activity
        android:name=".Email"
        android:label="@string/app_name" >
    </activity>
    <activity
        android:name=".Camera"
        android:label="Camera Application"
        android:screenOrientation="portrait" >
    </activity>
    <activity
        android:name=".Data"
        android:label="Data Application" >
    </activity>
    <activity
        android:name=".OpenedClass"
        android:label="Opened Class" >
    </activity>
</application>

这里有Data.java类:

package bogdy2p.ro.clicknet.web;

      import android.app.Activity;
      import android.content.Intent;
      import android.os.Bundle;
      import android.view.View;
      import android.view.View.OnClickListener;
      import android.widget.Button;
      import android.widget.EditText;
      import android.widget.TextView;

  public class Data extends Activity implements OnClickListener {

Button start, startFor;
EditText sendET;
TextView gotAnswer;

@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.get);
}

private void initialize() {

    start = (Button) findViewById(R.id.bSA);
    startFor = (Button) findViewById(R.id.bSAFR);
    sendET = (EditText) findViewById(R.id.etSend);
    gotAnswer = (TextView) findViewById(R.id.tvGot);
    start.setOnClickListener(this);
    startFor.setOnClickListener(this);
}

public void onClick(View arg0) {
    // TODO Auto-generated method stub
    switch (arg0.getId()) {
    case R.id.bSA:
        String bread = sendET.getText().toString();
        Bundle basket = new Bundle();
        basket.putString("key", bread);
        Intent a = new Intent(Data.this, OpenedClass.class);
        a.putExtras(basket);
        startActivity(a);
        break;
    case R.id.bSAFR:
        Intent i = new Intent(Data.this, OpenedClass.class);
        startActivityForResult(i, 0);
        break;
    }
}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    // TODO Auto-generated method stub
    super.onActivityResult(requestCode, resultCode, data);
}
 }

据我读到LogCat,问题是OpenedClass.java文件中的@Lay 27,但我无法弄清问题是什么。我的程序与视频中的程序相同,但我的程序不起作用,在视频中,程序运行成功。

1 个答案:

答案 0 :(得分:2)

您的gotBasket在某些时候为空。这意味着,出于某种原因,你的意图的额外内容不存在。

使用try-catch保护您的代码,或者您可以执行以下操作:

Bundle gotBasket = getIntent().getExtras();
if(gotBasket!=null){
    gotBread = gotBasket.getString("key");
    question.setText(gotBread);
}