传递复选框的数据下一个活动 [英] Pass checkbox data to next activity
问题描述
我创建一个列表视图
项的列表复选框
。我想通过检查选项,下一个活动
。
有关此我使用的是字符串数组作为结果传递给其他活动
。一切都很好,但我点击按钮来传递数据给下一个活动,申请停止用 NullPointerException异常$ C $的
的logcat
错误C>。这可能是非常简单的概念,但我不知道它,因为我是一个新手到Android。
任何帮助将是AP preciated ..
Java文件。
包com.example.travelplanner;
进口的java.util.ArrayList;
进口android.os.Bundle;
进口android.app.Activity;
进口android.content.Intent;
进口android.util.Log;
进口android.util.SparseBooleanArray;
进口android.view.Menu;
进口android.view.View;
进口android.view.View.OnClickListener;
进口android.widget.AdapterView;
进口android.widget.ArrayAdapter;
进口android.widget.Button;
进口android.widget.ListView;
进口android.widget.Toast;
进口android.widget.AdapterView.OnItemClickListener;
公共类TailoredtwoActivity扩展活动实现OnItemClickListener,OnClickListener {
的String []建设者;
按钮BTN1;
ListView控件mListView;
String []数组=新的String [] {火腿,土耳其,面包};
@覆盖
公共无效的onCreate(包savedInstanceState)
{
super.onCreate(savedInstanceState);
的setContentView(R.layout.activity_tailoredtwo);
ArrayAdapter<字符串>适配器=新的ArrayAdapter<字符串>(这一点,android.R.layout.simple_list_item_checked,阵列);
mListView =(ListView控件)findViewById(R.id.listViewcity);
mListView.setAdapter(适配器);
mListView.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
Button按钮=(按钮)findViewById(R.id.btn_tailortwo_submit);
button.setOnClickListener(本);
}
公共无效的onClick(视图查看){
SparseBooleanArray位置= mListView.getCheckedItemPositions();
对于(INT指数= 0;指数< array.length;指数++){
如果(positions.get(指数)==真)
{
建设者[指数] =数组[索引]
}
}
意图I1 =新的意图(这一点,TailoredthreeActivity.class);
叠B =新包();
b.putStringArray(城,建造者);
i1.putExtras(B);
startActivity(I1);
}
@覆盖
公共无效onItemClick(适配器视图<>为arg0,查看ARG1,INT ARG2,长ARG3){
// TODO自动生成方法存根
}
}
这是logcat的:
一十月7号至12日:48:49.686:E / AndroidRuntime(13047):致命异常:主要
十月七号至12号:48:49.686:E / AndroidRuntime(13047):java.lang.RuntimeException的:无法启动的活动ComponentInfo {com.example.travelplanner / com.example.travelplanner.TailoredthreeActivity}:显示java.lang.NullPointerException
十月七号至12号:48:49.686:E / AndroidRuntime(13047):在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1967)
十月七号至12号:48:49.686:E / AndroidRuntime(13047):在android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1992)
十月七号至12号:48:49.686:E / AndroidRuntime(13047):在android.app.ActivityThread.access $ 600(ActivityThread.java:127)
十月七号至12号:48:49.686:E / AndroidRuntime(13047):在android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1158)
十月七号至12号:48:49.686:E / AndroidRuntime(13047):在android.os.Handler.dispatchMessage(Handler.java:99)
十月七号至12号:48:49.686:E / AndroidRuntime(13047):在android.os.Looper.loop(Looper.java:137)
十月七号至12号:48:49.686:E / AndroidRuntime(13047):在android.app.ActivityThread.main(ActivityThread.java:4448)
十月七号至12号:48:49.686:E / AndroidRuntime(13047):在java.lang.reflect.Method.invokeNative(本机方法)
十月七号至12号:48:49.686:E / AndroidRuntime(13047):在java.lang.reflect.Method.invoke(Method.java:511)
十月七号至12号:48:49.686:E / AndroidRuntime(13047):在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:823)
十月七号至12号:48:49.686:E / AndroidRuntime(13047):在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:590)
十月七号至12号:48:49.686:E / AndroidRuntime(13047):在dalvik.system.NativeStart.main(本机方法)
十月七号至12号:48:49.686:E / AndroidRuntime(13047):由:显示java.lang.NullPointerException
十月七号至12号:48:49.686:E / AndroidRuntime(13047):在com.example.travelplanner.TailoredthreeActivity.onCreate(TailoredthreeActivity.java:59)
十月七号至12号:48:49.686:E / AndroidRuntime(13047):在android.app.Activity.performCreate(Activity.java:4465)
十月七号至12号:48:49.686:E / AndroidRuntime(13047):在android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049)
十月七号至12号:48:49.686:E / AndroidRuntime(13047):在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1931)
十月七号至12号:48:49.686:E / AndroidRuntime(13047):11 ...更多
我会建议你使用一个数组列表中添加所选项目和数组列表传递给第二个活动
选中=新的ArrayList<字符串>();
公共无效的onClick(视图查看){
SparseBooleanArray位置= mListView.getCheckedItemPositions();
对于(INT指数= 0;指数< array.length;指数++){
如果(positions.get(指数)==真)
{
checked.add(数组[指数]);
}
}
意图I1 =新的意图(MainActivity.this,TailoredthreeActivity.class);
i1.putStringArrayListExtra(名单,选中);
startActivity(I1);
}
要接受
的ArrayList<字符串> getChecked;
捆绑额外= getIntent()getExtras()。
如果(临时演员!= NULL)
{
getChecked = extras.getStringArrayList(名单);
}
I am creating a list of items with a listview
with checkbox
. I want to pass the checked options to next activity
.
For this I am using an string array for passing the results to other activity
. Everything is going fine but as I click on button to pass the data to next activity, Application halts with a logcat
error of NullPointerException
. It maybe a very simple concept but I am not known to it as I am a newbie to android.
Any help would be appreciated..
Java File..
package com.example.travelplanner;
import java.util.ArrayList;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.util.Log;
import android.util.SparseBooleanArray;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.Toast;
import android.widget.AdapterView.OnItemClickListener;
public class TailoredtwoActivity extends Activity implements OnItemClickListener, OnClickListener{
String[] builder;
Button btn1;
ListView mListView;
String[] array = new String[] {"Ham", "Turkey", "Bread"};
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_tailoredtwo);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_checked, array);
mListView = (ListView) findViewById(R.id.listViewcity);
mListView.setAdapter(adapter);
mListView.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
Button button = (Button) findViewById(R.id.btn_tailortwo_submit);
button.setOnClickListener(this);
}
public void onClick(View view) {
SparseBooleanArray positions = mListView.getCheckedItemPositions();
for(int index = 0; index < array.length; index++) {
if(positions.get(index)==true)
{
builder[index] = array[index];
}
}
Intent i1 = new Intent(this, TailoredthreeActivity.class);
Bundle b = new Bundle();
b.putStringArray("city", builder);
i1.putExtras(b);
startActivity(i1);
}
@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
// TODO Auto-generated method stub
}
}
This is logcat:
07-12 10:48:49.686: E/AndroidRuntime(13047): FATAL EXCEPTION: main
07-12 10:48:49.686: E/AndroidRuntime(13047): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.travelplanner/com.example.travelplanner.TailoredthreeActivity}: java.lang.NullPointerException
07-12 10:48:49.686: E/AndroidRuntime(13047): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1967)
07-12 10:48:49.686: E/AndroidRuntime(13047): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1992)
07-12 10:48:49.686: E/AndroidRuntime(13047): at android.app.ActivityThread.access$600(ActivityThread.java:127)
07-12 10:48:49.686: E/AndroidRuntime(13047): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1158)
07-12 10:48:49.686: E/AndroidRuntime(13047): at android.os.Handler.dispatchMessage(Handler.java:99)
07-12 10:48:49.686: E/AndroidRuntime(13047): at android.os.Looper.loop(Looper.java:137)
07-12 10:48:49.686: E/AndroidRuntime(13047): at android.app.ActivityThread.main(ActivityThread.java:4448)
07-12 10:48:49.686: E/AndroidRuntime(13047): at java.lang.reflect.Method.invokeNative(Native Method)
07-12 10:48:49.686: E/AndroidRuntime(13047): at java.lang.reflect.Method.invoke(Method.java:511)
07-12 10:48:49.686: E/AndroidRuntime(13047): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:823)
07-12 10:48:49.686: E/AndroidRuntime(13047): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:590)
07-12 10:48:49.686: E/AndroidRuntime(13047): at dalvik.system.NativeStart.main(Native Method)
07-12 10:48:49.686: E/AndroidRuntime(13047): Caused by: java.lang.NullPointerException
07-12 10:48:49.686: E/AndroidRuntime(13047): at com.example.travelplanner.TailoredthreeActivity.onCreate(TailoredthreeActivity.java:59)
07-12 10:48:49.686: E/AndroidRuntime(13047): at android.app.Activity.performCreate(Activity.java:4465)
07-12 10:48:49.686: E/AndroidRuntime(13047): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049)
07-12 10:48:49.686: E/AndroidRuntime(13047): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1931)
07-12 10:48:49.686: E/AndroidRuntime(13047): ... 11 more
I will suggest you to use a arraylist to add items selected and pass the arraylist to the second activity
checked = new ArrayList<String>();
public void onClick(View view) {
SparseBooleanArray positions = mListView.getCheckedItemPositions();
for(int index = 0; index < array.length; index++) {
if(positions.get(index)==true)
{
checked.add(array[index]);
}
}
Intent i1 = new Intent(MainActivity.this, TailoredthreeActivity.class);
i1.putStringArrayListExtra("list",checked);
startActivity(i1);
}
To receive
ArrayList<String> getChecked;
Bundle extras = getIntent().getExtras();
if(extras!=null)
{
getChecked = extras.getStringArrayList("list");
}
这篇关于传递复选框的数据下一个活动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!