Android setText always crash - android

I'm using Android Studio to Create a Default Navigation Drawer Activity.
And I just do "setText", my app always crashed.
Here is my code.
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mNavigationDrawerFragment = (NavigationDrawerFragment)
getSupportFragmentManager().findFragmentById(R.id.navigation_drawer);
mTitle = getTitle();
// Set up the drawer.
mNavigationDrawerFragment.setUp(
R.id.navigation_drawer,
(DrawerLayout) findViewById(R.id.drawer_layout));
TextView t1=(TextView)findViewById(R.id.textView);
t1.setText("Test");
}

You should check is there any R.id.textView in your activity_main.xml. If your TextView is nested inside another view, you should do something like this:
TextView t1=(TextView) view.findViewById(R.id.textView);
There is also a possibility, this TextView is inside your fragment and your fragment doesn't exists when you are trying to find this TextView by id.

Related

how to set backspace button in place ActionbarDrawerToggle to navigate to previous fragments?

I am calling fragments from Main_activity.java and also calling fragments from other fragments but i want to show backspace(<-) arrow button when any fragment is loaded(either from Main_activity or any other fragments) instead of ActionBarDrawerToggle. how can I achieve this?
Main Activity code
public class MainActivity extends AppCompatActivity {
NavigationView nav;
ActionBarDrawerToggle toggle;
DrawerLayout drawerLayout;
Toolbar toolbar;
ImageView homeInToolbar;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
toolbar=findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
nav=findViewById(R.id.navigationview);
drawerLayout=findViewById(R.id.drawer);
// homeInToolbar=findViewById(R.id.homeInToolbar);
toggle=new ActionBarDrawerToggle(this,drawerLayout,toolbar,R.string.open,R.string.close);
drawerLayout.addDrawerListener(toggle);
toggle.syncState();
FragmentManager manager=getSupportFragmentManager();
nav.setCheckedItem(R.id.menu_home);
FragmentTransaction transaction=manager.beginTransaction();
manager.beginTransaction().replace(R.id.framelayout,new HomeFragment()).commit();}
Main_Activity where the toogle is shown and after click on students, the image should look like
Add these 2 lines after setSupportActionBar(toolbar);
setSupportActionBar(toolbar);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setDisplayShowHomeEnabled(true);
This works for me.
In main_activity from where I calling the fragment, I have disabled toggle as shown in below code.
public void onClick(View v) {
toggle.setHomeAsUpIndicator(R.drawable.ic_back);
toggle.setDrawerIndicatorEnabled(false);
getActivity().getSupportFragmentManager().beginTransaction().replace(R.id.framelayout,new FeeFragment(toggle)).addToBackStack(null).commit();
}
});
and where ever necessary, I can enable toggle by below code.
toggle.setDrawerIndicatorEnabled(true);

How to hide drawer once the applilication starts?

I am developing an android application with a navigation drawer. Everything works fine. except that when I start my application the drawer appears automatically. Here what I tried to fix this issue:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mNavigationDrawerFragment = (NavigationDrawerFragment) getFragmentManager()
.findFragmentById(R.id.navigation_drawer);
mTitle = getTitle();
// Set up the drawer.
DrawerLayout mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
mNavigationDrawerFragment.setUp(R.id.navigation_drawer,
mDrawerLayout);
// as a solution I added this if-statement
if(mDrawerLayout.isDrawerOpen(GravityCompat.START)) {
mDrawerLayout.closeDrawers();
}
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
}
But it is not working! . How to hide it once the applilication starts ?
mDrawerLayout.closeDrawer(GravityCompat.START);
Hi u can use following code:
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
drawer.closeDrawer(GravityCompat.START);

How can I make the top logo in a navigation drawer act properly as a button?

