2013-03-30 2 views
0

아래의 로그를 확인하고 난 다시 보낼 AddContactActivity에서 제공된 코드 가능한 변화를 제안 - 연락처 이름을 어떠한 의도를 통해 setresult를 사용하지 않고 (inent, 1)방법이 나갈 : nullpointer 예외

그러나 그것은 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 

layout_main :

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

layout_contacts :

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

layout_add_contacts :

<?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(); 
      } 
     } 

    } 

안드로이드 그것이 의도 수신기()는 예외로하여 onActivityResult 던져 수단

<?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> 

답변

0

를 사용해야 당신은 당신의 배열 목록 (편곡) 나는이 재판을

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); 
     } 
    } 
+0

문제가 해결되었습니다. 많은 감사 –

0

명단. 데이터가 null이라고 생각합니다. NULL 체크를

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); 
} 
+0

를 초기화해야합니다. 그러나 여전히 오류는 지속됩니다. –

+0

다른 해결책 –