Android Crash导航抽屉

时间:2017-12-20 20:12:06

标签: java android

我几乎没有做任何事情,并且在点击第一个项目菜单时应用程序崩溃了。

manager_power.xml(布局):

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <Button
        android:id="@+id/btnOff"
        android:layout_width="99dp"
        android:layout_height="61dp"
        android:layout_marginEnd="8dp"
        android:layout_marginStart="8dp"
        android:layout_marginTop="152dp"
        android:text="TURN OFF"
        android:background="?android:attr/selectableItemBackground"
        android:drawableTop="@drawable/ic_shut_down_icon"
        app:layout_constraintEnd_toStartOf="@+id/btnRestart"
        app:layout_constraintHorizontal_bias="0.917"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <Button
        android:id="@+id/btnRestart"
        android:layout_width="99dp"
        android:layout_height="61dp"
        android:layout_marginEnd="100dp"
        android:layout_marginTop="152dp"
        android:text="RESTART"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        android:layout_marginRight="100dp" />

    <Button
        android:id="@+id/btnLock"
        android:layout_width="120dp"
        android:layout_height="61dp"
        android:layout_marginBottom="224dp"
        android:layout_marginStart="112dp"
        android:text="LOCK"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        android:layout_marginLeft="112dp" />
</android.support.constraint.ConstraintLayout>

主要课程:

import android.app.FragmentManager;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
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;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RelativeLayout;

import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.PrintWriter;
import java.net.Socket;

public class MainActivity extends AppCompatActivity
        implements NavigationView.OnNavigationItemSelectedListener {
    public static Socket clientSocket = null;
    public static ObjectInputStream objectInputStream = null;
    public static ObjectOutputStream objectOutputStream = null;
    EditText e1;
    Button bt, connectButton;
    PrintWriter pw;
    Socket s;

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

        FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
        fab.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
                        .setAction("Action", null).show();
            }
        });

        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.addDrawerListener(toggle);
        toggle.syncState();

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




    }

    @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.main, 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();
        //FragmentManager fragmentManager = getFragmentManager();
        FragmentManager fragmentManager = getFragmentManager();



        if (id == R.id.nav_power) {
            fragmentManager.beginTransaction().replace(R.id.content_frame, new managerComputer()).commit();
        } else if (id == R.id.nav_connect) {
         //   fragmentManager.beginTransaction().replace(R.id.content_frame, new startConnect()).commit();
        } 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;
    }


}

Class ManagerComputer

import android.app.Fragment;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

/**
 * Created by Karol on 20.12.2017.
 */

public class managerComputer extends Fragment{
    View myView;
    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) {
        myView = inflater.inflate(R.layout.manager_power,container,false);
        return myView;
    }
}

content_main.xml 包含(部分):

 <FrameLayout
        android:id="@+id/content_frame"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
    </FrameLayout>

错误代码:

