0

检查以下日志并建议可能更改提供的代码在 AddContactActivity 我想发回 - 联系人姓名而不是使用 setresult(inent , 1) 的意图

但它显示 NullPointerException(检查错误日志

错误日志显示:

    03-30 10:16:17.069: E/AndroidRuntime(337): FATAL EXCEPTION: main
    03-30 10:16:17.069: E/AndroidRuntime(337): java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=1, result=1, data=Intent { (has extras) }} to activity {com.main.listintent/com.main.listintent.ContactsActivity}: java.lang.NullPointerException
    03-30 10:16:17.069: E/AndroidRuntime(337):  at android.app.ActivityThread.deliverResults(ActivityThread.java:2532)
    03-30 10:16:17.069: E/AndroidRuntime(337):  at android.app.ActivityThread.handleSendResult(ActivityThread.java:2574)
    03-30 10:16:17.069: E/AndroidRuntime(337):  at android.app.ActivityThread.access$2000(ActivityThread.java:117)
    03-30 10:16:17.069: E/AndroidRuntime(337):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:961)
    03-30 10:16:17.069: E/AndroidRuntime(337):  at android.os.Handler.dispatchMessage(Handler.java:99)
    03-30 10:16:17.069: E/AndroidRuntime(337):  at android.os.Looper.loop(Looper.java:123)
    03-30 10:16:17.069: E/AndroidRuntime(337):  at android.app.ActivityThread.main(ActivityThread.java:3683)
    03-30 10:16:17.069: E/AndroidRuntime(337):  at java.lang.reflect.Method.invokeNative(Native Method)
    03-30 10:16:17.069: E/AndroidRuntime(337):  at java.lang.reflect.Method.invoke(Method.java:507)
    03-30 10:16:17.069: E/AndroidRuntime(337):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
    03-30 10:16:17.069: E/AndroidRuntime(337):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
    03-30 10:16:17.069: E/AndroidRuntime(337):  at dalvik.system.NativeStart.main(Native Method)
    03-30 10:16:17.069: E/AndroidRuntime(337): Caused by: java.lang.NullPointerException
    03-30 10:16:17.069: E/AndroidRuntime(337):  at com.main.listintent.ContactsActivity.onActivityResult(ContactsActivity.java:43)
    03-30 10:16:17.069: E/AndroidRuntime(337):  at android.app.Activity.dispatchActivityResult(Activity.java:3908)
    03-30 10:16:17.069: E/AndroidRuntime(337):  at android.app.ActivityThread.deliverResults(ActivityThread.java:2528)
    03-30 10:16:17.069: E/AndroidRuntime(337):  ... 11 more

布局主:

    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="fill_parent"
        android:layout_height="match_parent"
        tools:context=".MainActivity" >

    </RelativeLayout>

布局联系人:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="fill_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity" >

    <LinearLayout
        android:id="@+id/contacts_title_linear_layout"
        android:layout_width="fill_parent"
        android:layout_height="45dp"
        android:background="@android:color/darker_gray" >

        <TextView
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:layout_weight="1"
            android:paddingLeft="10dp"
            android:text="@string/contacts_label"
            android:textColor="@android:color/black"
            android:textSize="20sp"
            android:textStyle="bold" />

        <Button
            android:id="@+id/add_contacts_cancel_button"
            style="?android:attr/buttonStyleSmall"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:text="@string/new_button"
            android:textStyle="bold" />

    </LinearLayout>

    <ListView
        android:id="@+id/contacts_list"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@id/contacts_title_linear_layout" >

    </ListView>

</RelativeLayout>

布局添加联系人:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/LinearLayout1"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@android:color/white"
    android:orientation="vertical" >

    <LinearLayout
        android:id="@+id/add_contacts_title_layout"
        android:layout_width="match_parent"
        android:layout_height="45dp"
        android:background="@android:color/darker_gray" >

        <TextView
            android:id="@+id/add_contacts_title_label"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:text="@string/add_contact_label"
            android:textColor="@android:color/black"
            android:textSize="20sp"
            android:textStyle="bold" />
    </LinearLayout>

    <LinearLayout
        android:id="@+id/add_contacts_details_layout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:orientation="vertical" >

        <TextView
            android:id="@+id/textView1"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:paddingLeft="10dp"
            android:paddingTop="10dp"
            android:text="@string/add_contacts_contact_name"
            android:textColor="@android:color/black"
            android:textSize="18sp" />

        <EditText
            android:id="@+id/add_contacts_name_text"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginLeft="10dp"
            android:layout_marginRight="10dp"
            android:ems="10"
            android:inputType="textPersonName" />

        <TextView
            android:id="@+id/textView2"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:paddingLeft="10dp"
            android:paddingTop="6dp"
            android:text="@string/add_contacts_no"
            android:textColor="@android:color/black"
            android:textSize="18sp" />

        <EditText
            android:id="@+id/add_contacts_no_text"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginLeft="10dp"
            android:layout_marginRight="10dp"
            android:ems="10"
            android:inputType="number" />

    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center" >

        <Button
            android:id="@+id/add_contacts_cancel_button"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/cancel_button"
            android:textSize="18sp"
            android:width="100dp" />

        <Button
            android:id="@+id/add_contacts_add_button"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/add_button"
            android:textSize="18sp"
            android:width="100dp" />

    </LinearLayout>

