应用程序在启动导航栏活动时关闭

时间:2017-10-08 04:09:25

标签: android android-intent navigationbar

我的应用在使用Intent从另一个活动启动导航栏活动时崩溃。

我有一个名为guestLog的按钮,它必须启动导航栏活动。但是点击按钮后,应用程序就会强行关闭。

我对Navigationbar活动很新。有人可以告诉我,我在这里犯了什么错误吗?

Logcat:

10-08 09:28:47.602 4195-4195/com.grs.raja.collegeapp D/AndroidRuntime: Shutting down VM
10-08 09:28:47.603 4195-4195/com.grs.raja.collegeapp E/AndroidRuntime: FATAL EXCEPTION: main
                                                                       Process: com.grs.raja.collegeapp, PID: 4195
                                                                       java.lang.RuntimeException: Unable to start activity ComponentInfo{com.grs.raja.collegeapp/com.grs.raja.college.GuestPage}: java.lang.IllegalArgumentException: No view found for id 0x7f0d00a9 (com.grs.raja.collegeapp:id/first_container) for fragment Fragment{85424fa #0 id=0x7f0d00a9}
                                                                           at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2646)
                                                                           at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2707)
                                                                           at android.app.ActivityThread.-wrap12(ActivityThread.java)
                                                                           at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1460)
                                                                           at android.os.Handler.dispatchMessage(Handler.java:102)
                                                                           at android.os.Looper.loop(Looper.java:154)
                                                                           at android.app.ActivityThread.main(ActivityThread.java:6077)
                                                                           at java.lang.reflect.Method.invoke(Native Method)
                                                                           at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:866)
                                                                           at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:756)
                                                                        Caused by: java.lang.IllegalArgumentException: No view found for id 0x7f0d00a9 (com.grs.raja.collegeapp:id/first_container) for fragment Fragment{85424fa #0 id=0x7f0d00a9}
                                                                           at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1293)
                                                                           at android.support.v4.app.FragmentManagerImpl.moveFragmentToExpectedState(FragmentManager.java:1528)
                                                                           at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1595)
                                                                           at android.support.v4.app.BackStackRecord.executeOps(BackStackRecord.java:758)
                                                                           at android.support.v4.app.FragmentManagerImpl.executeOps(FragmentManager.java:2363)
                                                                           at android.support.v4.app.FragmentManagerImpl.executeOpsTogether(FragmentManager.java:2149)
                                                                           at android.support.v4.app.FragmentManagerImpl.optimizeAndExecuteOps(FragmentManager.java:2103)
                                                                           at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:2013)
                                                                           at android.support.v4.app.FragmentController.execPendingActions(FragmentController.java:388)
                                                                           at android.support.v4.app.FragmentActivity.onStart(FragmentActivity.java:607)
                                                                           at android.support.v7.app.AppCompatActivity.onStart(AppCompatActivity.java:178)
                                                                           at android.app.Instrumentation.callActivityOnStart(Instrumentation.java:1248)
                                                                           at android.app.Activity.performStart(Activity.java:6679)
                                                                           at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2609)
                                                                           at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2707) 
                                                                           at android.app.ActivityThread.-wrap12(ActivityThread.java) 
                                                                           at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1460) 
                                                                           at android.os.Handler.dispatchMessage(Handler.java:102) 
                                                                           at android.os.Looper.loop(Looper.java:154) 
                                                                           at android.app.ActivityThread.main(ActivityThread.java:6077) 
                                                                           at java.lang.reflect.Method.invoke(Native Method) 
                                                                           at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:866) 
                                                                           at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:756) 
10-08 09:28:47.604 1656-2396/system_process W/ActivityManager:   Force finishing activity com.grs.raja.collegeapp/com.grs.raja.college.GuestPage
10-08 09:28:47.607 1656-2396/system_process W/ActivityManager:   Force finishing activity com.grs.raja.collegeapp/com.grs.raja.college.ActivityOne

我的导航栏活动:(GuestPage.java)

package com.grs.raja.college;

import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v4.app.FragmentManager;
import android.view.View;
import android.support.design.widget.NavigationView;
import android.support.v4.view.GravityCompat;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBarDrawerToggle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.MenuItem;

public class GuestPage extends AppCompatActivity
        implements NavigationView.OnNavigationItemSelectedListener {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_guest_page);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);



        DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
        ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
                this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
        drawer.setDrawerListener(toggle);
        toggle.syncState();

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

        FragmentManager fragmentManager = getSupportFragmentManager();
        fragmentManager.beginTransaction().replace(R.id.first_container, new android.support.v4.app.Fragment()).commit();


        /*View parentLayout = findViewById(android.R.id.content);
        Snackbar.make(parentLayout, "You are viewing as a Guest.", Snackbar.LENGTH_LONG)
                .setAction("CLOSE", new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {

                    }
                })
                .setActionTextColor(getResources().getColor(android.R.color.holo_red_light ))
                .show();  */
    }

    @Override
    public void onBackPressed() {
        DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
        if (drawer.isDrawerOpen(GravityCompat.START)) {
            drawer.closeDrawer(GravityCompat.START);
        } else {
            super.onBackPressed();
        }
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.guest_page, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }

    @SuppressWarnings("StatementWithEmptyBody")
    @Override
    public boolean onNavigationItemSelected(MenuItem item) {
        // Handle navigation view item clicks here.
        int id = item.getItemId();

       /* if (id == R.id.nav_camera) {
            // Handle the camera action
        } else if (id == R.id.nav_gallery) {

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

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

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

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

        }*/

        DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
        drawer.closeDrawer(GravityCompat.START);
        return true;
    }
}

启动活动的功能:( guestLog是一个按钮)

guestLog.setOnClickListener(new View.OnClickListener()
{
    @Override
    public void onClick(View v) {
    Intent toGuest = new Intent (ActivityOne.this,GuestPage.class); startActivity(toGuest);
    }
});

2 个答案:

答案 0 :(得分:0)

问题出在以下代码中:

FragmentManager fragmentManager = getSupportFragmentManager();
fragmentManager.beginTransaction()
               .replace(R.id.first_container, new android.support.v4.app.Fragment())
               .commit();

您需要使用已创建的片段更改new android.support.v4.app.Fragment(),这会扩展android.support.v4.app.Fragment ..

答案 1 :(得分:0)

lang.IllegalArgumentException:找不到片段片段{85424fa#0 id = 0x7f0d00a9

的id 0x7f0d00a9(com.grs.raja.collegeapp:id / first_container)的视图

该异常告诉我们没有名为first_container的视图,请检查它是否在您的布局中可用。