gpt4 book ai didi

java - 将单选按钮和复选框值传递给另一个 Activity

转载 作者:行者123 更新时间:2023-12-01 11:57:46 25 4
gpt4 key购买 nike

我是安卓新手。这里尝试将单选按钮和复选框值从一个 Activity 传递到另一个 Activity 。它没有显示任何错误,但当我运行时它只是关闭。任何帮助将不胜感激。这里我发布了完整的代码..

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

}

第二个 Activity

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 list

<?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>

第二个.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>

日志猫

    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)

最佳答案

从 list 中声明的​​第二个 Activity 中,删除标签

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

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

Activity Action: Start as a main entry point, does not expect to receive data.

编辑:也动起来

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

在 onClick 中启动第二个 Activity

关于java - 将单选按钮和复选框值传递给另一个 Activity ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28299132/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com