</LinearLayout> 

MainActivity.java

    package com.main.listintent;

    import android.os.Bundle;
    import android.app.Activity;
    import android.content.Intent;

    public class MainActivity extends Activity {

        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            Intent intentcontacts=new Intent(this, ContactsActivity.class);
            startActivity(intentcontacts);
        }
    }

ContactsActivity.java

    package com.main.listintent;

    import java.util.ArrayList;

    import android.app.Activity;
    import android.content.Intent;
    import android.os.Bundle;
    import android.view.View;
    import android.view.View.OnClickListener;
    import android.widget.ArrayAdapter;
    import android.widget.Button;
    import android.widget.ListView;

    public class ContactsActivity extends Activity implements OnClickListener{

        ArrayList<String> arr;
        Button btnnew;
        ListView lvcontacts;

        @Override
        protected void onCreate(Bundle savedInstanceState) {
            // TODO Auto-generated method stub
            super.onCreate(savedInstanceState);
            setContentView(R.layout.layout_contacts);
            btnnew=(Button)findViewById(R.id.add_contacts_cancel_button);
            lvcontacts=(ListView)findViewById(R.id.contacts_list);
            btnnew.setOnClickListener(this);
        }

        @Override
        public void onClick(View arg0) {
            // TODO Auto-generated method stub
            Intent intentaddcontact=new Intent(this, AddContactActivity.class);
            startActivityForResult(intentaddcontact, 1);
        }

        @Override
        protected void onActivityResult(int requestCode, int resultCode, Intent data) {
            // TODO Auto-generated method stub
            super.onActivityResult(requestCode, resultCode, data);
            if(requestCode==1 && resultCode==1){
                Bundle bundle=data.getExtras();
                arr.add(bundle.getString("contactname"));
                ArrayAdapter<String>adp=new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, arr);
                lvcontacts.setAdapter(adp);
            }
        }


    }

AddContactActivity.java:

    package com.main.listintent;

    import android.app.Activity;
    import android.content.Intent;
    import android.os.Bundle;
    import android.view.View;
    import android.view.View.OnClickListener;
    import android.widget.Button;
    import android.widget.EditText;

    public class AddContactActivity extends Activity implements OnClickListener{

        Button btncancel,btnadd;
        EditText txtcontactname,txtcontactno;

        @Override
        protected void onCreate(Bundle savedInstanceState) {
            // TODO Auto-generated method stub
            super.onCreate(savedInstanceState);
            setContentView(R.layout.layout_add_contacts);
            txtcontactname=(EditText)findViewById(R.id.add_contacts_name_text);
            txtcontactno=(EditText)findViewById(R.id.add_contacts_no_text);
            btnadd=(Button)findViewById(R.id.add_contacts_add_button);
            btncancel=(Button)findViewById(R.id.add_contacts_cancel_button);
            btnadd.setOnClickListener(this);
            btncancel.setOnClickListener(this);
        }

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            if(v==btnadd){
                Bundle bundle=new Bundle();
                bundle.putString("contactname", txtcontactname.getText().toString());
                bundle.putString("contactno", txtcontactno.getText().toString());
                Intent intentcontacts=new Intent();
                intentcontacts.putExtras(bundle);
                setResult(1, intentcontacts);
                finish();
            }
            else if(v==btncancel){
                setResult(0);
                finish();
            }
        }

    }

安卓清单

    <?xml version="1.0" encoding="utf-8"?>
    <manifest xmlns:android="http://schemas.android.com/apk/res/android"
        package="com.main.listintent"
        android:versionCode="1"
        android:versionName="1.0" >

        <uses-sdk
            android:minSdkVersion="10"
            android:targetSdkVersion="10" />

        <application
            android:allowBackup="true"
            android:icon="@drawable/ic_launcher"
            android:label="@string/app_name"
            android:theme="@android:style/Theme.NoTitleBar" >
            <activity
                android:name="com.main.listintent.MainActivity"
                android:label="@string/app_name" android:theme="@android:style/Theme.NoTitleBar">
                <intent-filter>
                    <action android:name="android.intent.action.MAIN" />

                    <category android:name="android.intent.category.LAUNCHER" />
                </intent-filter>
            </activity>
            <activity android:name="ContactsActivity">
            </activity>
            <activity android:name="AddContactActivity" android:windowSoftInputMode="adjustResize">
            </activity>
        </application>

    </manifest>
4

2 回答 2

0

这意味着 Intent 接收器在其 onActivityResult() 中抛出了异常。我认为数据为空。应该使用空检查

if (data!=null)
{
Bundle bundle=data.getExtras();
arr.add(bundle.getString("contactname"));
ArrayAdapter<String>adp=new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1, arr);
lvcontacts.setAdapter(adp);
}
于 2013-03-30T11:04:24.747 回答
0

你必须初始化你的数组列表(arr)

 protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        // TODO Auto-generated method stub
        super.onActivityResult(requestCode, resultCode, data);
        if(requestCode==1 && resultCode==1){
          arr=new ArrayList<String>();
            Bundle bundle=data.getExtras();

            arr.add(bundle.getString("contactname"));
            ArrayAdapter<String>adp=new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, arr);
            lvcontacts.setAdapter(adp);
        }
    }
于 2013-03-30T11:11:31.060 回答