gpt4 book ai didi

java - SupportManager.getFragmentById 返回 null

转载 作者:行者123 更新时间:2023-11-30 02:23:12 25 4
gpt4 key购买 nike

大家好,我一直被 id 返回 null 的 getFragment 困住了

xml文件

<android.support.v4.widget.DrawerLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent">

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

<fragment
android:id="@+id/navigation_drawer"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="left"
tools:layout="@layout/fragment_navigation_drawer_list" />
</android.support.v4.widget.DrawerLayout>

主 Activity .java

public class MainActivity extends ActionBarActivity {
private static final String SCREEN_NAME = "MainActivity";

@Optional @InjectView(R.id.tool_bar) Toolbar toolbar;
//@InjectView(R.id.DrawerLayout) DrawerLayout mDrawerLayout;

@Inject GamerzWikiApplication mGamerzWikiApplication;
private NavigationDrawerFragment mNavigationDrawerFragment;

android.support.v7.app.ActionBar mActionBar;
ActionBarDrawerToggle mDrawerToggable;

private ArrayList<NavigationDrawerItem> mNavigationDrawerItemList;
private DrawerLayout mDrawerLayout;

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

ButterKnife.inject(this);
ObjectGraphInjector.inject(this);

setSupportActionBar(toolbar);
setupActionBar();
setupDrawerLayout();

}


@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;
}

private void setupActionBar() {
mActionBar = getSupportActionBar();

//SetDisplayHomeAsUpEnabled => Set whether home should be displayed as an "up" affordance(objects action/ to perform).
//mActionBar.setDisplayHomeAsUpEnabled(true);

//Enable or disable the "home" button in the corner of the action bar.
mActionBar.setHomeButtonEnabled(true);
}

private void setupDrawerLayout() {

mNavigationDrawerItemList = new ArrayList<NavigationDrawerItem>();
mNavigationDrawerItemList.add(new NavigationDrawerItem(getString(R.string.title_home), "{fa-home}") {
@Override
public void onClick() {
mActionBar.setTitle(R.string.title_home);
/*
getSupportFragmentManager().beginTransaction()
.replace(R.id.container, new TopGameFragment())
.commit();
*/
}
});


mNavigationDrawerItemList.add(new NavigationDrawerItem("ユーザ情報の確認・表示", "{fa-user}") {
@Override
public void onClick() {
//startActivity(AccountSettingActivity.createIntent(getApplicationContext()));
}
});

mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
mNavigationDrawerFragment = (NavigationDrawerFragment)
getSupportFragmentManager().findFragmentById(R.id.navigation_drawer);
mNavigationDrawerFragment.setup(
R.id.navigation_drawer,
mNavigationDrawerItemList,
mDrawerLayout
);

mActionBar.setTitle(R.string.title_home);
/*
getSupportFragmentManager().beginTransaction()
.replace(R.id.container, new TopGameFragment())
.commit();*/

}

@Override
public void onAttachFragment(Fragment fragment) {
// TODO Auto-generated method stub
super.onAttachFragment(fragment);

Toast.makeText(getApplicationContext(), String.valueOf(fragment.getId()), Toast.LENGTH_SHORT).show();

}

@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);
}

这是在它引用的 logcat 中抛出的异常 mNavigationDrawerFragment = (NavigationDrawerFragment)
getSupportFragmentManager().findFragmentById(R.id.navigation_drawer);

 Caused by: java.lang.NullPointerException
at com.atfreaks.gamerzwiki.MainActivity.setupDrawerLayout(MainActivity.java:101)
at com.atfreaks.gamerzwiki.MainActivity.onCreate(MainActivity.java:54)

我不知道它为什么返回 null 。谁能知道为什么会这样?谢谢!我包含了整个 mainActivity.java

最佳答案

我解决了我的问题。对于那些将来会遇到同样问题的人来说,这里就是解决方案。

问题不是因为 xml 文件或 getSupportManager.findFragmentById() 方法。通过一些调试,我尝试放置 if else 并检查 getSupportFragment 是否为 != null

这是修复。

if(getSupportFragmentManager() != null){
mNavigationDrawerFragment = (NavigationDrawerFragment)
getSupportFragmentManager().findFragmentById(R.id.navigation_drawer);
mNavigationDrawerFragment.setup(
R.id.navigation_drawer,
mNavigationDrawerItemList,
mDrawerLayout
);
} else {
Toast.makeText(getApplicationContext(), "getSupportFragmentManager is null", Toast.LENGTH_SHORT).show();
}

也归功于@njzk2。

关于java - SupportManager.getFragmentById 返回 null,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28175947/

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