Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/231.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
Android onResume()中的java.lang.NullPointerException_Android - Fatal编程技术网

Android onResume()中的java.lang.NullPointerException

Android onResume()中的java.lang.NullPointerException,android,Android,在按下home(主页)按钮最小化我的应用程序并在5-10分钟后重新打开后,我出现以下错误: java.lang.NullPointerException at de.cwalz.betconnection.aq.a(Unknown Source) at com.a.a.a.k.run(Unknown Source) at android.os.Handler.handleCallback(Handler.java:615) at android.os.Handler.dispatchMessag

在按下home(主页)按钮最小化我的应用程序并在5-10分钟后重新打开后,我出现以下错误:

java.lang.NullPointerException
at de.cwalz.betconnection.aq.a(Unknown Source)
at com.a.a.a.k.run(Unknown Source)
at android.os.Handler.handleCallback(Handler.java:615)
at android.os.Handler.dispatchMessage(Handler.java:92)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:4867)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1007)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:774)
at dalvik.system.NativeStart.main(Native Method)
我覆盖onResume的唯一时间是在我的基本活动中,我的其他活动从中扩展:

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

    int status = GooglePlayServicesUtil.isGooglePlayServicesAvailable(getApplicationContext());
    if (status != ConnectionResult.SUCCESS) {
        GooglePlayServicesUtil.getErrorDialog(status, this, 9000).show();   
    }
}
我想不出问题出在哪里,例外情况没有那么有用

编辑:我能够消除异常:

java.lang.NullPointerException
at de.cwalz.betconnection.WelcomeActivity$3.onSuccess(Unknown Source)
at com.loopj.android.http.JsonHttpResponseHandler$1$1.run(Unknown Source)
at android.os.Handler.handleCallback(Handler.java:615)
at android.os.Handler.dispatchMessage(Handler.java:92)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:4867)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1007)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:774)
at dalvik.system.NativeStart.main(Native Method) 
WelcomeActivity.java

public class WelcomeActivity extends ActionBarActivity implements ActionBar.TabListener {
    // views
    static ListView followingView;
    static ListView betView;

    // values
    static List<Bet> betList = new ArrayList<Bet>();
    static List<Bet> followingList = new ArrayList<Bet>();

    // adapter
    static BetListViewAdapter betAdapter;
    static BetListViewAdapter followingAdapter;

    // fragments
    FollowListFragment followFragment;
    BetListFragment betFragment;

    // drawer values
    protected List<String> mMenuItems;
    protected DrawerLayout mDrawerLayout;
    protected ListView mDrawerList;
    protected ActionBarDrawerToggle mDrawerToggle;
    protected CharSequence mTitle;
    protected CharSequence mDrawerTitle;

    /**
     * The {@link android.support.v4.view.PagerAdapter} that will provide
     * fragments for each of the sections. We use a {@link FragmentPagerAdapter}
     * derivative, which will keep every loaded fragment in memory. If this
     * becomes too memory intensive, it may be best to switch to a
     * {@link android.support.v4.app.FragmentStatePagerAdapter}.
     */
    SectionsPagerAdapter mSectionsPagerAdapter;

    /**
     * The {@link ViewPager} that will host the section contents.
     */
    ViewPager mViewPager;

    /**
     * The current user
     */ 
    UserHandler user = null;