E/FragmentManager: No view found for id 0x7f080038 (kowalski.karol.dot.com.myapplication:id/content_frame) for fragment managerComputer{ad75886 #1 id=0x7f080038}
E/FragmentManager: Activity state:
E/FragmentManager:   Local Activity c2e4854 State:
E/FragmentManager:     mResumed=true mStopped=false mFinished=false
E/FragmentManager:     mChangingConfigurations=false
E/FragmentManager:     mCurrentConfig={0 1.0 showBtnBg = 0 260mcc2mnc pl_PL ldltr sw360dp w360dp h616dp 240dpi nrml long port finger -keyb/v/h -nav/h s.5}
E/FragmentManager:     mMultiWindowStyle=MultiWindowStyle{type=0, zone=ZONE_UNKNOWN, option=0x00000000, bounds=null, isNull=false, isolatedCenterPoint=Point(0, 0), scale=0.0, specificTaskId=-1}
E/FragmentManager:     mLoadersStarted=true
E/FragmentManager:     Active Fragments in 84db547:
E/FragmentManager:       #0: ReportFragment{23c0174 #0 android.arch.lifecycle.LifecycleDispatcher.report_fragment_tag}
E/FragmentManager:         mFragmentId=#0 mContainerId=#0 mTag=android.arch.lifecycle.LifecycleDispatcher.report_fragment_tag
E/FragmentManager:         mState=5 mIndex=0 mWho=android:fragment:0 mBackStackNesting=0
E/FragmentManager:         mAdded=true mRemoving=false mResumed=true mFromLayout=false mInLayout=false
E/FragmentManager:         mHidden=false mDetached=false mMenuVisible=true mHasMenu=false
E/FragmentManager:         mRetainInstance=false mRetaining=false mUserVisibleHint=true
E/FragmentManager:         mFragmentManager=
E/FragmentManager: FragmentManager{84db547 in HostCallbacks{fb6269d}}
E/FragmentManager:         mHost=android.app.Activity$HostCallbacks@fb6269d
E/FragmentManager:         Child FragmentManager{ac07712 in ReportFragment{23c0174}}:
E/FragmentManager:           FragmentManager misc state:
E/FragmentManager:             mHost=android.app.Activity$HostCallbacks@fb6269d
E/FragmentManager:             mContainer=android.app.Fragment$1@2265ee3
E/FragmentManager:             mParent=ReportFragment{23c0174 #0 android.arch.lifecycle.LifecycleDispatcher.report_fragment_tag}
E/FragmentManager:             mCurState=5 mStateSaved=false mDestroyed=false
E/FragmentManager:       #1: managerComputer{ad75886 #1 id=0x7f080038}
E/FragmentManager:         mFragmentId=#7f080038 mContainerId=#7f080038 mTag=null
E/FragmentManager:         mState=0 mIndex=1 mWho=android:fragment:1 mBackStackNesting=0
E/FragmentManager:         mAdded=true mRemoving=false mResumed=false mFromLayout=false mInLayout=false
E/FragmentManager:         mHidden=false mDetached=false mMenuVisible=true mHasMenu=false
E/FragmentManager:         mRetainInstance=false mRetaining=false mUserVisibleHint=true
E/FragmentManager:         mFragmentManager=FragmentManager{84db547 in HostCallbacks{fb6269d}}
E/FragmentManager:         mHost=
E/FragmentManager: android.app.Activity$HostCallbacks@fb6269d
E/FragmentManager:     Added Fragments:
E/FragmentManager:       #0: ReportFragment{23c0174 #0 android.arch.lifecycle.LifecycleDispatcher.report_fragment_tag}
E/FragmentManager:       #1: managerComputer{ad75886 #1 id=0x7f080038}
E/FragmentManager:     FragmentManager misc state:
E/FragmentManager:       mHost=android.app.Activity$HostCallbacks@fb6269d
E/FragmentManager:       mContainer=android.app.Activity$HostCallbacks@fb6269d
E/FragmentManager:       mCurState=5 mStateSaved=false mDestroyed=false
E/FragmentManager:   ViewRoot:
E/FragmentManager:     mAdded=true mRemoved=false
E/FragmentManager:     mConsumeBatchedInputScheduled=true
E/FragmentManager:     mConsumeBatchedInputImmediatelyScheduled=false
E/FragmentManager:     mPendingInputEventCount=0
E/FragmentManager:     mProcessInputEventsScheduled=false
E/FragmentManager:     mTraversalScheduled=true (barrier=35)
E/FragmentManager:     android.view.ViewRootImpl$NativePreImeInputStage: mQueueLength=0
E/FragmentManager:     android.view.ViewRootImpl$ImeInputStage: mQueueLength=0
E/FragmentManager:     android.view.ViewRootImpl$NativePostImeInputStage: mQueueLength=0
E/FragmentManager:   Choreographer:
E/FragmentManager:     mFrameScheduled=true
E/FragmentManager:     mLastFrameTime=73702619 (20 ms ago)
E/FragmentManager:   View Hierarchy:
E/FragmentManager:     
E/FragmentManager: com.android.internal.policy.PhoneWindow$DecorView{5384f13 V.E...... R.....ID 0,0-540,960}
E/FragmentManager:       android.widget.LinearLayout{1d1c0e0 V.E...... ......ID 0,0-540,960}
E/FragmentManager:         android.view.ViewStub{c538f99 G.E...... ......I. 0,0-0,0 #1020489 android:id/action_mode_bar_stub}
E/FragmentManager:         android.widget.FrameLayout{eaaf25e V.E...... ......ID 0,0-540,960}
E/FragmentManager:           android.support.v7.widget.FitWindowsLinearLayout{756063f V.E...... ......ID 0,0-540,960 #7f08000a app:id/action_bar_root}
E/FragmentManager:             android.support.v7.widget.ViewStubCompat{9420b0c G.E...... ......I. 0,0-0,0 #7f080015 app:id/action_mode_bar_stub}
E/FragmentManager:             android.support.v7.widget.ContentFrameLayout{7c48c55 V.E...... ......ID 0,0-540,960 #1020002 android:id/content}
E/FragmentManager:               android.support.v4.widget.DrawerLayout{2fd66a VFED..... ......ID 0,0-540,960 #7f080044 app:id/drawer_layout}
E/FragmentManager:                 android.support.design.widget.CoordinatorLayout{7aec75b V.E...... ........ 0,36-540,960}
E/FragmentManager:                   
E/FragmentManager: android.support.design.widget.AppBarLayout{2cec96f V.E...... ........ 0,0-540,84}
E/FragmentManager:                     android.support.v7.widget.Toolbar{18acbf8 V.E...... ........ 0,0-540,84 #7f0800ba app:id/toolbar}
E/FragmentManager:                       android.support.v7.widget.AppCompatTextView{c0e98d1 V.ED..... ........ 108,21-309,62}
E/FragmentManager:                       android.support.v7.widget.AppCompatImageButton{76cef36 VFED..C.. ........ 0,0-84,84}
E/FragmentManager:                       android.support.v7.widget.ActionMenuView{4087e37 V.E...... ........ 480,0-540,84}
E/FragmentManager:                         android.support.v7.widget.ActionMenuPresenter$OverflowMenuButton{39cafa4 VFED..CL. ........ 0,6-60,78}
E/FragmentManager:                   android.support.design.widget.FloatingActionButton{e45c67c VFED..C.. ........ 432,816-516,900 #7f08004f app:id/fab}
E/FragmentManager:                 android.support.design.widget.NavigationView{412f10d V.ED..... ......I. 0,0-420,960 #7f080072 app:id/nav_view}
E/FragmentManager:                   
E/FragmentManager: android.support.design.internal.NavigationMenuView{e90c8c2 VFED.V... .F....I. 0,0-420,960 #7f080042 app:id/design_navigation_view}
E/FragmentManager:                     android.widget.LinearLayout{8dcc6d3 V.E...... ........ 0,0-420,276 #7f080073 app:id/navigation_header_container}
E/FragmentManager:                       android.widget.LinearLayout{4c72210 V.E...... ........ 0,0-420,264}
E/FragmentManager:                         android.support.v7.widget.AppCompatImageView{d8a9109 V.ED..... ........ 24,86-96,170 #7f08005c app:id/imageView}
E/FragmentManager:                         android.support.v7.widget.AppCompatTextView{226af0e V.ED..... ........ 24,170-396,211}
E/FragmentManager:                         android.support.v7.widget.AppCompatTextView{6a2fd2f V.ED..... ........ 24,211-372,240 #7f0800b2 app:id/textView}
E/FragmentManager:                     android.support.design.internal.NavigationMenuItemView{de44f3c VFED..C.. ...P.... 0,276-420,348}
E/FragmentManager:                       android.support.v7.widget.AppCompatCheckedTextView{ec234c5 V.ED..... ...P.... 24,0-396,72 #7f080041 app:id/design_menu_item_text}
E/FragmentManager:                       android.view.ViewStub{a22ae1a G.E...... ...P..I. 0,0-0,0 #7f080040 app:id/design_menu_item_action_area_stub}
E/FragmentManager:                     android.support.design.internal.NavigationMenuItemView{3ec3d4b VFED..C.. ........ 0,348-420,420}
E/FragmentManager:                       android.support.v7.widget.AppCompatCheckedTextView{9452328 V.ED..... ........ 24,0-396,72 #7f080041 app:id/design_menu_item_text}
E/FragmentManager:                       android.view.ViewStub{c165841 G.E...... ......I. 0,0-0,0 #7f080040 app:id/design_menu_item_action_area_stub}
E/FragmentManager:                     android.support.design.internal.NavigationMenuItemView{7ad91e6 VFED..C.. ........ 0,420-420,492}
E/FragmentManager:                       android.support.v7.widget.AppCompatCheckedTextView{53f6327 V.ED..... ........ 24,0-396,72 #7f080041 app:id/design_menu_item_text}
E/FragmentManager:                       android.view.ViewStub{35d49d4 G.E...... ......I. 0,0-0,0 #7f080040 app:id/design_menu_item_action_area_stub}
E/FragmentManager:                     
E/FragmentManager: android.support.design.internal.NavigationMenuItemView{92f377d VFED..C.. ........ 0,492-420,564}
E/FragmentManager:                       android.support.v7.widget.AppCompatCheckedTextView{3b0e672 V.ED..... ........ 24,0-396,72 #7f080041 app:id/design_menu_item_text}
E/FragmentManager:                       android.view.ViewStub{6b50ac3 G.E...... ......I. 0,0-0,0 #7f080040 app:id/design_menu_item_action_area_stub}
E/FragmentManager:                     android.widget.FrameLayout{a2f2f40 V.E...... ........ 0,564-420,578}
E/FragmentManager:                       android.view.View{1fcce79 V.ED..... ........ 0,12-420,14}
E/FragmentManager:                     android.support.v7.widget.AppCompatTextView{a22f7be V.ED..... ........ 0,578-420,650}
E/FragmentManager:                     android.support.design.internal.NavigationMenuItemView{353901f VFED..C.. ........ 0,650-420,722}
E/FragmentManager:                       android.support.v7.widget.AppCompatCheckedTextView{777ff6c V.ED..... ........ 24,0-396,72 #7f080041 app:id/design_menu_item_text}
E/FragmentManager:                       
E/FragmentManager: android.view.ViewStub{772d935 G.E...... ......I. 0,0-0,0 #7f080040 app:id/design_menu_item_action_area_stub}
E/FragmentManager:                     android.support.design.internal.NavigationMenuItemView{e12d1ca VFED..C.. ........ 0,722-420,794}
E/FragmentManager:                       android.support.v7.widget.AppCompatCheckedTextView{62b0f3b V.ED..... ........ 24,0-396,72 #7f080041 app:id/design_menu_item_text}
E/FragmentManager:                       android.view.ViewStub{19ba658 G.E...... ......I. 0,0-0,0 #7f080040 app:id/design_menu_item_action_area_stub}
E/FragmentManager:   Looper (main, tid 1) {c04d3b1}
E/FragmentManager:     Message 0: { when=-15ms barrier=35 }
E/FragmentManager:     Message 1: { when=+36ms callback=android.view.View$UnsetPressedState target=android.view.ViewRootImpl$ViewRootHandler }
E/FragmentManager:     Message 2: { when=+730ms callback=android.view.View$ScrollabilityCache target=android.view.ViewRootImpl$ViewRootHandler }
E/FragmentManager:     (Total messages: 3, polling=false, quitting=false)
E/FragmentManager:   Local FragmentActivity c2e4854 State:
E/FragmentManager:     mCreated=truemResumed=true mStopped=false mReallyStopped=false
E/FragmentManager:     mLoadersStarted=true
E/FragmentManager:   
E/FragmentManager: FragmentManager misc state:
E/FragmentManager:     mHost=android.support.v4.app.FragmentActivity$HostCallbacks@2744096
E/FragmentManager:     mContainer=android.support.v4.app.FragmentActivity$HostCallbacks@2744096
E/FragmentManager:     mCurState=5 mStateSaved=false mDestroyed=false
D/AndroidRuntime: Shutting down VM
E/AndroidRuntime: FATAL EXCEPTION: main
                  Process: kowalski.karol.dot.com.myapplication, PID: 769
                  java.lang.IllegalArgumentException: No view found for id 0x7f080038 (kowalski.karol.dot.com.myapplication:id/content_frame) for fragment managerComputer{ad75886 #1 id=0x7f080038}
                      at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:976)
                      at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:1164)
                      at android.app.BackStackRecord.run(BackStackRecord.java:793)
                      at android.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1557)
                      at android.app.FragmentManagerImpl$1.run(FragmentManager.java:488)
                      at android.os.Handler.handleCallback(Handler.java:739)
                      at android.os.Handler.dispatchMessage(Handler.java:95)
                      at android.os.Looper.loop(Looper.java:148)
                      at android.app.ActivityThread.main(ActivityThread.java:7331)
                      at java.lang.reflect.Method.invoke(Native Method)
                      at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230)
                      at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120)

我试图重新启动android studio,再次创建一个类,但仍然没有。我正在停止一个toutorial,我不知道为什么这不起作用。 我会很高兴任何提示!

由于

1 个答案:

答案 0 :(得分:0)

如果您发现单击if (id == R.id.nav_power)但没有此ID的视图。仔细检查值是否存在。如果没有在R.id.nav_power

按钮的主布局布局中创建一个