App crash on view change - android

Im trying to make the contentView change on a button click, but the app crash when i start it by some reason.
This is my onCreate method
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
b1 = (Button) findViewById(R.id.bStart);
b1.setOnClickListener((OnClickListener) mHandler);
mHandler = new View.OnClickListener() {
public void onClick(View v) {
switch(v.getId()) {
case R.id.bStart:
// it was the first button
setContentView(R.layout.cards);
break;
}
}
};}

Your approach is wrong. setContentView() method should be called only once in onCreate() method and it shouldn't be invoked by clicking the button. In addtion, findViewById() methods should be called after calling setContentView() method. If you want to replace your layout, consider using fragments or do some manipulation on the views inside the layout without calling setContentView() method again.

Related

Android Studio onClick and OnclickListener not working once changed to another Activity

I've added my acivity class in this link
Click MeI am fairly new to Android and am having difficulty trying to add a button on my second activity. I am able to place a button in my main activity and then I use it to navigate to my secondary activity (using setContentView(R.layout.)) and then I use the same 'onClick' method or even 'OnClickListener' method but the button on my second activity just wont work on another activity. Maybe i am missing something
]3
just try to do this:
public class FirstActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.first_activity);
findViewById(R.id.about_us).setOnClickListener(new
View.OnClickListener() {
#Override
public void onClick(View v) {
startActivity(new Intent(FirstActivity.this, SecondActivity.class));
}
});
}
}
and in second activity again find your button in second activity xml by id and write onClickListener for it
You need to implement two separate methods for two different buttons. I would suggest do these things in the Java code instead of XML.
You can do some thing like this:
Button button = findViewById(R.something.something);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
//perform your operation(s) here.
}
});
As I understand you try to use one layout.xml for both activities.
You need to declare method click1 in both activities, not only in first.
It means that your first activity has to have method public void click1() and the second activity has to duplicate method public void click1()
I know it's old however,
#Meikiem idea is great. When you use setContentView(View View) you are just setting the activity's
content to another view (xml), and thus not really using the other .java file which has another
onClick method defined for the second button.
Activity's setContentView(view)
You need to create an Intent and pass it along the startActivity method.
Intent Definition

How to Re Active onClick Listener in onResume Activity

I have created an Activity named BaseNavigationActivity that createsNavigationDrawer. All other activities are extending from this activity. It works fine,open the drawer on button click.
But when i go into another activity, and come back to previous Activity. The button stops working. Although visually button is still there, and if i click and drag from left side, it opens the Drawer Layout
When i debug it, i found out that it calls onRestart() and onResume() methods, but there is no code written in these method.
So do i need to add the code there as well? or is there any way to implement onClickListner in onRestart() and onResume() of Activity
onCreate()
drawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout_main);
headerLayout= (FrameLayout) findViewById(R.id.header_layout);
btnOpenDrawer= (Button) findViewById(R.id.btn_openDrawer);
homeLayout= (LinearLayout) findViewById(R.id.home_layout);
btnOpenDrawer.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
drawerLayout.openDrawer(Gravity.LEFT);
}
});
#Override
protected void onResume() {
super.onResume();
}
#Override
protected void onRestart() {
super.onRestart();
}
Create class level ClickListener like onClickListener and Use it wherever you want.
// Activate Listener
myView.setOnClickListener(onClickListener);
// DeActivate Listener
myView.setOnClickListener(null);
// Listener
View.OnClickListener onClickListener = new View.OnClickListener() {
#Override
public void onClick(View v) {
}
};
In my opinion the error is that you set your OnClickListener in the onCreate Method. The OnClickListener only gets set when you create the activity. When you switch to your second activity, the OnClickListener will get overwritten and when you go back to your first activity the OnClickListener will not be set, because the first activity was already created, and only onResume will get called.
Try to move your code from the onCreate to onResume, so the listener will get set every time the activity is moving back to the front.

Android layout loaded twice

