将Radiobutton和Checkbox值传递给另一个活动

时间:2015-02-03 12:35:52

标签: java android xml android-intent android-activity

我是android新手。这里我试图将radiobutton和Checkbox值从一个活动传递到另一个活动。它没有显示任何错误,但是当我运行时它只是关闭。任何帮助将不胜感激。我已经发布了完整的代码..

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.CheckBox;
import android.widget.RadioButton;
public class MainActivity extends Activity {


Intent obj = new Intent(this, SecondActivity.class);
Bundle extras = new Bundle();

RadioButton male, female, single, married;
CheckBox twelveth, bE, mE;
String s1="+2";
String s2="B.E.";
String s3="M.E.";
StringBuilder s;
String str;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    male = (RadioButton) findViewById(R.id.male);
    female = (RadioButton) findViewById(R.id.female);
    single = (RadioButton) findViewById(R.id.single);
    married = (RadioButton) findViewById(R.id.married);
    twelveth = (CheckBox) findViewById(R.id.twelveth);
    bE = (CheckBox) findViewById(R.id.BE);
    mE = (CheckBox) findViewById(R.id.ME);

    str=s.toString();
    extras.putString("Education",str);
}

public void onGenderClicked(View v) {
    boolean checked = ((RadioButton) v).isChecked();
    switch (v.getId()) {
        case R.id.male:
            if (checked)
                extras.putString("Gender", "Male");
            break;
        case R.id.female:
            if (checked)
                extras.putString("Gender", "Female");
            break;

    }
}

public void onStatusClicked(View v) {
    boolean checked = ((RadioButton) v).isChecked();
    switch (v.getId()) {
        case R.id.single:
            if (checked)
                extras.putString("Status", "Single");
            break;
        case R.id.married:
            if (checked)
                extras.putString("Status", "Married");
            break;

    }
}

public void ButtonClick(View v) {
    obj.putExtras(extras);
    startActivity(obj);
}

public void onCheckBoxClicked(View v)
{
    boolean checked=((CheckBox)v).isChecked();
    switch(v.getId())
    {
        case R.id.twelveth:
            if(checked)
            {
                s.append("\n"+s1);
            }
            break;
        case R.id.BE:
            if(checked)
            {
                s.append("\n"+s2);
            }
            break;
        case R.id.ME:
            if(checked)
            {
                s.append("\n"+s3);
            }
            break;
    }
    str=s.toString();
    extras.putString("Education",str);
}

}   

SecondActivity

package com.example.aravindpraveen.buttonsandboxes;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.widget.TextView;


public class SecondActivity extends Activity {
    TextView tvGender,tvStatus,tvEducation;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.second);
        tvGender=(TextView)findViewById(R.id.tvGender);
        tvStatus=(TextView)findViewById(R.id.tvStatus);
        tvEducation=(TextView)findViewById(R.id.tvEducation);
        Intent obj1=getIntent();
        Bundle extras=obj1.getExtras();
        String gender=extras.getString("Gender");
        String status=extras.getString("Starus");
        String education=extras.getString("Education");
        tvGender.setText(gender);
        tvStatus.setText(status);
        tvEducation.setText(education);
    }
}

Android Manifest

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.aravindpraveen.buttonsandboxes" >

<application
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >
    <activity
        android:name=".MainActivity"
        android:label="@string/app_name" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
    <activity
        android:name=".SecondActivity"
        android:label="@string/app_name" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.DEFAULT" />
        </intent-filter>
    </activity>
</application>

activity_main.xml中

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity"
android:background="#6457d962"
android:orientation="vertical">
<TextView android:text="@string/b_b"
    android:layout_width="match_parent"
    android:layout_height="50dp"
    android:textSize="40sp"
    android:background="#f62dd93c"
    android:textColor="#c34c27e6"
    android:gravity="center"/>
<Space
    android:layout_width="match_parent"
    android:layout_height="15dp" />
<TextView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="@string/gender"
    android:textSize="40sp"
    android:gravity="center"
    android:textColor="#000000"/>
<RadioGroup
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:id="@+id/rg_gender">
    <RadioButton
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/male"
        android:text="@string/male"
        android:textSize="30sp"
        android:checked="true"
        android:onClick="onGenderClicked"/>
    <RadioButton
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/female"
        android:text="@string/female"
        android:textSize="30sp"
        android:onClick="onGenderClicked"/>
    </RadioGroup>
<Space
    android:layout_width="match_parent"
    android:layout_height="15dp"
    />
<TextView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:textSize="40sp"
    android:text="@string/M_status"
    android:textColor="#000000"
    android:gravity="center"/>
<RadioGroup
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:id="@+id/rg_status">
    <RadioButton
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/single"
        android:text="@string/single"
        android:textSize="30sp"
        android:checked="true"
        android:onClick="onStatusClicked"/>
    <RadioButton
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/married"
        android:text="@string/married"
        android:textSize="30sp"
        android:onClick="onStatusClicked"/>
