hi all I have been stuck with this getFragment by id returning null
xml file
<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>
MainActivity.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);
}
}
This is the exception thrown in the logcat it refers to 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)
I don't know why it returns null . can anyone have idea why this happens? thanks! I included the whole mainActivity.java
I solved my problem. And for those who would encounter the same problem in the future here is the solution.
The problem wasn't because of the xml file or the getSupportManager.findFragmentById()
method. With some debugging, I tried putting if else and checking if getSupportFragment is != null
.
Here's the fix.
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();
}
credits to @njzk2 as well.