当我试图关闭DrawerLayout时,我的应用程序崩溃了

时间:2016-03-03 11:54:14

标签: android drawerlayout navigationview

我有一些有线问题。在我向下滚动后,当我试图关闭DrawerLayout时,应用程序崩溃了。如果您需要我将添加的任何代码。 感谢。

我希望此代码能为您提供帮助。当我滚动DrawerLayout直到结束并按下某些东西或尝试关闭它时,应用程序就会崩溃。

public class newsfeed_activity extends AppCompatActivity
    implements NavigationView.OnNavigationItemSelectedListener {

private android.support.v7.widget.Toolbar toolbar_main;
private android.support.v7.widget.Toolbar toolbar_web;
private ListAdapter listadapter;
private SwipeRefreshLayout refreshList;
private ListView listView_article;
private Handler handler = new Handler();
private WebView web;
private WebBackForwardList webBackForwardList;
private DrawerLayout drawer;

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

    Intent welcome = new Intent(this,Explanation.class);
    startActivity(welcome);

    if(FacebookAndGoogle.isLoggedWithFacebook())
    {
        FacebookAndGoogle.getBitmapFromURL(FacebookAndGoogle.getCurrentFacebookProfile().getProfilePictureUri(500, 500).toString());
        FacebookAndGoogle.setFullName(FacebookAndGoogle.getCurrentFacebookProfile().getName());
    }
    else if(FacebookAndGoogle.isLoggedWithGoogle())
    {
        FacebookAndGoogle.getBitmapFromURL(FacebookAndGoogle.getCurrentGoogleProfile().getImage().getUrl().replace("sz=50", "sz=500").toString());
        FacebookAndGoogle.setFullName(FacebookAndGoogle.getCurrentGoogleProfile().getDisplayName());
    }
    createWebView();
    toolbar_web = (android.support.v7.widget.Toolbar) findViewById(R.id.toolbar_web);
    setSupportActionBar(toolbar_web);
    getSupportActionBar().setDisplayShowTitleEnabled(false);
    toolbar_web.setVisibility(View.GONE);
    toolbar_main = (android.support.v7.widget.Toolbar) findViewById(R.id.toolbar_main);
    setSupportActionBar(toolbar_main);
    getSupportActionBar().setDisplayShowTitleEnabled(false);
    saveInInternalFolder("NewzBay", "check");
    Vector<Article> articles = new Vector<Article>();
    articles.add(new Article("Subject", "כותרת ראשית 1", "Second Headline", BitmapFactory.decodeResource(getResources(), R.drawable.anchor), null, "Site", "http://www.ynet.co.il/articles/0,7340,L-4740637,00.html", 605, 24, true));
    articles.add(new Article("Subject", "כותרת ראשית 2", "Second Headline", BitmapFactory.decodeResource(getResources(), R.drawable.anchor), null, "Site", "http://www.google.co.il", 524, 53, false));
    articles.add(new Article("Subject", "כותרת ראשית 3", "Second Headline", BitmapFactory.decodeResource(getResources(), R.drawable.anchor), null, "Site", "http://www.facebook.com", 106, 40, true));
    listView_article = (ListView) findViewById(R.id.listView_articles);
    listadapter = new ArticleAdapter(this, articles, web, toolbar_main, toolbar_web);
    listView_article.setAdapter(listadapter);
    createSwipeRefreshLayout();

    drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
    ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
            this, drawer, toolbar_main, R.string.navigation_drawer_open, R.string.navigation_drawer_close) {
        @Override
        public void onDrawerSlide(View drawerView, float slideOffset) {
            super.onDrawerSlide(drawerView, slideOffset);
            drawerHandler(drawer);
        }
    };
    toggle.setDrawerIndicatorEnabled(false);
    toolbar_main.setNavigationIcon(R.drawable.anchor);
    toolbar_main.setNavigationOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            drawerHandler(drawer);
        }
    });
    drawer.setDrawerListener(toggle);
    toggle.syncState();

    NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
    navigationView.setNavigationItemSelectedListener(this);
}