On button click event I'm using setContentView(R.layout.activity_main); it works correctly.
When if I try to start new activity with Intent and startactivity commands it loads layout twice it looks like layout loading correctly then 1 second same layout loaded again.
Before start activity its loaded single time.
show.setOnClickListener(new View.OnClickListener()
{
#Override
public void onClick(View v)
{
String selected = spinner0.getSelectedItem().toString();
if(selected.equals("Item 2"))
{
Intent intent = new Intent(second_layout.this,MainActivity.class);
setContentView(R.layout.activity_main);
startActivity(intent);
}
}
});
I'm guessing this line is the problem.-
setContentView(R.layout.activity_main);
setContentView will just change the layout for the current activity, so you're changing the current layout to activity_main, and then you open the Intent for MainActivity class.
Just remove that line.
When you are starting a new activity, there is no need of setContentView while starting the intent.
The intent which is getting started will have the code for loading the layout. So please remove this line.
I hope, in your MainActivity.class, you will already be writing setContentView(R.layout.activity_main) and this is enough for showing the required layout. So remove this extra line which you have included while starting the intent.
You can not set your second activity layout in your first activity before starting your second activity. It will set automatically in your second activity's onCreate() method. So you should write setContentView(R.layout.activity_main); in your MainActivity's onCreate() method. Just remove it from the onClick listener.
So write in your onClick as below:
show.setOnClickListener(new View.OnClickListener()
{
#Override
public void onClick(View v)
{
String selected = spinner0.getSelectedItem().toString();
if(selected.equals("Item 2"))
{
Intent intent = new Intent(second_layout.this,MainActivity.class);
startActivity(intent);
}
}
});
And in your MainActivity you have to set your layout as below:
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
}

setcontent view not working

button.setOnClickListener(new View.OnClickListener(){
public void onClick(View v){
setContentView(R.layout.activity_chart);
}
});
Hi i have the above code wherein upon clicking a button i am trying to display them activity activity_chart. In that activity i want to display a graph. Here i am calling a method createIntent(). But my problem is that the graph is not getting plotted. Please help i am new to android.
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
createIntent();
}
public Intent createIntent()
{
...
}
Am i calling the method right.
A new Activity is called with:
startActivity(new Intent(currentActivity.this, nextActivity.class));
Then in your new Activities onCreate(Bundle savedInstance) method you can call setContentView(Layout layout); to set the new Layout.
So if you want to change the Activity when clicking on a Button you have to do the following:
button.setOnClickListener(new View.OnClickListener(){
public void onClick(View v){
startActivity(new Intent(currentActivity.this, nextActivity.class));
}
});
You are currently only changing the layout of the current Activity when clicking the button and not changing to another Activity.
I hope I understood you correctly. If not then provide me with some more code so I can try to understand what you want to do.

android button event listener does not work

I know this is the basic stuff but I couldn't figure it out. Here is my code:
public class test extends Activity{
private static final String TAG = "test";
private Button Test;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Log.v(TAG,"onCreate is called.");
this.setContentView(R.layout.main);
Test= (Button)this.findViewById(R.id.Test);
Test.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Log.v(TAG, "onClick Entered.");
// Perform action on click
}
});
setContentView(R.layout.main);
}
}
I can see the first log "OnCreate" but the button click event listener doesn't seem
to work because I can't see "OnClick Entered". Where did I do wrong?
Thanks
You are calling setContentView twice, the second time after you have set up your on click listener. This means that the view that you add the listener to is no longer visible, it is replaced with a different instance of the view. Remove the second setContentView.
A few comments / things to try:
You have your naming conventions backwards. You should be naming your activity in title case, your variables in camel case (e.g. your activity "test.java" should be Test.java, your Button Test should be Button test). That's not your problem, but just something to keep in mind.
You're calling super.onCreate() twice. I really don't know what effect that has, but it shouldn't be there. You're also calling setContentView() twice. One call to onCreate, one call to setContentView is all you should have. EDIT: Apparently three times, per Jems's comment.
In main.xml, do you have a Button with an id of test? (i.e. android:id="#+id/test")
Where is showTrafficButton defined? Are you sure that shouldn't be:
Button showTrafficButton = (Button)this.findViewById(R.id.Test);

Categories

Resources