gpt4 book ai didi

java - 单击 ActionBar 菜单时显示 fragment 返回 null

转载 作者:太空宇宙 更新时间:2023-11-04 14:12:28 27 4
gpt4 key购买 nike

我想在单击操作栏菜单项时显示/隐藏 fragment 。但是当我点击时我的应用程序给我错误。首次运行时出现 fragment 。当我想隐藏 fragment 应用程序时给出空错误。

如何解决这个问题?

现在谢谢。

Logcat

01-26 03:48:40.942    2295-2295/test.sy.myapplication E/AndroidRuntime﹕ FATAL EXCEPTION: main
java.lang.NullPointerException
at android.app.BackStackRecord.run(BackStackRecord.java:661)
at android.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1435)
at android.app.FragmentManagerImpl$1.run(FragmentManager.java:441)
at android.os.Handler.handleCallback(Handler.java:730)
at android.os.Handler.dispatchMessage(Handler.java:92)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:5103)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:525)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
at dalvik.system.NativeStart.main(Native Method)

LayerList.Java

package test.sy.myapplication;

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


public class LayerList extends Fragment {

@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View mView = inflater.inflate(R.layout.layerlist,container,false);
return mView;

}
}

MainActivity.Java

package test.sy.myapplication;

import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;


public class MainActivity extends ActionBarActivity {

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


@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.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.
switch (item.getItemId()) {
case R.id.action_settings:
return true;
case R.id.showsth:

FragmentManager fm = getFragmentManager();
FragmentTransaction ft = getFragmentManager().beginTransaction();
ft.setCustomAnimations(android.R.animator.fade_in,
android.R.animator.fade_out);
ft.show(fm.findFragmentById(R.id.fragment));
ft.commit();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
}

最佳答案

ft.show(fm.findFragmentById(R.id.fragment));

这条线导致了问题

fm.findFragmentById(R.id.fragment) 返回 null。

通过这一行,您尝试在 layout/activity_main 中查找先前添加到 R.id.fragment 中的 fragment 。如果之前没有添加或替换fragment,很明显会返回null AMK

关于java - 单击 ActionBar 菜单时显示 fragment 返回 null,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28147041/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com