2013-09-30 2 views
0

응용 프로그램이 충돌하지 않고 notifyDataSetChanged를 호출 할 수 없습니다.조각에서 Asynctask 다음에 notifyDataSetChanged하는 방법

ListFragment 클래스 :

public class ServerListFragment extends ListFragment implements OnTaskCompleteListener { 


    private AsyncTaskManager mAsyncTaskManager; 
private ServersDataSource datasource; 
private ArrayList<Server> servers; 
    private boolean paused = false; 
    private boolean generateList = true; 
    private String operation = ""; 
/** 
* The serialization (saved instance state) Bundle key representing the 
* activated item position. Only used on tablets. 
*/ 
private static final String STATE_ACTIVATED_POSITION = "activated_position"; 

/** 
* The fragment's current callback object, which is notified of list item 
* clicks. 
*/ 
private Callbacks mCallbacks = sDummyCallbacks; 

/** 
* The current activated item position. Only used on tablets. 
*/ 
private int mActivatedPosition = ListView.INVALID_POSITION; 

/** 
* A callback interface that all activities containing this fragment must 
* implement. This mechanism allows activities to be notified of item 
* selections. 
*/ 
public interface Callbacks { 
    /** 
    * Callback for when an item has been selected. 
    */ 
    public void onItemSelected(String id); 
} 

/** 
* A dummy implementation of the {@link Callbacks} interface that does 
* nothing. Used only when this fragment is not attached to an activity. 
*/ 
private static Callbacks sDummyCallbacks = new Callbacks() { 
    @Override 
    public void onItemSelected(String id) { 
    } 
}; 

/** 
* Mandatory empty constructor for the fragment manager to instantiate the 
* fragment (e.g. upon screen orientation changes). 
*/ 
public ServerListFragment() { 
} 
/* 
* Default list adapter 
*/ 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 

    // retainInstance and survive on configuration changes (rotation) 
    setRetainInstance(true); 


    // Create manager and set this activity as context and listener 
    mAsyncTaskManager = new AsyncTaskManager(getActivity(), this); 
    // Handle task that can be retained before 
    mAsyncTaskManager.handleRetainedTask(getActivity().getLastNonConfigurationInstance()); 

    // start async task 
    getServersServices(); 


} 


@Override 
public void onViewCreated(View view, Bundle savedInstanceState) { 
    super.onViewCreated(view, savedInstanceState); 

    // Restore the previously serialized activated item position. 
    if (savedInstanceState != null 
      && savedInstanceState.containsKey(STATE_ACTIVATED_POSITION)) { 
     setActivatedPosition(savedInstanceState 
       .getInt(STATE_ACTIVATED_POSITION)); 
    } 
} 

@Override 
public void onAttach(Activity activity) { 
    super.onAttach(activity); 

    // Activities containing this fragment must implement its callbacks. 
    if (!(activity instanceof Callbacks)) { 
     throw new IllegalStateException(
       "Activity must implement fragment's callbacks."); 
    } 

    mCallbacks = (Callbacks) activity; 
} 

@Override 
public void onDetach() { 
    super.onDetach(); 

    // Reset the active callbacks interface to the dummy implementation. 
    mCallbacks = sDummyCallbacks; 
} 

@Override 
public void onListItemClick(ListView listView, View view, int position, 
     long id) { 
    super.onListItemClick(listView, view, position, id); 

    // Notify the active callbacks interface (the activity, if the 
    // fragment is attached to one) that an item has been selected. 
    mCallbacks.onItemSelected(DummyContent.ITEMS.get(position).id); 
} 

@Override 
public void onSaveInstanceState(Bundle outState) { 
    super.onSaveInstanceState(outState); 
    if (mActivatedPosition != ListView.INVALID_POSITION) { 
     // Serialize and persist the activated item position. 
     outState.putInt(STATE_ACTIVATED_POSITION, mActivatedPosition); 
    } 
} 

/** 
* Turns on activate-on-click mode. When this mode is on, list items will be 
* given the 'activated' state when touched. 
*/ 
public void setActivateOnItemClick(boolean activateOnItemClick) { 
    // When setting CHOICE_MODE_SINGLE, ListView will automatically 
    // give items the 'activated' state when touched. 
    getListView().setChoiceMode(
      activateOnItemClick ? ListView.CHOICE_MODE_SINGLE 
        : ListView.CHOICE_MODE_NONE); 
} 

private void setActivatedPosition(int position) { 
    if (position == ListView.INVALID_POSITION) { 
     getListView().setItemChecked(mActivatedPosition, false); 
    } else { 
     getListView().setItemChecked(position, true); 
    } 

    mActivatedPosition = position; 
} 

public void notifyDataSetChanged() { 
    ((BaseAdapter) getListAdapter()).notifyDataSetChanged(); 
} 


public void getServersServices() { 
    datasource = new ServersDataSource(getActivity()); 
    datasource.open(); 
    datasource.emptyServersTable(); 
    operation = "GetSevers"; 
    mAsyncTaskManager.setupTask(new Task(getResources(), getActivity(), getResources().getString(R.string.refreshing), operation, 0, "")); 
} 