public boolean onNavigationItemSelected(MenuItem item) {
    // Handle navigation view item clicks here.
    int id = item.getItemId();
    if (id == R.id.nav_hot_news) {
        Intent intent = new Intent(this, ExploreArticles.class);
        startActivity(intent);
        this.onStop();
    } else if (id == R.id.nav_news) {

    } else if (id == R.id.nav_economy) {

    } else if (id == R.id.nav_sport) {

    } else if (id == R.id.nav_settings) {
        Intent settings = new Intent(this, settings_activity.class);
        startActivity(settings);
        this.onStop();
    }
    else if (id == R.id.nav_discconect) {
        if (FacebookAndGoogle.isLoggedWithGoogle()) {
            Plus.AccountApi.clearDefaultAccount(FacebookAndGoogle.getmGoogleApiClient());
            FacebookAndGoogle.getmGoogleApiClient().disconnect();
            FacebookAndGoogle.getmGoogleApiClient().connect();
        }
        else if(FacebookAndGoogle.isLoggedWithFacebook())
        {
            FacebookAndGoogle.reset(BitmapFactory.decodeResource(getResources(), R.drawable.user_icon));
            LoginManager.getInstance().logOut();
        }
        FacebookAndGoogle.reset(BitmapFactory.decodeResource(getResources(), R.drawable.user_icon));
        Intent intent = new Intent(this, entrance.class);
        startActivity(intent);
        finish();
    }
    DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
    drawer.closeDrawer(GravityCompat.START);
    return true;
}

活动的XML

<?xml version="1.0" encoding="utf-8"?>

<include
    layout="@layout/app_bar_newsfeed_activity"
    android:layout_width="match_parent"
    android:layout_height="match_parent" />

<android.support.design.widget.NavigationView
    android:id="@+id/nav_view"
    android:layout_width="wrap_content"
    android:layout_height="match_parent"
    android:layout_gravity="start"
    android:fitsSystemWindows="true"
    app:headerLayout="@layout/nav_header_newsfeed_activity"
    app:menu="@menu/newsfeed_activity_drawer" />

我找到了Logcat:

