按钮单击强制关闭活动

时间:2014-08-14 15:46:42

标签: java android nullpointerexception forceclose

我的问题是当我点击bSA按钮时遇到错误并且活动关闭。     java.lang.runtimeexception无法启动活动组件信息...

这是我的代码 Data.java:

package com.example.myapp;

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

    public class Data extends Activity implements View.OnClickListener {

    Button start, startFor;
    EditText sendET;
    TextView gotAnswer;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.get);
        initialize();
    }

    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);

    }

    @Override
    public void onClick(View view) {
        switch (view.getId()){
            case R.id.bSA:
                String bread = sendET.getText().toString();
                Bundle basket = new Bundle();
                basket.putString("key", bread);
                Intent a = new Intent(getApplicationContext(), OpenedClass.class);
                a.putExtras(a);
                startActivity(a);
                break;
            case R.id.bSAFR:

                break;
        }

    }
}

get.xml:

<?xml version="1.0" encoding="utf-8"?>

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
          android:layout_width="match_parent"
          android:layout_height="match_parent">

<EditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content"

        android:id="@+id/etSend" android:layout_gravity="center_horizontal"/>
<Button
        android:layout_below="@+id/etSend"
        android:layout_alignParentRight="true"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Start Activity"
        android:id="@+id/bSA"/>
<Button
        android:layout_toLeftOf="@+id/bSA"
        android:layout_alignTop="@+id/bSA"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Start Activity for Results"
        android:id="@+id/bSAFR"/>
<TextView
        android:layout_below="@+id/bSAFR"

        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="TextView"
        android:id="@+id/tvGot"/>
</RelativeLayout>

=============================================== ==============

OpenedClass.java

package com.example.myapp;

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


public class OpenedClass extends Activity implements View.OnClickListener,         RadioGroup.OnCheckedChangeListener {

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


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.send);
    initialize();
    Bundle gotBasket = getIntent().getExtras();
    gotBread = gotBasket.getString("key");
    question.setText(gotBread);


}

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

@Override
public void onClick(View view) {

}

@Override
public void onCheckedChanged(RadioGroup arg0, int arg1) {
    // TODO Auto-generated method stub
    switch (arg1) {
    case R.id.rCrazy:

        break;
     case R.id.rFun:

         break;
     case R.id.rBoth:

         break;


    }

}

}


send.xml

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
          android:orientation="vertical"
          android:layout_width="match_parent"
          android:layout_height="match_parent">

<TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceSmall"
        android:text="Hosein is ..."
        android:id="@+id/tvQuestion"/>
<RadioGroup
        android:id="@+id/rgAnswers"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content">


<RadioButton
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Crazy"
        android:id="@+id/rCrazy"
        />
<RadioButton
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Super Fun"
        android:id="@+id/rFun"/>
<RadioButton
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Both"
        android:id="@+id/rBoth"/>
</RadioGroup>
<Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Return"
        android:id="@+id/bReturn"/>

<TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="New Text"
        android:id="@+id/tvText"/>
</LinearLayout>

1 个答案:

答案 0 :(得分:1)

Intent a = new Intent(getApplicationContext(), OpenedClass.class);
a.putExtras(a);

你不能把a放在里面。这会导致无限递归和堆栈溢出。

你可能想要

a.putExtras(basket);