public void onTaskComplete(Task task) { 
    if (task.isCancelled()) { 
     // Report about cancel 
     if (DEBUG) Log.i(TAG, "ServersStatus onTaskComplete task cancel"); 
    } else { 
      // Get result 
      Boolean result = null; 
      try { 
       result = task.get(); 
      } catch (Exception e) { 
       if (DEBUG) Log.e(TAG, "ServersStatus onTaskComplete error: " + e.toString()); 
      } 
      // Report about result 
      servers = datasource.getAllServers(); 
      if(servers.size() > 0 && generateList){ 
       DummyContent.setContext(servers); 
       notifyDataSetChanged(); 
      } 

     operation = ""; 
    } 
    generateList = true; 
} 

// Our handler for received Intents. This will be called whenever an Intent 
// with an action named "custom-event-name" is broadcasted. 
private BroadcastReceiver mMessageReceiver = new BroadcastReceiver() { 
    @Override 
    public void onReceive(Context context, Intent intent) { 
     // Get extra data included in the Intent 
     String message = intent.getStringExtra("message"); 
     if (message.equals("Refresh list")) { 
      servers = datasource.getAllServers(); 
      getServersServices(); 
     } 
    } 
}; 
} 

오류 로그 :

> 09-30 15:15:39.360: E/AndroidRuntime(14322): FATAL EXCEPTION: main 
> 09-30 15:15:39.360: E/AndroidRuntime(14322): 
> java.lang.NullPointerException 09-30 15:15:39.360: 
> E/AndroidRuntime(14322): at 
> com.ww.www.ServerListFragment.notifyDataSetChanged(ServerListFragment.java:307) 
> 09-30 15:15:39.360: E/AndroidRuntime(14322): at 
> com.ww.www.ServerListFragment.onTaskComplete(ServerListFragment.java:385) 
> 09-30 15:15:39.360: E/AndroidRuntime(14322): at 
> com.ww.www.core.AsyncTaskManager.onComplete(AsyncTaskManager.java:67) 
> 09-30 15:15:39.360: E/AndroidRuntime(14322): at 
> com.ww.www.core.Task.onPostExecute(Task.java:330) 09-30 15:15:39.360: 
> E/AndroidRuntime(14322): at 
> com.ww.www.core.Task.onPostExecute(Task.java:1) 09-30 15:15:39.360: 
> E/AndroidRuntime(14322): at 
> android.os.AsyncTask.finish(AsyncTask.java:631) 09-30 15:15:39.360: 
> E/AndroidRuntime(14322): at 
> android.os.AsyncTask.access$600(AsyncTask.java:177) 09-30 
> 15:15:39.360: E/AndroidRuntime(14322): at 
> android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:644) 
> 09-30 15:15:39.360: E/AndroidRuntime(14322): at 
> android.os.Handler.dispatchMessage(Handler.java:99) 09-30 
> 15:15:39.360: E/AndroidRuntime(14322): at 
> android.os.Looper.loop(Looper.java:137) 09-30 15:15:39.360: 
> E/AndroidRuntime(14322): at 
> android.app.ActivityThread.main(ActivityThread.java:4898) 09-30 
> 15:15:39.360: E/AndroidRuntime(14322): at 
> java.lang.reflect.Method.invokeNative(Native Method) 09-30 
> 15:15:39.360: E/AndroidRuntime(14322): at 
> java.lang.reflect.Method.invoke(Method.java:511) 09-30 15:15:39.360: 
> E/AndroidRuntime(14322): at 
> com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1008) 
> 09-30 15:15:39.360: E/AndroidRuntime(14322): at 
> com.android.internal.os.ZygoteInit.main(ZygoteInit.java:775) 09-30 
> 15:15:39.360: E/AndroidRuntime(14322): at 
> dalvik.system.NativeStart.main(Native Method) 

답변

1

나는 당신이 그 일식 마스터 세부 사항은 DummyContent 클래스와 흐름에 대한 제공의 예를 사용하는 것을 볼 수 있지만,

setListAdapter(new ArrayAdapter<DummyContent.DummyItem>(getActivity(), 
       android.R.layout.simple_list_item_activated_1, 
       android.R.id.text1, 
       DummyContent.ITEMS)); 

가 넣어이 코드가 작동하려면 당신이 중요 코드의 평화를 제거한 on the CreateCreate

0

코드에서 어댑터를 참조 할 수 없습니다. 제발 어댑터를 설정하십시오.

0

ListFragment.setListAdapter가 어디에서나 호출되는 것을 보지 못했습니다. 여기에 충돌의 이유가 나타났습니다.

public void notifyDataSetChanged() { 
    ((BaseAdapter) getListAdapter()).notifyDataSetChanged(); 
} 

여기에서는 List 어댑터를 가져와 notifyDataSetChanged()를 호출하려고합니다. 목록 어댑터가 없으므로 NPE가됩니다.

0

문제는이 방법에 아마도 :

public void notifyDataSetChanged() { 
    ((BaseAdapter) getListAdapter()).notifyDataSetChanged(); 
} 

는 것 같아요 그 getListAdapter() 반환 null 난 당신이 어느 곳이 ListFragment에 어떤 어댑터를 설정하는 것을 볼 수 없습니다 때문입니다.

관련 문제