03-03 14:11:27.390 2115-2115/magshimim.newzbay E/AndroidRuntime: FATAL EXCEPTION: main
                                                             Process: magshimim.newzbay, PID: 2115
                                                             java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.TextView.setText(java.lang.CharSequence)' on a null object reference
                                                                 at magshimim.newzbay.newsfeed_activity.drawerHandler(newsfeed_activity.java:241)
                                                                 at magshimim.newzbay.newsfeed_activity.access$100(newsfeed_activity.java:39)
                                                                 at magshimim.newzbay.newsfeed_activity$1.onDrawerSlide(newsfeed_activity.java:94)
                                                                 at android.support.v4.widget.DrawerLayout.dispatchOnDrawerSlide(DrawerLayout.java:773)
                                                                 at android.support.v4.widget.DrawerLayout.setDrawerViewOffset(DrawerLayout.java:784)
                                                                 at android.support.v4.widget.DrawerLayout$ViewDragCallback.onViewPositionChanged(DrawerLayout.java:1848)
                                                                 at android.support.v4.widget.ViewDragHelper.continueSettling(ViewDragHelper.java:737)
                                                                 at android.support.v4.widget.DrawerLayout.computeScroll(DrawerLayout.java:1130)
                                                                 at android.view.View.updateDisplayListIfDirty(View.java:14986)
                                                                 at android.view.View.getDisplayList(View.java:15022)
                                                                 at android.view.View.draw(View.java:15789)
                                                                 at android.view.ViewGroup.drawChild(ViewGroup.java:3678)
                                                                 at android.view.ViewGroup.dispatchDraw(ViewGroup.java:3472)
                                                                 at android.view.View.updateDisplayListIfDirty(View.java:14994)
                                                                 at android.view.View.getDisplayList(View.java:15022)
                                                                 at android.view.View.draw(View.java:15789)
                                                                 at android.view.ViewGroup.drawChild(ViewGroup.java:3678)
                                                                 at android.view.ViewGroup.dispatchDraw(ViewGroup.java:3472)
                                                                 at android.view.View.updateDisplayListIfDirty(View.java:14994)
                                                                 at android.view.View.getDisplayList(View.java:15022)
                                                                 at android.view.View.draw(View.java:15789)
                                                                 at android.view.ViewGroup.drawChild(ViewGroup.java:3678)
                                                                 at android.view.ViewGroup.dispatchDraw(ViewGroup.java:3472)
                                                                 at android.view.View.updateDisplayListIfDirty(View.java:14994)
                                                                 at android.view.View.getDisplayList(View.java:15022)
                                                                 at android.view.View.draw(View.java:15789)
                                                                 at android.view.ViewGroup.drawChild(ViewGroup.java:3678)
                                                                 at android.view.ViewGroup.dispatchDraw(ViewGroup.java:3472)
                                                                 at android.view.View.updateDisplayListIfDirty(View.java:14994)
                                                                 at android.view.View.getDisplayList(View.java:15022)
                                                                 at android.view.View.draw(View.java:15789)
                                                                 at android.view.ViewGroup.drawChild(ViewGroup.java:3678)
                                                                 at android.view.ViewGroup.dispatchDraw(ViewGroup.java:3472)
                                                                 at android.view.View.draw(View.java:16071)
                                                                 at android.widget.FrameLayout.draw(FrameLayout.java:592)
                                                                 at com.android.internal.policy.impl.PhoneWindow$DecorView.draw(PhoneWindow.java:2916)
                                                                 at android.view.View.updateDisplayListIfDirty(View.java:14999)
                                                                 at android.view.View.getDisplayList(View.java:15022)
                                                                 at android.view.ThreadedRenderer.updateViewTreeDisplayList(ThreadedRenderer.java:275)
                                                                 at android.view.ThreadedRenderer.updateRootDisplayList(ThreadedRenderer.java:281)
                                                                 at android.view.ThreadedRenderer.draw(ThreadedRenderer.java:320)
                                                                 at android.view.ViewRootImpl.draw(ViewRootImpl.java:2741)
                                                                 at android.view.ViewRootImpl.performDraw(ViewRootImpl.java:2573)
                                                                 at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:2165)
                                                                 at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1180)
                                                                 at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:6558)
                                                                 at android.view.Choreographer$CallbackRecord.run(Choreographer.java:777)
                                                                 at android.view.Choreographer.doCallbacks(Choreographer.java:590)
                                                                 at android.view.Choreographer.doFrame(Choreographer.java:560)
                                                                 at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:763)
                                                                 at android.os.Handler.handleCallback(Handler.java:739)
                                                                 at android.os.Handler.dispatchMessage(Handler.java:95)
                                                                 at android.os.Looper.loop(Looper.java:145)
                                                                 at android.app.ActivityThread.main(ActivityThread.java:5832)
                                                                 at java.lang.reflect.Method.invoke(Native Method)
                                                                 at java.lang.reflect.Method.invoke(Method.java:372)
                                                                 at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1399)
                                                                 at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1194)

3 个答案:

答案 0 :(得分:0)

为什么找到onItemSelected Method.You也找到了view并点击了view child.Your process就像这样工作。 找到抽屉视图 selecet孩子。 并再次找到Dirawerview。所有时间再次查找视图。 `

  

DrawerLayout drawer =(DrawerLayout)findViewById(R.id.drawer_layout);   //删除此行,因为您已经在oncreate中       drawer.closeDrawer(GravityCompat.START);

`

答案 1 :(得分:0)

错误发生在您的活动drawerHandle()中。您最有可能尝试使用findViewById()获取对视图的引用而不是获取视图。发布drawerHandle(...)方法的正文,否则很难确切地说出错误。

答案 2 :(得分:0)

Ty for Dmitriy :) 发生的事情是我试图将文本和图片设置为空引用。

    private void drawerHandler(DrawerLayout drawer)
{
    if (drawer.isDrawerOpen(GravityCompat.START)) {
        drawer.closeDrawer(GravityCompat.START);
    }
    else {
        drawer.openDrawer(GravityCompat.START);
    }
    TextView userFullName = (TextView) drawer.findViewById(R.id.tv_userFullName);
    if(userFullName != null)
    {
        userFullName.setText(FacebookAndGoogle.getFullName());
    }
    ImageButton userPic = (ImageButton) drawer.findViewById(R.id.ib_userPic);
    if(userPic != null)
    {
        userPic.setImageBitmap(RoundedImageView.getCroppedBitmap(FacebookAndGoogle.getProfilePic(), 240));
    }
}