从android中的片段管理工具栏中的后退按钮

时间:2017-08-15 19:09:47

标签: java android xml android-fragments android-toolbar

我想在我的工具栏中使用后退键,但我的应用程序崩溃了。 这是我的MainActivity.java:

package com.penta.games.waslehrernichtdrfen;

import android.os.Bundle;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;

public class MainActivity extends AppCompatActivity {
    boolean status = false;
    Button levelauswahl;

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

        levelauswahl = (Button)findViewById(R.id.levelauswahl);
        levelauswahl.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                FragmentManager fragmentManager = getSupportFragmentManager();
                FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();

                    LevelAuswahl levelAuswahl = new LevelAuswahl();
                    fragmentTransaction.add(R.id.fragment_container, levelAuswahl);
                    fragmentTransaction.addToBackStack(null);
                    fragmentTransaction.commit();


            }
        });


    }

}

这是我的片段:

package com.penta.games.waslehrernichtdrfen;


import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v7.widget.Toolbar;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

public class LevelAuswahl extends Fragment {
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment

        View rootView = inflater.inflate(R.layout.level_auswahl, container, false);
        Toolbar toolbar = (Toolbar) getView().findViewById(R.id.toolbar);
        toolbar.setTitle("Title");
        toolbar.setNavigationIcon(R.drawable.backarrow1);
        toolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
               getActivity().onBackPressed();
            }
        });
        return rootView;
    }
}

我希望如果我打开片段,我可以通过单击工具栏中的后退箭头返回MainActivity。 但是如果我试图打开片段,我的应用程序就崩溃了。

这是我的fragment.xml:

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/levelauswahl"
    android:background="#ffffff"
    tools:context="com.penta.games.waslehrernichtdrfen.LevelAuswahl">


    <android.support.v7.widget.Toolbar
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="#e70000"
        android:id="@+id/toolbar"
        android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
        app:title="Was Lehrer nicht dürfen" />
    </RelativeLayout>

工具栏应始终存在,但只有在我打开片段时才会显示后退按钮。 我正在编程java一个星期,所以请原谅我的小知识。

这是我的LogCat的输出:

08-15 22:44:47.799 2086-2086/? E/AndroidRuntime: FATAL EXCEPTION: main
                                                 Process: com.penta.games.waslehrernichtdrfen, PID: 2086
                                                 java.lang.NullPointerException
                                                     at com.penta.games.waslehrernichtdrfen.LevelAuswahl.onCreateView(LevelAuswahl.java:18)
                                                     at android.support.v4.app.Fragment.performCreateView(Fragment.java:2192)
                                                     at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1299)
                                                     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.FragmentManagerImpl$1.run(FragmentManager.java:710)
                                                     at android.os.Handler.handleCallback(Handler.java:733)
                                                     at android.os.Handler.dispatchMessage(Handler.java:95)
                                                     at android.os.Looper.loop(Looper.java:157)
                                                     at android.app.ActivityThread.main(ActivityThread.java:5356)
                                                     at java.lang.reflect.Method.invokeNative(Native Method)
                                                     at java.lang.reflect.Method.invoke(Method.java:515)
                                                     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1265)
                                                     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1081)
                                                     at dalvik.system.NativeStart.main(Native Method)

1 个答案:

答案 0 :(得分:0)

我发现了错误。在你的片段的oncreateView方法

中试试这个
    View rootView = inflater.inflate(R.layout.level_auswahl, container, false);
    //the view here should be the one which inflates the layout which contains your toolbar. 
    //I assume it is rootview which u mentioned just above this line. 
    Toolbar toolbar = (Toolbar) rootView.findViewById(R.id.toolbar);
    toolbar.setTitle("Title");
    toolbar.setNavigationIcon(R.drawable.backarrow1);
    toolbar.setNavigationOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

           //achieve your behaviour here
        }
    });
    return rootView;