2

我在 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);
}
   }

来自 Eclipse 的 LogCat:

       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 而言,问题是 @ Line 27 in OpenedClass.java file ,但我不知道问题是什么。我的程序与视频中的程序相同,但我的程序不起作用,并且在视频中,程序运行成功。

4

1 回答 1

2

gotBasket在某些时候是空的。这意味着,出于某种原因,您的意图的附加功能不存在。

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

Bundle gotBasket = getIntent().getExtras();
if(gotBasket!=null){
    gotBread = gotBasket.getString("key");
    question.setText(gotBread);
}
于 2012-10-29T00:27:18.873 回答