将复选框数据传递给下一个活动 [英] Pass checkbox data to next activity

查看:20
本文介绍了将复选框数据传递给下一个活动的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在创建一个带有 listviewcheckbox 的项目列表.我想将选中的选项传递给下一个 activity.

I am creating a list of items with a listview with checkbox. I want to pass the checked options to next activity.

为此,我使用字符串数组将结果传递给其他活动.一切正常,但是当我单击按钮将数据传递给下一个活动时,应用程序因 NullPointerExceptionlogcat 错误而停止.这可能是一个非常简单的概念,但我并不了解它,因为我是 android 的新手.

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 文件..

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

    }
}

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

领取

 ArrayList<String> getChecked;   
 Bundle extras = getIntent().getExtras();
    if(extras!=null)
    {
      getChecked = extras.getStringArrayList("list");

   }

这篇关于将复选框数据传递给下一个活动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