将数据从一个活动发送到另一个活动 - 不工作

时间:2014-02-18 18:44:41

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

我有一个待办事项清单,当我选中一个活动中的复选框时,我希望CheckBox和复选框旁边的文本转到另一个活动。这是我的适配器:

public class MyItemAdapter extends ArrayAdapter<String>  {

private final Context context;
private final String[] values;

    public MyItemAdapter(Context context, String[] values) {
    super(context, R.layout.item_list, values);
    this.context = context;
    this.values = values;
}

@Override
  public View getView(int position, View convertView, ViewGroup parent) {
    LayoutInflater inflater = (LayoutInflater) context
        .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    View rowView = inflater.inflate(R.layout.item_list, parent, false);
    CheckBox textView = (CheckBox) rowView.findViewById(R.id.checkBox1);
    textView.setText(values[position]);

    return rowView;
  }

public static SparseBooleanArray getCheckedItemPositions() {
    // TODO Auto-generated method stub
    return null;
}

public static void remove(Object pos) {
    // TODO Auto-generated method stub

}

public void changeData(String[] items) {
    // TODO Auto-generated method stub

}

public static void setAdapter(MyItemAdapter mAdapter) {
    // TODO Auto-generated method stub

}

这是我的第一个活动代码:

         final CheckBox check = (CheckBox) findViewById(R.id.checkBox1);
             check.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            Intent intent = new Intent(MainActivity.this, FinishedItems.class);
            intent.putExtra("check", items);
            Bundle extras = new Bundle();
            extras.putString("status", "Data Recived");
            intent.putExtras(extras);
            startActivity(intent);

        }
    });
    }

这是我的第二个活动代码:

    Intent intent = getIntent();
    String check = intent.getStringExtra("check");
    ((CheckBox)findViewById(R.id.checkBox1)).setText(check);
    Bundle bundle = intent.getExtras();
    String status = bundle.getString("status");
    Toast toast = Toast.makeText(this, status, Toast.LENGTH_LONG);
    toast.show();

最后这是我的Log cat:

 02-18 10:33:24.779: D/AndroidRuntime(1575): Shutting down VM
 02-18 10:33:24.779: W/dalvikvm(1575): threadid=1: thread exiting with uncaught exception       (group=0x40015560)
 02-18 10:33:24.789: E/AndroidRuntime(1575): FATAL EXCEPTION: main
 02-18 10:33:24.789: E/AndroidRuntime(1575): java.lang.RuntimeException: Unable to start activity    ComponentInfo{com.Stanton.quicktodolist/com.Stanton.quicktodolist.MainActivity}:    java.lang.NullPointerException
 02-18 10:33:24.789: E/AndroidRuntime(1575):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1647)
 02-18 10:33:24.789: E/AndroidRuntime(1575):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
 02-18 10:33:24.789: E/AndroidRuntime(1575):    at android.app.ActivityThread.access$1500(ActivityThread.java:117)
 02-18 10:33:24.789: E/AndroidRuntime(1575):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
 02-18 10:33:24.789: E/AndroidRuntime(1575):    at android.os.Handler.dispatchMessage(Handler.java:99)
 02-18 10:33:24.789: E/AndroidRuntime(1575):    at android.os.Looper.loop(Looper.java:130)
 02-18 10:33:24.789: E/AndroidRuntime(1575):    at android.app.ActivityThread.main(ActivityThread.java:3683)
 02-18 10:33:24.789: E/AndroidRuntime(1575):    at java.lang.reflect.Method.invokeNative(Native Method)
 02-18 10:33:24.789: E/AndroidRuntime(1575):    at java.lang.reflect.Method.invoke(Method.java:507)
 02-18 10:33:24.789: E/AndroidRuntime(1575):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
 02-18 10:33:24.789: E/AndroidRuntime(1575):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
 02-18 10:33:24.789: E/AndroidRuntime(1575):    at dalvik.system.NativeStart.main(Native Method)
 02-18 10:33:24.789: E/AndroidRuntime(1575): Caused by: java.lang.NullPointerException
 02-18 10:33:24.789: E/AndroidRuntime(1575):    at com.Stanton.quicktodolist.MainActivity.onCreate(MainActivity.java:70)
 02-18 10:33:24.789: E/AndroidRuntime(1575):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
 02-18 10:33:24.789: E/AndroidRuntime(1575):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1611)
 02-18 10:33:24.789: E/AndroidRuntime(1575):    ... 11 more

请帮帮我

2 个答案:

答案 0 :(得分:0)

您的stacktrace的这一行

02-18 10:33:24.789: E/AndroidRuntime(1575):    at com.Stanton.quicktodolist.MainActivity.onCreate(MainActivity.java:70)

指出错误。 MainActivity中的第70行导致空指针。学习和理解堆栈跟踪通常很有帮助。如果您无法自己弄明白,请向我们展示MainActivity的onCreate方法。

答案 1 :(得分:0)

我相信你用额外的替换意图,只需尝试这个代码,让我知道pelase

final CheckBox check = (CheckBox) findViewById(R.id.checkBox1);
             check.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            Intent intent = new Intent(MainActivity.this, FinishedItems.class);
            intent.putExtra("check", items);
            //Bundle extras = new Bundle();
           // extras.putString("status", "Data Recived");
            intent.putExtras("status", "Data Recived");
            startActivity(intent);

        }
    });

    }

并且第二个活动是

 Intent intent = getIntent();
    String check = intent.getStringExtra("check");
    ((CheckBox)findViewById(R.id.checkBox1)).setText(check);
   // Bundle bundle = intent.getExtras();
    String status = intent.getStringExtra("status");
    Toast toast = Toast.makeText(this, status, Toast.LENGTH_LONG);
    toast.show();