Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/200.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java ListActivity getApplicationContext()nullPointerException_Java_Android_Listactivity_Pull To Refresh - Fatal编程技术网

Java ListActivity getApplicationContext()nullPointerException

Java ListActivity getApplicationContext()nullPointerException,java,android,listactivity,pull-to-refresh,Java,Android,Listactivity,Pull To Refresh,我的问题是我有一个ListActivity类,当我执行注销函数时,我有一个nullpointerexception,因为getApplicationContext为null 问题在于: userFunction.logoutUser(getApplicationContext()); 类多云: public class Cloudy extends ListActivity { private static final int LOGOUT = 4;

我的问题是我有一个ListActivity类,当我执行注销函数时,我有一个nullpointerexception,因为getApplicationContext为null

问题在于:

        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" };
    }

NPE意味着
userFunction
为空,而不是
getApplicationContext()

的结果。首先为什么要使用
getApplicationContext()
?这很少是正确的答案。你在哪里创建
userFunction
,因为我看不到它是在任何地方创建的,这意味着它是空的谢谢!就这样!在哪里可以看到NPE是userFunction?如果要访问null变量的属性或方法,即不指向实际对象,则会抛出NPE。由于userFunction是该行中尝试访问方法的唯一变量,因此它必须是它。
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)