我想在我的工具欄中找回按鈕,但我的應用崩潰了。 這是我MainActivity.java:從工具欄管理工具欄中的按鈕從android中的片段
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)
可以顯示錯誤日誌? –
運行該程序,當它崩潰時,單擊「Android監視器」,然後打開logcat。將您的錯誤日誌複製並粘貼到您的答案編輯中,以便我們可以知道錯誤的確切位置。 –
@AalapPatel我做了 – OverflowStacker