2013-07-18 1 views
1

연락처 목록을 만들려면이 자습서 [http://vimaltuts.com/android-tutorials/android-load-image-from-sqlite-to-listview][1]를 따라야합니다. sqlite db에서 추가, 편집 및 제거. 완벽하게 작동하지만 이제이 listActivity를 listfragment로 변경하려고합니다. 지금까지 나는 그것을 할 수 없었고, Null Pointer Exception (아래 참조)을 얻는다. 생각하는 조각주기에 문제가 있습니다. 도와 주실 수 있니?SQlite에서 ListFragment에 대한 ListActivity NullPointerException

원래 내 연락처 활동

import com.vimaltuts.mycontacts.R; 

import android.os.AsyncTask; 
import android.os.Bundle; 
import android.app.ListActivity; 
import android.content.Intent; 
import android.database.Cursor; 
import android.view.Menu; 
import android.view.MenuItem; 

public class MyContactsActivity extends ListActivity { 

    SQLiteConnector sqlCon; 
    private CustomContactsAdapter custAdapter; 

    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState);  
     String[] from = new String[] { Contacts.NAME, Contacts.PHONE,Contacts.MAIL }; 
     int[] to = new int[] { R.id.name, R.id.phone, R.id.email }; 
     custAdapter = new CustomContactsAdapter(this,R.layout.contact_list_item, null, from, to); 
     this.setListAdapter(custAdapter);  
    } 

    @Override 
    protected void onResume() { 
     super.onResume(); 
     new GetContacts().execute((Object[]) null); 
    } 

    @SuppressWarnings("deprecation") 
    @Override 
    protected void onStop() { 
     Cursor cursor = custAdapter.getCursor(); 

     if (cursor != null) 
      cursor.deactivate(); 

     custAdapter.changeCursor(null); 
     super.onStop(); 

    } 

    private class GetContacts extends AsyncTask<Object, Object, Cursor> { 
     SQLiteConnector dbConnector = new SQLiteConnector(MyContactsActivity.this); 

     @Override 
     protected Cursor doInBackground(Object... params) { 
      return dbConnector.getAllContacts(); 
     } 

     @Override 
     protected void onPostExecute(Cursor result) { 
      custAdapter.changeCursor(result); 
     } 
    } 

    @Override 
    public boolean onCreateOptionsMenu(Menu menu) { 
     getMenuInflater().inflate(R.menu.activity_my_contacts, menu); 
     return true; 
    } 

    @Override 
    public boolean onOptionsItemSelected(MenuItem item) { 
     Intent addContact = new Intent(MyContactsActivity.this,AddEditContacts.class); 
     startActivity(addContact); 
     return super.onOptionsItemSelected(item); 
    } 

} 

사용자에게 연락 어댑터

import android.content.Context; 
import android.content.Intent; 
import android.database.Cursor; 
import android.graphics.BitmapFactory; 
import android.support.v4.widget.SimpleCursorAdapter; 
import android.view.LayoutInflater; 
import android.view.View; 
import android.view.ViewGroup; 
import android.widget.ImageButton; 
import android.widget.ImageView; 
import android.widget.TextView; 

public class CustomContactsAdapter extends SimpleCursorAdapter { 

    private int layout; 
    private ImageButton editBtn; 
    private ImageButton delBtn; 
    LayoutInflater inflator; 

    public CustomContactsAdapter(Context context, int layout, Cursor c,String[] from, int[] to) { 
     super(context, layout, c, from, to,0); 
     this.layout = layout; 
     inflator= LayoutInflater.from(context); 
    } 

    public View newView(Context context, Cursor cursor, ViewGroup parent) {  
     View v = inflator.inflate(layout, parent, false); 
     return v; 
    } 

