0

I am a beginner in Android. I am trying to start a new activity via an intent inside an onclick event. This works fine in emulator. But when I try it in a real device it doesn't work and the application goes to the main screen again. No errors are shown in the logcat.

This is where I call the startActivity method.

relativeAbout.setOnClickListener(new View.OnClickListener() {


        public void onClick(View v) {
            // TODO Auto-generated method stub
            try {
                Class OurClass = Class
                        .forName("com.wad.tourismguide.AboutCity");
                Intent newIntent = new Intent(getApplicationContext(),
                        OurClass);


                newIntent.putExtra("name", name);
                newIntent.putExtra("detail", detail);
                newIntent.putExtra("image", main);
                startActivity(newIntent);



                System.out.println("intent starting");
            } catch (ClassNotFoundException e) {
                // TODO: handle exception
                e.printStackTrace();
            }
        }
    });

The new activity is shown below.

    public class AboutCity extends Activity {
    TextView cityName;
    ImageView image;
    TextView detailText;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);

        setContentView(R.layout.aboutcity);

        cityName = (TextView)findViewById(R.id.tvDetailCityName);
        image = (ImageView)findViewById(R.id.ivDetailImage);
        detailText = (TextView)findViewById(R.id.tvDetailText);

        String name = getIntent().getStringExtra("name");
        System.out.println("city name"+ name);
        String detail = getIntent().getStringExtra("detail");
        System.out.println("city detail"+ detail);
        Bitmap b= (Bitmap)getIntent().getParcelableExtra("image");
        System.out.println(detail);
        cityName.setText(name);
        detailText.setText(detail);
        image.setImageBitmap(b);
    }


}

As I explained earlier, this works fine in the emulator. But it doesn't work in the real device. I can't find where I am going wrong. Can someone please help me with this?

Paul Bellora
  • 54,340
  • 18
  • 130
  • 181
Pathfinder92
  • 117
  • 2
  • 5
  • 12
  • 1
    Post the error logcat too. And, is there a reason for using this to declare the class: `Class OurClass`? – Siddharth Lele Nov 15 '12 at 05:13
  • Are you receiving classNotFound Exception?... if your com.wad.tourismguide.AboutCity package and class names are perfect than it should work... Here you aer catching ClassNotFoundException So if the class path is wrong it will throw the exception and simply stays in the MainActivity... – Kartihkraj Duraisamy Nov 15 '12 at 05:51

3 Answers3

2

Unless there is a specific reason to use the Class OurClass = Class.forName("com.wad.tourismguide.AboutCity"); to set the target Class, the conventional way of calling another activity via Intent (with Extras) will work as advertised.

Intent newIntent = new Intent(getApplicationContext(), AboutCity.class);
newIntent.putExtra("name", name);
newIntent.putExtra("detail", detail);
newIntent.putExtra("image", main);
startActivity(newIntent);

You could also try another variant which essentially works as the code above:

Intent newIntent = new Intent();
newIntent.setClass(getApplicationContext(), AboutCity.class);
newIntent.putExtra("name", name);
newIntent.putExtra("detail", detail);
newIntent.putExtra("image", main);
startActivity(newIntent);

EDIT: After reading this: http://www.xyzws.com/Javafaq/what-does-classforname-method-do/17, I am inclined to think that the Class OurClass = Class.forName("com.wad.tourismguide.AboutCity"); is not needed at all. Someone can correct me if I am wrong on this though.

Siddharth Lele
  • 27,623
  • 15
  • 98
  • 151
2

I don't think your code is wrong but you can just simple it when call the new Intent:

Intent newIntent = new Intent(CurrentActivity.this, AboutCity.class);
newIntent.putExtra("name", name);
newIntent.putExtra("detail", detail);
newIntent.putExtra("image", main);
startActivity(newIntent);

Your code will be more legible after all and you won't need to see Exceptions (less error chances).

Brayan Neves
  • 329
  • 1
  • 15
0

Just pass to the constructor your current activity class and activity class you want to start:

Intent intent = new Intent(CurrentActivity.this, AboutCity.class);
// put extra
startActivity(intent);
Sergey Dmitriev
  • 454
  • 3
  • 13