0

该活动应该调用另一个活动,但它强制关闭应用程序。

package com.example.helloworld;

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);
        initialize();
    }

    private void initialize() {
        // TODO Auto-generated method stub
        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 v) {
        // TODO Auto-generated method stub
        switch (v.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);
    }

}

这是另一个要调用的 Activity:

package com.example.helloworld;

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, 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.tvQuestions);
        test = (TextView) findViewById(R.id.tvText);
        returnData = (Button) findViewById(R.id.bResults);
        selectionList = (RadioGroup) findViewById(R.id.rgAnswers);
        returnData.setOnClickListener(this);
        selectionList.setOnCheckedChangeListener(this);


    }

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

    }

    public void onCheckedChanged(RadioGroup arg0, int arg1) {
        // TODO Auto-generated method stub
        switch (arg1) {
        case R.id.rChoice1:
            setData="ABC";
            break;
        case R.id.rChoice2:
            setData="MNO";
            break;
        case R.id.rChoice3:
            setData="XYZ";
            break;
        }
        test.setText(setData);
    }

}

这是我的 AndroidMaifest.xml 文件:

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.helloworld"
    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>

            </intent-filter>
        </activity>
        <activity
            android:name=".Menu"
            android:label="@string/title_activity_main" >
            <intent-filter>
                <action android:name="com.example.helloworld.MENU" />

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

</manifest>

最后 LogCat 日志:

10-17 10:36:56.423: D/AndroidRuntime(272): Shutting down VM
10-17 10:36:56.423: W/dalvikvm(272): threadid=1: thread exiting with uncaught exception (group=0x4001d800)
10-17 10:36:56.453: E/AndroidRuntime(272): FATAL EXCEPTION: main
10-17 10:36:56.453: E/AndroidRuntime(272): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.helloworld/com.example.helloworld.OpenedClass}: java.lang.NullPointerException
10-17 10:36:56.453: E/AndroidRuntime(272):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663)
10-17 10:36:56.453: E/AndroidRuntime(272):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
10-17 10:36:56.453: E/AndroidRuntime(272):  at android.app.ActivityThread.access$2300(ActivityThread.java:125)
10-17 10:36:56.453: E/AndroidRuntime(272):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
10-17 10:36:56.453: E/AndroidRuntime(272):  at android.os.Handler.dispatchMessage(Handler.java:99)
10-17 10:36:56.453: E/AndroidRuntime(272):  at android.os.Looper.loop(Looper.java:123)
10-17 10:36:56.453: E/AndroidRuntime(272):  at android.app.ActivityThread.main(ActivityThread.java:4627)
10-17 10:36:56.453: E/AndroidRuntime(272):  at java.lang.reflect.Method.invokeNative(Native Method)
10-17 10:36:56.453: E/AndroidRuntime(272):  at java.lang.reflect.Method.invoke(Method.java:521)
10-17 10:36:56.453: E/AndroidRuntime(272):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
10-17 10:36:56.453: E/AndroidRuntime(272):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
10-17 10:36:56.453: E/AndroidRuntime(272):  at dalvik.system.NativeStart.main(Native Method)
10-17 10:36:56.453: E/AndroidRuntime(272): Caused by: java.lang.NullPointerException
10-17 10:36:56.453: E/AndroidRuntime(272):  at com.example.helloworld.OpenedClass.initialize(OpenedClass.java:38)
10-17 10:36:56.453: E/AndroidRuntime(272):  at com.example.helloworld.OpenedClass.onCreate(OpenedClass.java:25)
10-17 10:36:56.453: E/AndroidRuntime(272):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
10-17 10:36:56.453: E/AndroidRuntime(272):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)
10-17 10:36:56.453: E/AndroidRuntime(272):  ... 11 more
10-17 10:36:59.083: I/Process(272): Sending signal. PID: 272 SIG: 9

问题是应用程序编译没有错误,但当我尝试调用活动时崩溃。我不确定问题出在哪里。是清单吗?

4

5 回答 5

2

如果我正确地计算了行数(到 OpenedClass.java 的第 38 行),则在此行生成 NPE:

returnData.setOnClickListener(this);

里面OpenedClass.initialize()。该变量returnData正在通过上面的几行初始化:

returnData = (Button) findViewById(R.id.bResults);

问题很可能是您的布局 (send.xml) 没有bResults视图,因此returnData被分配了null

于 2012-10-17T05:34:29.223 回答
1

试试这个 :

    startFor.setOnClickListener(new OnClickListener()
      {
      @Override
      public void onClick(View v)
      {
        // TODO Auto-generated method stub
         switch (v.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;
    }
   });
于 2012-10-17T05:35:30.560 回答
0

你用过这条线

a.putExtras(basket);

在第一堂课中,并没有收到那个意图OpenedClass

也许这就是问题

于 2012-10-17T05:31:53.590 回答
0

似乎没有找到您的观点之一。

调试您的应用程序并确保它们都被找到并且不返回 null;

于 2012-10-17T05:33:55.990 回答
0

在活动中像这样改变

 case R.id.bSA:
        String bread = sendET.getText().toString();     
        Intent a = new Intent(Data.this, OpenedClass.class);
        a.putExtras("key", bread); 
        startActivity(a);
        break;
于 2012-10-17T05:34:04.680 回答