调用新Intent时,Android Activity强制关闭

时间:2012-10-17 05:27:33

标签: android android-intent sdk

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

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

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

5 个答案:

答案 0 :(得分:2)

如果我正确计算了行数(到OpenedClass.java的第38行),NPE就会在这一行生成:

returnData.setOnClickListener(this);

OpenedClass.initialize()内。变量returnData正在通过以下几行初始化:

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

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

答案 1 :(得分: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;
    }
   });

答案 2 :(得分:0)

您已使用此行

a.putExtras(basket);

在第一堂课中并未在OpenedClass

中收到该意图

也许这就是问题

答案 3 :(得分:0)

看起来您的某个观点未找到。

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

答案 4 :(得分: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;