问题是 从五个复选框选择中选择一个选项,并在另一个页面中显示所选选项的结果。
我在应用中有2项活动。
所有编码都是在java文件上完成的,不需要在xml文件中添加任何代码。
MainActivity.java
import java.util.ArrayList;
import java.util.List;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.*;
import android.view.View.*;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.LinearLayout;
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//Q3
LinearLayout ll3 = new LinearLayout(this);
ll3.setOrientation(LinearLayout.VERTICAL);
final List <CheckBox> cbs = new ArrayList<CheckBox>();
for(int i = 0; i < 5; i++){
CheckBox cb = new CheckBox(this);
cb.setText("Apple" + (i + 1));
ll3.addView(cbs.get(i));
}
Button button3 = new Button(this);
button3.setText("Select");
button3.setOnClickListener(new OnClickListener(){
public void onClick(View v) {
StringBuffer str = new StringBuffer();
for(int i = 0; i < cbs.size(); i++){
if(cbs.get(i).isChecked()){
str.append(cbs.get(i).getText().toString() + "; ");
}
}
Intent i = new Intent(MainActivity.this,MainActivity2.class);
i.putExtra("checked", str.toString());
startActivity(i);
}
});
ll3.addView(button3);
setContentView(ll3);
}
}
另一个活动文件名为MainActivity2.java
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.widget.LinearLayout;
import android.widget.TextView;
public class MainActivity2 extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main_activity2);
//Q3
TextView tv = new TextView(this);
tv.setText("You have selected:");
TextView tv1 = new TextView(this);
tv1.setText(this.getIntent().getStringExtra("checked"));
LinearLayout ll3 = new LinearLayout(this);
ll3.setOrientation(LinearLayout.VERTICAL);
ll3.addView(tv);
ll3.addView(tv1);
setContentView(ll3);
}
}
当我运行应用程序时,它会显示无法运行的错误消息状态。有什么问题吗?
Logcat文件
02-20 08:09:07.274: D/AndroidRuntime(376): Shutting down VM
02-20 08:09:07.274: W/dalvikvm(376): threadid=1: thread exiting with uncaught exception (group=0x40015560)
02-20 08:09:07.303: E/AndroidRuntime(376): FATAL EXCEPTION: main
02-20 08:09:07.303: E/AndroidRuntime(376): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.practical033/com.practical033.MainActivity}: java.lang.IndexOutOfBoundsException: Invalid index 0, size is 0
02-20 08:09:07.303: E/AndroidRuntime(376): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1647)
02-20 08:09:07.303: E/AndroidRuntime(376): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
02-20 08:09:07.303: E/AndroidRuntime(376): at android.app.ActivityThread.access$1500(ActivityThread.java:117)
02-20 08:09:07.303: E/AndroidRuntime(376): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
02-20 08:09:07.303: E/AndroidRuntime(376): at android.os.Handler.dispatchMessage(Handler.java:99)
02-20 08:09:07.303: E/AndroidRuntime(376): at android.os.Looper.loop(Looper.java:123)
02-20 08:09:07.303: E/AndroidRuntime(376): at android.app.ActivityThread.main(ActivityThread.java:3683)
02-20 08:09:07.303: E/AndroidRuntime(376): at java.lang.reflect.Method.invokeNative(Native Method)
02-20 08:09:07.303: E/AndroidRuntime(376): at java.lang.reflect.Method.invoke(Method.java:507)
02-20 08:09:07.303: E/AndroidRuntime(376): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
02-20 08:09:07.303: E/AndroidRuntime(376): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
02-20 08:09:07.303: E/AndroidRuntime(376): at dalvik.system.NativeStart.main(Native Method)
02-20 08:09:07.303: E/AndroidRuntime(376): Caused by: java.lang.IndexOutOfBoundsException: Invalid index 0, size is 0
02-20 08:09:07.303: E/AndroidRuntime(376): at java.util.ArrayList.throwIndexOutOfBoundsException(ArrayList.java:257)
02-20 08:09:07.303: E/AndroidRuntime(376): at java.util.ArrayList.get(ArrayList.java:311)
02-20 08:09:07.303: E/AndroidRuntime(376): at com.practical033.MainActivity.onCreate(MainActivity.java:32)
02-20 08:09:07.303: E/AndroidRuntime(376): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
02-20 08:09:07.303: E/AndroidRuntime(376): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1611)
02-20 08:09:07.303: E/AndroidRuntime(376): ... 11 more
02-20 08:09:09.733: I/Process(376): Sending signal. PID: 376 SIG: 9
答案 0 :(得分:1)
你可以用两种方式 - 使用静态变量并为该变量提供复选框值并将其访问到另一个活动 - 使用intent从第1个活动传递数据传递值并在其他活动中接收 额外的样品如下 活动第1期
Intent i = new Intent(FirstScreen.this, SecondScreen.class);
String keyIdentifer = null;
i.putExtra("STRING_I_NEED", strName);
活动第二
String newString;
if (savedInstanceState == null) {
extras = getIntent().getExtras();
if(extras == null) {
newString= null;
} else {
newString= extras.getString("STRING_I_NEED");
}
} else {
newString= (String) savedInstanceState.getSerializable("STRING_I_NEED");
}
以任何一种方式使用它
答案 1 :(得分:1)
您尚未将复选框添加到cbs列表,而您正尝试访问该复选框...
尝试在for循环中使用cbs.add(cb);
添加要列出的复选框...
试试这个
final List <CheckBox> cbs = new ArrayList<CheckBox>();
for(int i = 0; i < 5; i++){
CheckBox cb = new CheckBox(this);
cb.setText("Apple" + (i + 1));
cbs.add(cb);
ll3.addView(cbs.get(i));
}