I am trying to make the top logo of the nav drawer act as a button that takes me back to the first fragment of the app, but I can't seem to make the button work at all, not even for simple tasks, such as a toast. The app crashes upon opening the app, so I don't even get to try using the button.
This is the error I get: "Attempt to invoke virtual method 'void android.widget.ImageButton.setOnClickListener(android.view.View$OnClickListener)' on a null object reference"
The logo is declared as an ImageButton in XML and this is the java code:
public class MainActivity extends AppCompatActivity
implements NavigationView.OnNavigationItemSelectedListener {
public ImageButton logo;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//Set the initial fragment
MainFragment mainFragment = new MainFragment();
FragmentTransaction fragmentTransaction = getFragmentManager().beginTransaction();
fragmentTransaction.replace(R.id.fragment_container, mainFragment);
fragmentTransaction.commit();
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
drawer.setDrawerListener(toggle);
toggle.syncState();
NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
navigationView.setNavigationItemSelectedListener(this);
logo = (ImageButton) findViewById(R.id.main_icon);
logo.setOnClickListener(new View.OnClickListener(){
#Override
public void onClick(View v){
Toast toast = Toast.makeText(getApplicationContext(), "works", Toast.LENGTH_SHORT);
toast.show();
}
});
}
Assuming your ImageButton is in the NavigationView's header, and that the NavigationView has only one header View, you can call findViewById() on that.
logo = (ImageButton) navigationView.getHeaderView(0).findViewById(R.id.main_icon);
Alternatively, since the View is an ImageButton, you could set an onClick attribute on the layout entry, and move your onClick() method to the Activity.
<ImageButton android:id="#+id/main_icon"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/your_logo"
android:onClick="onClick" />
If you choose this option, make sure to remove the setOnClickListener() call.
public class MainActivity extends AppCompatActivity
implements NavigationView.OnNavigationItemSelectedListener {
...
public void onClick(View v) {
Toast toast = Toast.makeText(getApplicationContext(), "works", Toast.LENGTH_SHORT);
toast.show();
}
}
you have to create view. Use this
View view = navigationView.inflateHeaderView(R.layout.nav_header_main);
logo = (ImageButton) view.findViewById(R.id.main_icon);

Android Studio generated Navigation Drawer's Header

I've generated a navigation drawer using Android Studio by New -> Activity -> Navigation Drawer Activity. As a recent feature of Android Studio I didn't find enough examples through Google to access TextView in the header. I want to dynamically change the header title.
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
TextView tv = (TextView) drawer.findViewById(R.id.tvNavHeader);
In onCreate does not help me.
The NavigationView has a bug in v23.1.0. You can't access to the views contained in the HeaderView in onCreate method.
A workaround is to inflate the HeaderView and add it programatically in the onCreate method like this:
public class MainActivity extends AppCompatActivity {
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main_activity);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
View header = LayoutInflater.from(this).inflate(R.layout.nav_header_view, null);
navigationView.addHeaderView(header);
TextView tv = (TextView) header.findViewById(R.id.tvNavHeader);
}
}
There is no longer need to inflate the HeaderView anymore.
View header = navigationView.getHeaderView(0);
TextView text = (TextView) header.findViewById(R.id.textView);
This works for one header. If you have several, just browse the google issue provided by Mattia.

Actionbar activity and fragment manager

I'm using an ActioBarActivity in order to use the new toolbar from appcompact v21.
But, when using this type of activity, when I replace a fragment with de parameter addToBackStack(), it doesn't work.
When the user presses the back button, the role activity is destroyed.
This is my onCreate()
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_container);
Toolbar mToolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(mToolbar);
mNavigationDrawerFragment = (NavigationDrawerFragment)
getFragmentManager().findFragmentById(R.id.navigation_drawer);
mTitle = getTitle();
// Set up the drawer.
mNavigationDrawerFragment.setUp(
R.id.navigation_drawer,
(DrawerLayout) findViewById(R.id.drawer_layout), mToolbar,getSupportActionBar());
if (savedInstanceState == null){
getFragmentManager().beginTransaction()
.replace(R.id.container, ListaFragment.newInstance(""))
.commit();
}
}
And this is the click that replaces the fragment
#Override
public void helpClick(int stringResource) {
getFragmentManager().beginTransaction()
.replace(R.id.container, HelpFragment.newInstance(stringResource))
.addToBackStack(null)
.commit();
}
I don't know if there is an compability erro when using the import android.support.v7.app.ActionBarActivity with the import android.app.Fragment;
Had the same problem. You should use classes from the support library: android.support.v4.app.FragmentManager and android.support.v4.app.Fragment instead of default ones.
So you have to replace getFragmentManager() with the getSupportFragmentManager() and import android.app.Fragment; with import android.support.v4.app.Fragment;.
Also your fragments should extend android.support.v4.app.Fragment class.

Categories

Resources