setContentView not working when click button

Pease help me with my code.

When i click any button, button1, button2 or button3 opens new activity, but layout is empty, without any text's and others.

Code of activity from calling the new activity:

package com.novator.inweld;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

public class TentsCatalog extends Activity implements OnClickListener
{
    private Button button1;
    private Button button2;
    private Button button3;

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

        button1 = (Button)findViewById(R.id.button1);
        button2 = (Button)findViewById(R.id.button2);
        button3 = (Button)findViewById(R.id.button3);

        button1.setOnClickListener(this);
        button2.setOnClickListener(this);
        button3.setOnClickListener(this);
    }

    @Override
    public void onClick(View view)
    {
        if(view == button1)
        {
            Intent intent = new Intent(this, TentPage.class);
            intent.putExtra("tentId", "1");
            startActivity(intent);
        }

        if(view == button2)
        {
            Intent intent = new Intent(this, TentPage.class);
            intent.putExtra("tentId", "2");
            startActivity(intent);
        }

        if(view == button3)
        {
            Intent intent = new Intent(this, TentPage.class);
            intent.putExtra("tentId", "3");
            startActivity(intent);
        }
    }
}

Code of my new activity:

package com.novator.inweld;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;

public class TentPage extends Activity
{
    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);

        Intent intent= getIntent();
        String tentId = intent.getStringExtra("tentId");

        if(tentId == "1")
        {
            setContentView(R.layout.tent1);
        }

        if(tentId == "2")
        {
            setContentView(R.layout.tent2);
        }

        if(tentId == "3")
        {
            setContentView(R.layout.tent3);
        }
    }

}

use equals(), not ==

if(tentId.equals("1"))

setContentView doesn't attach click event for a button, setContentView issues in Android. GitHub Gist: instantly share nextPage = (​Button) findViewById(R.id.button4);. nextPage. public void onClick(View v) {. When the user clicks a button, the Button object receives an on-click event. To make click event work add android:onClick attribute to the Button element in your XML layout. The value for this attribute must be the name of the method you want to call in response to a click event.


change onClick method with:

   @Override
public void onClick(View v)
{
    Intent intent = new Intent(this, TentPage.class);
    switch (v.getId()) {
    case R.id.button1:
        intent.putExtra("tentId", "1");
        startActivity(intent);
        break;
    case R.id.button2:
        intent.putExtra("tentId", "2");
        startActivity(intent);
        break;
    case R.id.button3:
        intent.putExtra("tentId", "3");
        startActivity(intent);
        break;


    default:
        break;
    }

and use tentId.equals(""); for String check you must use equals() method and for number value use == like:

if(tentId.equals("1"))
        {
            setContentView(R.layout.tent1);
        }

        if(tentId.equals("2"))
        {
            setContentView(R.layout.tent2);
        }

        if(tentId.equals("3"))
        {
            setContentView(R.layout.tent3);
        }

setContentView issues in Android · GitHub, When I redid the recipe class I did not change much, but now when I click the recipe button on the main menu, the screen will not display the  Pease help me with my code. When i click any button, button1, button2 or button3 opens new activity, but layout is empty, without any text's and others. Code of activity from calling the new acti


Java is a b*%$#ch... Use equals():

if(tentId.equals("1")) ...

Also, according to this answer, Android supports Java 1.7's strings in switch statements, meaning you can rewrite your code in a tidier fashion:

switch(tendId) {
    case "1": ...
    case "2": ...
    case "3": ...
}

Keep in mind that the simplest solution would be to pass tendId as an int and not a string:

 intent.putExtra("tendId", 1);
 int tendId = intent.getIntExtra("tendId");

Android Activity SetContentView() Not Working? Help., I have an error in my android code..my setContentView is not working. my code on resources,then click on layout,then you see a "file_name.xml" if not rename it to I have this code: Button btnLaunch; btnLaunch=(Button)findViewById(R.id. setContentView() fails to create the layout causing the app to crash This is the layout file which I have set using the setContentView() in onCreate() method of Android. It is a long code use to create a Tic Tac Toe.


Replace your TentPage like:

package com.novator.inweld;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;

public class TentPage extends Activity
{
    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);

        Intent intent= getIntent();
        String tentId = intent.getStringExtra("tentId");

        if(tentId.equals("1"))
        {
            setContentView(R.layout.tent1);
        }

        if(tentId.equals("2"))
        {
            setContentView(R.layout.tent2);
        }

        if(tentId.equals("3"))
        {
            setContentView(R.layout.tent3);
        }
    }

}

setContentView not working in my android code, When i click any button, button1, button2 or button3 opens new activity, but layout is empty, without any text's and others. Code of activity from calling the new  I want to open a new activity page/window while click on a button. setContentView(R.layout default url and button click event is not working.


setContentView not working when click button, Cannot call Other Layout from Another Layout by Button Click Then, when I try to call layRegistration layout from layLogIn by Button click, it not working. our view from the "main" layout resource SetContentView (Resource. In Xamarin.Android I can write: SetContentView(Resource.Layout.Main); for set the activity content from Main.axml This site uses cookies for analytics, personalized content and ads. By continuing to browse this site, you agree to this use.


Cannot call Other Layout from Another Layout by Button Click , SendSMSAct activity on button click.So when you are coming to SendSMSAct activity you are not setting SetContentView(Resource.Layout. It must be switched to another layout, I tested in debug mode - execution doesn't go there at all.


How to show other layouts via button click? My code won't work , onCreate(savedInstanceState); setContentView(R.layout.main); Button btn1 http://developer.android.com/reference/android/widget/Button.html[^]. Permalink. Posted 21-Dec-12 Need to use android:onClick attribute to switch between layouts(Listener doesn't work in this case) Great thanks to TorstenH! If the mouse’s left-click button doesn’t work, only sometimes responds, accidentally “unclicks” as you drag, misclicks, or double-clicks when you click once, that’s a pretty good sign there’s something wrong with the hardware in the left-click button itself.