    @Override 
    public void bindView(View v, final Context context, Cursor c) { 
     final int id = c.getInt(c.getColumnIndex(Contacts.ID)); 
     final String name = c.getString(c.getColumnIndex(Contacts.NAME)); 
     final String phone = c.getString(c.getColumnIndex(Contacts.PHONE)); 
     final String email = c.getString(c.getColumnIndex(Contacts.MAIL)); 
     final String fb = c.getString(c.getColumnIndex(Contacts.FB)); 
     final byte[] image = c.getBlob(c.getColumnIndex(Contacts.IMAGE)); 
     ImageView iv = (ImageView) v.findViewById(R.id.photo); 

     if (image != null) { 
      if (image.length > 3) { 
       iv.setImageBitmap(BitmapFactory.decodeByteArray(image, 0,image.length)); 
      } 
     } 

     TextView tname = (TextView) v.findViewById(R.id.name); 
     tname.setText(name); 
     TextView tphone = (TextView) v.findViewById(R.id.phone); 
     tphone.setText(phone); 
     TextView temail = (TextView) v.findViewById(R.id.email); 
     temail.setText(email); 

     final SQLiteConnector sqlCon = new SQLiteConnector(context); 

     editBtn=(ImageButton) v.findViewById(R.id.edit_btn);  
     editBtn.setOnClickListener(new View.OnClickListener() { 

      @Override 
      public void onClick(View v) { 
       Intent intent=new Intent(context,AddEditContacts.class).setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);     
       intent.putExtra("id", id); 
       intent.putExtra("name", name); 
       intent.putExtra("phone", phone); 
       intent.putExtra("email", email); 
       intent.putExtra("fb", fb); 
       intent.putExtra("blob", image); 
       context.startActivity(intent); 
      } 

     }); 

     delBtn=(ImageButton) v.findViewById(R.id.del_btn); 
     delBtn.setOnClickListener(new View.OnClickListener() { 

      @Override 
      public void onClick(View v) { 
       sqlCon.deleteContact(id); 
       Intent intent=new Intent(context,ChildrenListFragment.class).setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 
       context.startActivity(intent); 
      } 

     });  

     v.setOnClickListener(new View.OnClickListener() { 

      @Override 
      public void onClick(View v) { 
       Intent intent=new Intent(context,ViewContact.class).setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);     
       intent.putExtra("id", id); 
       intent.putExtra("name", name); 
       intent.putExtra("phone", phone); 
       intent.putExtra("email", email); 
       intent.putExtra("fb", fb); 
       intent.putExtra("blob", image); 
       context.startActivity(intent); 
      } 
     }); 

    } 

} 

이 변경 내 연락처 활동

public class MyContactsActivity extends ListFragment{ 

    SQLiteConnector sqlCon; 
    private CustomContactsAdapter custAdapter; 


    @Override 
    public void onActivityCreated(Bundle savedInstanceState) { 
     super.onActivityCreated(savedInstanceState); 

     Activity activity = getActivity(); 

     if (activity != null) { 


      String[] from = new String[] { Contacts.NAME, Contacts.PHONE,Contacts.MAIL }; 
      int[] to = new int[] { R.id.name, R.id.phone, R.id.email }; 
      CustomContactsAdapter custAdapter = new CustomContactsAdapter(activity,R.layout.contact_list_item, null, from, to); 
      this.setListAdapter(custAdapter); 
      SQLiteConnector dbConnector = new SQLiteConnector(activity); 


     } 
    } 

    @Override 
    public void onResume() { 
     super.onResume(); 
     new GetContacts().execute((Object[]) null); 
    } 

    @SuppressWarnings("deprecation") 
    @Override 
    public void onStop() { 
     Cursor cursor = custAdapter.getCursor(); 

     if (cursor != null) 
      cursor.deactivate(); 

     custAdapter.changeCursor(null); 
     super.onStop(); 

    } 

    private class GetContacts extends AsyncTask<Object, Object, Cursor> { 
     Activity activity = getActivity(); 
     SQLiteConnector dbConnector = new SQLiteConnector(activity); 

     @Override 
     protected Cursor doInBackground(Object... params) { 
      return dbConnector.getAllContacts(); 
     } 

