在活动之间传输数据的问题该应用程序停止按下按钮

时间:2014-04-12 01:34:15

标签: android

我在屏幕之间传输数据时遇到问题。 我用过了意图。 下面是我的代码。

这是我父类中的代码:

    final EditText ed1= (EditText) findViewById(R.id.EditText05);       
    Button b = (Button) findViewById(R.id.submit);
    b.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {

            Intent i = new Intent(S1.this,S2.class);

            i.putExtra("ed1",ed1.getText().toString());

            startActivity(i);

        }
    });

这是我孩子班的代码:

    TextView tv = (TextView)findViewById(R.id.yo);

    tv.setText(getIntent().getExtras().getString("ed1"));

当我运行应用程序时,它停止工作并在logcat中显示以下内容:

    04-11 21:32:32.698: E/AndroidRuntime(923): FATAL EXCEPTION: main
    04-11 21:32:32.698: E/AndroidRuntime(923): Process: com.app.experiment, PID: 923
    04-11 21:32:32.698: E/AndroidRuntime(923): java.lang.NullPointerException
    04-11 21:32:32.698: E/AndroidRuntime(923):  at com.app.experiment.S1$1.onClick(S1.java:56)
    04-11 21:32:32.698: E/AndroidRuntime(923):  at android.view.View.performClick(View.java:4424)
    04-11 21:32:32.698: E/AndroidRuntime(923):  at android.view.View$PerformClick.run(View.java:18383)
    04-11 21:32:32.698: E/AndroidRuntime(923):  at android.os.Handler.handleCallback(Handler.java:733)
    04-11 21:32:32.698: E/AndroidRuntime(923):  at android.os.Handler.dispatchMessage(Handler.java:95)
    04-11 21:32:32.698: E/AndroidRuntime(923):  at android.os.Looper.loop(Looper.java:137)
    04-11 21:32:32.698: E/AndroidRuntime(923):  at android.app.ActivityThread.main(ActivityThread.java:4998)
    04-11 21:32:32.698: E/AndroidRuntime(923):  at java.lang.reflect.Method.invokeNative(Native Method)
    04-11 21:32:32.698: E/AndroidRuntime(923):  at java.lang.reflect.Method.invoke(Method.java:515)
    04-11 21:32:32.698: E/AndroidRuntime(923):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:777)
    04-11 21:32:32.698: E/AndroidRuntime(923):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:593)
    04-11 21:32:32.698: E/AndroidRuntime(923):  at dalvik.system.NativeStart.main(Native Method)

这是我的xml文件,我有按钮:

<RelativeLayout 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"

android:gravity="center_vertical"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >                     

<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:text="@string/Starters"
android:textAppearance="?android:attr/textAppearanceMedium" />

<ListView
    android:id="@+id/android:list"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:layout_alignParentLeft="true"
    android:layout_alignParentRight="true"
    android:layout_below="@+id/submit"
    android:visibility="invisible" />

<Button
    android:id="@+id/submit"
    style="?android:attr/buttonStyleSmall"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentRight="true"
    android:text="@string/Submit" />

  </RelativeLayout>

这是我有textview的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:orientation="vertical" >

<TextView
    android:id="@+id/yo"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="textview" />

</LinearLayout>

这是具有edittexts的xml:

   <?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:padding="10dp"
android:paddingLeft="10dp"
android:paddingRight="10dp" >

<TextView
    android:id="@+id/name"
    android:layout_width="128dp"
    android:layout_height="wrap_content"
    android:paddingBottom="2dip"
    android:paddingTop="6dip"
    android:textColor="#43bd00"
    android:textSize="16sp"
    android:textStyle="bold" />

<EditText
    android:id="@+id/EditText05"
    android:layout_width="50dp"
    android:layout_height="wrap_content"
    android:layout_alignParentRight="true"
    android:layout_alignTop="@+id/name"
    android:ems="10"
    android:inputType="number" />

<EditText
    android:id="@+id/EditText06"
    android:layout_width="100dp"
    android:layout_height="wrap_content"
    android:layout_alignParentRight="true"
    android:layout_below="@+id/EditText05"
    android:ems="10"
    android:inputType="text"/>

 </RelativeLayout>