</RadioGroup>
<Space
    android:layout_width="match_parent"
    android:layout_height="15dp"
    />
<TextView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:textSize="40sp"
    android:text="@string/education"
    android:textColor="#000000"
    android:gravity="center"
    />
<CheckBox
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="@string/twelveth"
    android:textSize="30sp"
    android:id="@+id/twelveth"
    android:onClick="onCheckBoxClicked"/>
<CheckBox
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="@string/bE"
    android:textSize="30sp"
    android:id="@+id/BE"
    android:onClick="onCheckBoxClicked"/>
<CheckBox
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="@string/mE"
    android:textSize="30sp"
    android:id="@+id/ME"
    android:onClick="onCheckBoxClicked"/>
<Button
    android:layout_width="wrap_content"
    android:layout_height="50dp"
    android:id="@+id/submit"
    android:text="@string/submit"
    android:layout_gravity="center"
    android:onClick="ButtonClick"/>
</LinearLayout>

second.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
   android:layout_width="match_parent"
   android:layout_height="match_parent"
   android:background="#f4dbde"
    android:orientation="vertical">
<TextView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:textSize="40sp"
    android:background="#fba1a1"
    android:text="@string/heading"
    android:gravity="center"
    android:textColor="#ffffff"/>
<Space
    android:layout_width="match_parent"
    android:layout_height="20dp" />
<TextView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="@string/gender"
    android:textSize="40sp"
    android:gravity="center"
    android:textColor="#ffffff"
    android:background="#f7beae"/>
<TextView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:textSize="30sp"
    android:paddingTop="20dp"
    android:gravity="center"
    android:id="@+id/tvGender"/>
<Space
    android:layout_width="match_parent"
    android:layout_height="20dp" />
<TextView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="@string/M_status"
    android:textSize="40sp"
    android:gravity="center"
    android:textColor="#ffffff"
    android:background="#f7beae"/>
<TextView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:textSize="30sp"
    android:paddingTop="20dp"
    android:gravity="center"
    android:id="@+id/tvStatus"/>
<Space
    android:layout_width="match_parent"
    android:layout_height="20dp" />
<TextView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="@string/education"
    android:textSize="40sp"
    android:gravity="center"
    android:textColor="#ffffff"
    android:background="#f7beae"/>
<TextView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:textSize="30sp"
    android:paddingTop="20dp"
    android:gravity="center"
    android:id="@+id/tvEducation"/>
</LinearLayout>

logcat的

    02-03 11:39:42.859    1135-1135/com.example.aravindpraveen.buttonsandboxes D/AndroidRuntime﹕ Shutting down VM
02-03 11:39:42.863    1135-1135/com.example.aravindpraveen.buttonsandboxes W/dalvikvm﹕ threadid=1: thread exiting with uncaught exception (group=0xa4c2c648)
02-03 11:39:42.899    1135-1135/com.example.aravindpraveen.buttonsandboxes E/AndroidRuntime﹕ FATAL EXCEPTION: main
java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.example.aravindpraveen.buttonsandboxes/com.example.aravindpraveen.buttonsandboxes.MainActivity}: java.lang.NullPointerException
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2137)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2261)
        at android.app.ActivityThread.access$600(ActivityThread.java:141)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1256)
        at android.os.Handler.dispatchMessage(Handler.java:99)
        at android.os.Looper.loop(Looper.java:137)
        at android.app.ActivityThread.main(ActivityThread.java:5103)
        at java.lang.reflect.Method.invokeNative(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:525)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
        at dalvik.system.NativeStart.main(Native Method)
 Caused by: java.lang.NullPointerException
        at android.content.ContextWrapper.getPackageName(ContextWrapper.java:135)
        at android.content.ComponentName.<init>(ComponentName.java:75)
        at android.content.Intent.<init>(Intent.java:3662)
        at com.example.aravindpraveen.buttonsandboxes.MainActivity.<init>(MainActivity.java:15)
        at java.lang.Class.newInstanceImpl(Native Method)
        at java.lang.Class.newInstance(Class.java:1130)
        at android.app.Instrumentation.newActivity(Instrumentation.java:1061)
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2128)

at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2261)
at android.app.ActivityThread.access$600(ActivityThread.java:141)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1256)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:5103)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:525)
    at    com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
at dalvik.system.NativeStart.main(Native Method)

1 个答案:

答案 0 :(得分:1)

来自第二个活动在Manifest中声明,删除标签

<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>

我认为将主要活动作为MAIN而不是MAIN可能会导致此问题 也在documentaion

  

活动操作:作为主入口点开始,不期望接收数据。

修改 也动了

str=s.toString();
extras.putString("Education",str);

在onClick中启动第二个活动