     @Override 
     protected void onPostExecute(Cursor result) { 
      if (isDestroyed) return; 
      custAdapter.changeCursor(result); 
     } 


    } 


    public void onCreateOptionsMenu(Menu menu,MenuInflater inflater) { 
     //getActivity().getMenuInflater().inflate(R.menu.activity_my_contacts, menu); 
     super.onCreateOptionsMenu(menu, inflater); 
     menu.clear(); 
     inflater.inflate(R.menu.activity_my_contacts, menu); 


    } 

    @Override 
    public boolean onOptionsItemSelected(MenuItem item) { 
     Activity activity = getActivity(); 
     Intent addContact = new Intent(activity,AddEditContacts.class); 
     startActivity(addContact); 
     return super.onOptionsItemSelected(item); 
    } 


} 

오류

07-17 22:01:33.959: E/AndroidRuntime(10924): java.lang.NullPointerException 
07-17 22:01:33.959: E/AndroidRuntime(10924): at com.ektroid.graphico.MyContactsActivity$GetContacts.onPostExecute(MyContactsActivity.java:95) 
07-17 22:01:33.959: E/AndroidRuntime(10924): at com.ektroid.graphico.MyContactsActivity$GetContacts.onPostExecute(MyContactsActivity.java:1) 
07-17 22:01:33.959: E/AndroidRuntime(10924): at android.os.AsyncTask.finish(AsyncTask.java:602) 
07-17 22:01:33.959: E/AndroidRuntime(10924): at android.os.AsyncTask.access$600(AsyncTask.java:156) 
07-17 22:01:33.959: E/AndroidRuntime(10924): at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:615) 
07-17 22:01:33.959: E/AndroidRuntime(10924): at android.os.Handler.dispatchMessage(Handler.java:99) 
07-17 22:01:33.959: E/AndroidRuntime(10924): at android.os.Looper.loop(Looper.java:137) 
07-17 22:01:33.959: E/AndroidRuntime(10924): at android.app.ActivityThread.main(ActivityThread.java:4424) 
07-17 22:01:33.959: E/AndroidRuntime(10924): at java.lang.reflect.Method.invokeNative(Native Method) 
07-17 22:01:33.959: E/AndroidRuntime(10924): at java.lang.reflect.Method.invoke(Method.java:511) 
07-17 22:01:33.959: E/AndroidRuntime(10924): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784) 
07-17 22:01:33.959: E/AndroidRuntime(10924): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551) 
07-17 22:01:33.959: E/AndroidRuntime(10924): at dalvik.system.NativeStart.main(Native Method) 
07-17 22:01:34.569: I/dalvikvm(10924): threadid=3: reacting to signal 3 
07-17 22:01:34.569: I/dalvikvm(10924): Wrote stack traces to '/data/anr/traces.txt' 
07-17 22:01:35.859: I/Process(10924): Sending signal. PID: 10924 SIG: 9 

답변

0

나는 또한 활동의 ​​단편들의 생명주기를 단계별로 점검했다. Activity 버전과 Fragment 버전을 비교했습니다. doInBackground가 null을 리턴한다는 것을 제외하고는 Fragment 버전에서는 모든 것이 정상적으로 보인다. 나는 이유를 알 수 없었다. 나는 여러 가지 가능한 해결책을 시도했지만 성공하지 못했습니다. 마지막으로 나는 이것을 위해 프래그먼트 사용을 중단하고 활동을 사용하기로 결정했다. 활동이 다 완벽하게 작동합니다

0

내부 OnPostExecute는 다음과 같은 조건이 결과에 null을 전달하는 onResume에서

if((!isRemoving()) && (isResumed()) && cursor!=null) 
+0

이것은 도움이되지 않았습니다. 고마워요 – amps

0

에게 ... 사용
그리고 onResume이 활동의 ​​라이프 사이클에에서 onCreate 후 실행으로 ...

+0

실제로 거기에, onPostExecute에 없습니다. 고맙습니다. 그러나 당신이 나를 도울 수 있습니까, 어떻게 고칠 수 있습니까? – amps

관련 문제