这是我的S1.class:

 public class S1 extends ListActivity {
 private ProgressDialog pDialog;
 ListView lv;
 TextView starters,tx1;
 int key = 0;
 private static String url = "http://ashapurasoftech.com/train/test.json";
 private static final String TAG_a = "menu";
 private static final String TAG_Name = "Name";
 JSONArray items = null;
 ArrayList<HashMap<String, String>> itemList;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.table1);
    final EditText ed1= (EditText) findViewById(R.id.edit_text_06);
    Button b = (Button) findViewById(R.id.submit);
    b.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent i = new Intent(S1.this,S2.class);
            i.putExtra("ed1",ed1.getText().toString());
            startActivity(i);
        }
    });
    lv =getListView();
    starters = (TextView) findViewById(R.id.textView1);
    starters.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View arg0) {
            if (key==0){
                key=1;
                lv.setVisibility(View.VISIBLE);}
                else if(key==1) {
                    key=0;              
                    lv.setVisibility(View.GONE);
                }               
        }
    });
    itemList = new ArrayList<HashMap<String, String>>();
    lv.setOnItemClickListener(new OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view,int position, long id) {
        String name = ((TextView) view.findViewById(R.id.name)).getText().toString();
        Intent in = new Intent(getApplicationContext(),
        SingleContactActivity.class);
        in.putExtra(TAG_Name, name);
        startActivity(in);
        }
    });
new Getitems().execute();
       }
private class Getitems extends AsyncTask<Void, Void, Void> {
    @Override
    protected void onPreExecute() {
        super.onPreExecute();           
        pDialog = new ProgressDialog(S1.this);
        pDialog.setMessage("Loading...");
        pDialog.setCancelable(true);
        pDialog.show();
    }
    @Override
    protected Void doInBackground(Void... arg0) {ServiceHandler sh = new ServiceHandler();
        String jsonStr = sh.makeServiceCall(url, ServiceHandler.GET);
        if (jsonStr != null) {
            try {
                JSONObject jsonObj = new JSONObject(jsonStr);
                items = jsonObj.getJSONArray(TAG_a);
                                 for (int i = 0; i < items.length(); i++) {
                    JSONObject c = items.getJSONObject(i);
            HashMap<String, String> item = new HashMap<String, String>();
                String name = c.getString(TAG_Name);
                    item.put(TAG_Name, name);
                    itemList.add(item);         
                }}
                catch (JSONException e) {
                e.printStackTrace();}}
        else {Log.e("ServiceHandler", "Couldn't get any data from the url");}
        return null;    }
        @Override
                protected void onPostExecute(Void result) {
        super.onPostExecute(result);
        if (pDialog.isShowing())
            pDialog.dismiss();
        ListAdapter adapter = new SimpleAdapter(
        S1.this, itemList,
        R.layout.textview, new String[] {TAG_Name},new int[] { R.id.name});
        setListAdapter(adapter);            
        }}}

这是我的S2.class:

        public class S2 extends Activity{
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.yo);
                    TextView tv = (TextView)findViewById(R.id.edit_text_06);
    tv.setText(getIntent().getExtras().getString("ed1"));
        } }

这是我的SingleContactActivity.class:

       public class SingleContactActivity  extends Activity {

private static final String TAG_Name = "Name";
@Override
       public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.textview);
    Intent in = getIntent();
    String name = in.getStringExtra(TAG_Name);
    TextView lblName = (TextView) findViewById(R.id.name);
    lblName.setText(name);
    }}

3 个答案:

答案 0 :(得分:0)

我相信EditText.getText()可以返回null。

答案 1 :(得分:0)

专门针对Android和Java,因为大多数错误会导致程序崩溃,学会测试所有内容,并记录意外情况......

尝试使用onclick侦听器:

if(ed1 != null){
  CharSequence charSequence = ed1.getText();
  if(text != null){
    text = charSequence.toString();
    Intent i = new Intent(S1.this,S2.class);
    i.putExtra("ed1",text);
    startActivity(i);
  } else {
    Log.d("DEBUG", "text is null");
  }
} else {
  Log.d("DEBUG", "ed1 is null");
}

另一个块看起来像这样:

TextView tv = (TextView)findViewById(R.id.yo);
if(tv != null){
  Bundle extras = getIntent().getExtras();
  if(extras != null){
    String text = extras.getString("ed1");
    if(text != null){
      tv.setText(text);
    } else {
      Log.d("DEBUG", "text is null");
    }
  } else {
    Log.d("DEBUG", "extras is null")
  }
} else {
  Log.d("DEBUG", "tv is null");
}

答案 2 :(得分:0)

我认为您无法将EditText声明为final,因为这意味着它不会记住新文本,并且它始终指向null的初始化值,因此您具有nullpointer异常。尝试清除ed1的声明中的final,现在让我们。