    @SuppressLint("NewApi")
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_welcome);

        // Set up the action bar.
        final ActionBar actionBar = getSupportActionBar();
        actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
        // Create the adapter that will return a fragment for each of the three
        // primary sections of the activity.
        mSectionsPagerAdapter = new SectionsPagerAdapter(
                getSupportFragmentManager());

        // Set up the ViewPager with the sections adapter.
        mViewPager = (ViewPager) findViewById(R.id.pager);
        mViewPager.setAdapter(mSectionsPagerAdapter);

        // When swiping between different sections, select the corresponding
        // tab. We can also use ActionBar.Tab#select() to do this if we have
        // a reference to the Tab.
        mViewPager.setOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener() {
                    @Override
                    public void onPageSelected(int position) {
                        actionBar.setSelectedNavigationItem(position);
                    }
                });

        // For each of the sections in the app, add a tab to the action bar.
        for (int i = 0; i < mSectionsPagerAdapter.getCount(); i++) {
            // Create a tab with text corresponding to the page title defined by
            // the adapter. Also specify this Activity object, which implements
            // the TabListener interface, as the callback (listener) for when
            // this tab is selected.
            actionBar.addTab(actionBar.newTab()
                    .setText(mSectionsPagerAdapter.getPageTitle(i))
                    .setTabListener(this));
        }

        // create user object
        user = new UserHandler(getApplicationContext());

        if (!user.isLoggedIn()) {
            Intent loginIntent = new Intent(getApplicationContext(), LoginActivity.class);
            startActivity(loginIntent);
        }

        // set ad for non premium user
        if (!user.isPremium()) {
            AdHandler.setBannerAd(this, (LinearLayout)findViewById(R.id.adPlaceholder));
        }

        // check if bet was submitted
        String bet = "";
        bet = getIntent().getStringExtra(AddBetActivity.EXTRA_BET);
        if (bet != null) {
            Toast toast = Toast.makeText(getApplicationContext(), bet, Toast.LENGTH_LONG);
            toast.show();
        }

        // check if first login
        Boolean firstLogin = getIntent().getBooleanExtra(RegisterActivity.EXTRA_FIRSTLOGIN, false);
        if (firstLogin) {
            showFirstLoginDialog();
        }

        // ...

        // show progress bars
        loadData();

    }

    /**
     * A {@link FragmentPagerAdapter} that returns a fragment corresponding to
     * one of the sections/tabs/pages.
     */
    public class SectionsPagerAdapter extends FragmentPagerAdapter {

        public SectionsPagerAdapter(FragmentManager fm) {
            super(fm);
        }

        @Override
        public Fragment getItem(int position) {
            if (position == 0) {
                betFragment = BetListFragment.getInstance();
                return betFragment;
            }
            else {
                followFragment = FollowListFragment.getInstance();
                return followFragment;
            }
        }

        @Override
        public int getCount() {
            // Show 2 total pages.
            return 2;
        }

        @Override
        public CharSequence getPageTitle(int position) {
            if (position == 0)
                return getString(R.string.page_title_bets);
            else
                return getString(R.string.page_title_following);
        }
    }

    // ...

    @Override
    public void onTabSelected(ActionBar.Tab tab,
            FragmentTransaction fragmentTransaction) {
        // When the given tab is selected, switch to the corresponding page in
        // the ViewPager.
        mViewPager.setCurrentItem(tab.getPosition());
    }

    @Override
    public void onTabUnselected(ActionBar.Tab tab,
            FragmentTransaction fragmentTransaction) {
    }

    @Override
    public void onTabReselected(ActionBar.Tab tab,
            FragmentTransaction fragmentTransaction) {
    }

    // ...

    public static class BetListFragment extends Fragment {
        Context context;
        View rootView;
        ListView listView;
        ProgressBar progressBar;
        TextView noBets;

        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
            context = container.getContext();
            rootView = inflater.inflate(R.layout.fragment_bet_list, container, false);

            listView = (ListView) rootView.findViewById(R.id.betList);
            progressBar = (ProgressBar) rootView.findViewById(R.id.progressBar);
            noBets = (TextView) rootView.findViewById(R.id.noBets);

            betAdapter = new BetListViewAdapter(context, R.layout.listview_bet, betList);
            listView.setAdapter(betAdapter);
            // set listener
            listView.setOnItemClickListener(new ListView.OnItemClickListener() {
                @Override
                public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                    Bet bet = betAdapter.getItem(position);

                    Intent betActivity = new Intent(context, BetActivity.class);
                    betActivity.putExtra("de.cwalz.bet.BETID", bet.getID());
                    startActivity(betActivity);
                }
            });
            return rootView;
        }

        public View getRootView() {
            return rootView;
        }

        public void showLoadingView(boolean status) {       
            if (status) {
                listView.setVisibility(View.GONE);
                noBets.setVisibility(View.GONE);
                progressBar.setVisibility(View.VISIBLE);
            }
            else {
                listView.setVisibility(View.VISIBLE);
                progressBar.setVisibility(View.GONE);               
            }
        }

        public BetListFragment() {}

        public static BetListFragment getInstance() {
            return new BetListFragment();
        }
    }

    public static class FollowListFragment extends Fragment {
        Context context;
        View rootView;
        ListView listView;
        ProgressBar progressBar;
        TextView noFollowingUsers;

        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
            context = container.getContext();

            rootView = inflater.inflate(R.layout.fragment_bet_list, container, false);
            listView = (ListView) rootView.findViewById(R.id.betList);
            progressBar = (ProgressBar) rootView.findViewById(R.id.progressBar);
            noFollowingUsers = (TextView) rootView.findViewById(R.id.noFollowingUsers);

            followingAdapter = new BetListViewAdapter(context, R.layout.listview_bet, followingList);
            listView.setAdapter(followingAdapter);
            // set listener
            listView.setOnItemClickListener(new ListView.OnItemClickListener() {
                @Override
                public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                    Bet bet = followingAdapter.getItem(position);

                    Intent betActivity = new Intent(context, BetActivity.class);
                    betActivity.putExtra("de.cwalz.bet.BETID", bet.getID());
                    startActivity(betActivity);
                }
            });
            return rootView;
        }

        public View getRootView() {
            return rootView;
        }

        public void showLoadingView(boolean status) {
            if (status) {
                listView.setVisibility(View.GONE);
                noFollowingUsers.setVisibility(View.GONE);
                progressBar.setVisibility(View.VISIBLE);
            }
            else {
                listView.setVisibility(View.VISIBLE);
                progressBar.setVisibility(View.GONE);               
            }
        }

        public FollowListFragment() {}

        public static FollowListFragment getInstance() {
            return new FollowListFragment();
        }
    }

    private void loadData() {
        RequestParams params = new RequestParams();

        String email = user.getEmail();
        params.put("email", email);
        params.put("password", user.getPassword());
        params.put("token", TextUtil.getToken(email));

        DataUtil.post("GetBetList", params, new JsonHttpResponseHandler() {
            public void onSuccess(int statusCode, Header[] headers, JSONObject response) {
                try {
                    // hide progress bars
                    betFragment.showLoadingView(false);
                    followFragment.showLoadingView(false);


                    JSONArray blist = response.getJSONArray("blist");
                    JSONArray flist = response.getJSONArray("flist");

                    // add bets
                    betList.clear();
                    for(int i=0; i<blist.length(); i++) {
                        // ...
                    }

                    // add bets from followed users
                    followingList.clear();
                    for(int i=0; i<flist.length(); i++) {
                        // ...
                    }

                    if (betList.isEmpty()) {
                        TextView text = (TextView) betFragment.getRootView().findViewById(R.id.noBets);
                        text.setVisibility(View.VISIBLE);
                    }

                    if (followingList.isEmpty()) {
                        TextView text = (TextView) followFragment.getRootView().findViewById(R.id.noFollowingUsers);
                        text.setVisibility(View.VISIBLE);
                    }

                    // notify adapter and show listview
                    betAdapter.notifyDataSetChanged();
                    followingAdapter.notifyDataSetChanged();
                }
                catch (JSONException e) {
                     Log.e("ERROR", e.getMessage());
                }
            }
        });         
    }
}

你是在用ProGuard吗?因为这可以解释为什么你的异常没有那么有用。你能在这里放一个断点并调试,看看在哪一行抛出异常吗?你怎么知道它在onResume中?可能该活动已重新创建并调用了onCreate。@Schontoon是的,我正在使用ProGuard。brummfondel这只是一个假设。您仍然无法对内部类进行模糊处理以找到实际的行号。但另一种方法是只记录onSuccess方法中使用的每个对象,然后就可以看到哪个对象为null,并修复它所在的路径。很可能当你的应用程序恢复时,事情会以一种跳过某些对象的创建或初始化的方式开始,因此你会崩溃,因为它是空的。