1

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

}
Vladimir
  • 229
  • 2
  • 16

4 Answers4

2

use equals(), not ==

if(tentId.equals("1"))
Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
pskink
  • 23,874
  • 6
  • 66
  • 77
1

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");
Community
  • 1
  • 1
zmbq
  • 38,013
  • 14
  • 101
  • 171
1

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);
        }
Shayan Pourvatan
  • 11,898
  • 4
  • 42
  • 63
0

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

}
Shailendra Madda
  • 20,649
  • 15
  • 100
  • 138