2014-05-13 3 views
0

내 문제는 내가 ListActivity 클래스를 가지고 있으며 getApplicationContext가 null이기 때문에 logout 함수를 실행하려고 할 때 nullpointerexception이 발생한다는 것입니다. ListActivity getApplicationContext() nullPointerException

문제

는 여기에 있습니다 : 구름

 userFunction.logoutUser(getApplicationContext()); 

클래스 :

public class Cloudy extends ListActivity { 

     private static final int LOGOUT = 4; 

     private LinkedList<String> mListItems; 
     private PullToRefreshListView mPullRefreshListView; 
     private ArrayAdapter<String> mAdapter; 

     private UserFunctions userFunction; 

     @Override 
     protected void onCreate(Bundle savedInstanceState) { 
      super.onCreate(savedInstanceState); 
      setContentView(R.layout.activity_cloudy); 

      mPullRefreshListView = (PullToRefreshListView) findViewById(R.id.pull_refresh_list); 

      // Set a listener to be invoked when the list should be refreshed. 
      mPullRefreshListView.setOnRefreshListener(new OnRefreshListener<ListView>() { 
       @Override 
       public void onRefresh(PullToRefreshBase<ListView> refreshView) { 
        String label = DateUtils.formatDateTime(getApplicationContext(), System.currentTimeMillis(), DateUtils.FORMAT_SHOW_TIME 
          | DateUtils.FORMAT_SHOW_DATE | DateUtils.FORMAT_ABBREV_ALL); 

        // Update the LastUpdatedLabel 
        refreshView.getLoadingLayoutProxy().setLastUpdatedLabel(label); 

        // Do work to refresh the list here. 
        new GetDataTask().execute(); 
       } 
      }); 

      // Add an end-of-list listener 
      mPullRefreshListView.setOnLastItemVisibleListener(new OnLastItemVisibleListener() { 

       @Override 
       public void onLastItemVisible() { 
        Toast.makeText(Cloudy.this, "End of List!", Toast.LENGTH_SHORT).show(); 
       } 
      }); 

      ListView actualListView = mPullRefreshListView.getRefreshableView(); 

      // Need to use the Actual ListView when registering for Context Menu 
      registerForContextMenu(actualListView); 

      mListItems = new LinkedList<String>(); 
      mListItems.addAll(Arrays.asList(mStrings)); 

      mAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, mListItems); 

      /** 
      * Add Sound Event Listener 
      */ 
      SoundPullEventListener<ListView> soundListener = new SoundPullEventListener<ListView>(this); 
      soundListener.addSoundEvent(State.PULL_TO_REFRESH, R.raw.pull_event); 
      soundListener.addSoundEvent(State.RESET, R.raw.reset_sound); 
      soundListener.addSoundEvent(State.REFRESHING, R.raw.refreshing_sound); 
      mPullRefreshListView.setOnPullEventListener(soundListener); 

      // You can also just use setListAdapter(mAdapter) or 
      // mPullRefreshListView.setAdapter(mAdapter) 
      actualListView.setAdapter(mAdapter); 
     } 

     private class GetDataTask extends AsyncTask<Void, Void, String[]> { 

      @Override 
      protected String[] doInBackground(Void... params) { 
       // Simulates a background job. 
       try { 
        Thread.sleep(4000); 
       } catch (InterruptedException e) { 
       } 
       return mStrings; 
      } 

      @Override 
      protected void onPostExecute(String[] result) { 
       mListItems.addFirst("Added after refresh..."); 
       mAdapter.notifyDataSetChanged(); 

       // Call onRefreshComplete when the list has been refreshed. 
       mPullRefreshListView.onRefreshComplete(); 

       super.onPostExecute(result); 
      } 
     } 

     @Override 
     public boolean onCreateOptionsMenu(Menu menu) { 
      menu.add(Menu.NONE, LOGOUT, Menu.NONE, "Logout"); 
      return super.onCreateOptionsMenu(menu); 
     } 

     @Override 
     public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) { 
      AdapterContextMenuInfo info = (AdapterContextMenuInfo) menuInfo; 

      menu.setHeaderTitle("Item: " + getListView().getItemAtPosition(info.position)); 
      menu.add("Item 1"); 
      menu.add("Item 2"); 
      menu.add("Item 3"); 
      menu.add("Item 4"); 

      super.onCreateContextMenu(menu, v, menuInfo); 
     } 

     @Override 
     public boolean onOptionsItemSelected(MenuItem item) { 

      switch (item.getItemId()) { 
      case LOGOUT: 
       userFunction.logoutUser(getApplicationContext()); 
       Intent intent = new Intent(Cloudy.this, Login.class); 

       intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); 
       startActivity(intent); 

       finish(); 
       break; 

      } 

      return super.onOptionsItemSelected(item); 
     } 

     private String[] mStrings = { "Abbaye de Belloc", "Abbaye du Mont des Cats", "Abertam", "Abondance", "Ackawi", "Acorn", "Adelost", "Affidelice au Chablis", 
       "Afuega'l Pitu", "Airag", "Airedale", "Aisy Cendre", "Allgauer Emmentaler", "Abbaye de Belloc", "Abbaye du Mont des Cats", "Abertam", "Abondance", 
       "Ackawi", "Acorn", "Adelost", "Affidelice au Chablis", "Afuega'l Pitu", "Airag", "Airedale", "Aisy Cendre", "Allgauer Emmentaler" }; 
    } 

로그 :

05-13 21:17:41.717: E/AndroidRuntime(3716): FATAL EXCEPTION: main 
05-13 21:17:41.717: E/AndroidRuntime(3716): Process: com.ramon.cloudy, PID: 3716 
05-13 21:17:41.717: E/AndroidRuntime(3716): java.lang.NullPointerException 
05-13 21:17:41.717: E/AndroidRuntime(3716):  at com.ramon.cloudy.Cloudy.onOptionsItemSelected(Cloudy.java:142) 
05-13 21:17:41.717: E/AndroidRuntime(3716):  at android.app.Activity.onMenuItemSelected(Activity.java:2687) 
05-13 21:17:41.717: E/AndroidRuntime(3716):  at com.android.internal.policy.impl.PhoneWindow.onMenuItemSelected(PhoneWindow.java:1103) 
05-13 21:17:41.717: E/AndroidRuntime(3716):  at com.android.internal.view.menu.MenuBuilder.dispatchMenuItemSelected(MenuBuilder.java:735) 
05-13 21:17:41.717: E/AndroidRuntime(3716):  at com.android.internal.view.menu.MenuItemImpl.invoke(MenuItemImpl.java:152) 
05-13 21:17:41.717: E/AndroidRuntime(3716):  at com.android.internal.view.menu.MenuBuilder.performItemAction(MenuBuilder.java:874) 
05-13 21:17:41.717: E/AndroidRuntime(3716):  at com.android.internal.view.menu.ListMenuPresenter.onItemClick(ListMenuPresenter.java:177) 
05-13 21:17:41.717: E/AndroidRuntime(3716):  at android.widget.AdapterView.performItemClick(AdapterView.java:308) 
05-13 21:17:41.717: E/AndroidRuntime(3716):  at android.widget.AbsListView.performItemClick(AbsListView.java:1478) 
05-13 21:17:41.717: E/AndroidRuntime(3716):  at android.widget.AbsListView$PerformClick.run(AbsListView.java:3480) 
05-13 21:17:41.717: E/AndroidRuntime(3716):  at android.widget.AbsListView$3.run(AbsListView.java:4823) 
05-13 21:17:41.717: E/AndroidRuntime(3716):  at android.os.Handler.handleCallback(Handler.java:733) 
05-13 21:17:41.717: E/AndroidRuntime(3716):  at android.os.Handler.dispatchMessage(Handler.java:95) 
05-13 21:17:41.717: E/AndroidRuntime(3716):  at android.os.Looper.loop(Looper.java:157) 
05-13 21:17:41.717: E/AndroidRuntime(3716):  at android.app.ActivityThread.main(ActivityThread.java:5356) 
05-13 21:17:41.717: E/AndroidRuntime(3716):  at java.lang.reflect.Method.invokeNative(Native Method) 
05-13 21:17:41.717: E/AndroidRuntime(3716):  at java.lang.reflect.Method.invoke(Method.java:515) 
05-13 21:17:41.717: E/AndroidRuntime(3716):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1265) 
05-13 21:17:41.717: E/AndroidRuntime(3716):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1081) 
05-13 21:17:41.717: E/AndroidRuntime(3716):  at dalvik.system.NativeStart.main(Native Method) 
+0

왜 처음에는'getApplicationContext()'를 사용하고 있습니까? 그것은 거의 정답이 아닙니다. http://www.doubleencore.com/2013/06/context/ – CommonsWare

+0

어디에 (서)'null '을 의미하는지 알지 못하기 때문에'userFunction'을 어디에서 만드나요 – tyczj

답변

0

NPE는 userFunctiongetApplicationContext()의 널 (null)이 아닌 결과 것을 의미한다.

+0

고마워요! 그게 다야! NPE가 userFunction이라는 것을 어디서 볼 수 있습니까? – user3633957

+0

null이거나 실제 객체를 가리 키지 않는 변수의 속성이나 메소드에 액세스하려는 경우 NPE가 발생합니다. userFunction은 메서드에 액세스하려고 시도하는 해당 행의 유일한 변수이므로 해당 변수가 있어야합니다. – Ridcully

관련 문제