user2609157
user2609157

Reputation:

Navigation of activities in android

Hello I am trying to learn the screen navigation in android


CopperChimneyPhotos.java

public class CopperChimneyPhotos extends Activity{

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.copper_chimney_photos);

        Button btn=(Button) findViewById(R.id.PhotoButton);

        btn.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                Intent pt=new Intent(CopperChimneyPhotos.this,CopperChimneyDesc.class);
                startActivity(pt);
            }

        });
    }
}

CopperChimneyDesc.java

public class CopperChimneyDesc extends Activity{

    private static String url = "http://url:7002/xxxx/";
    private static String url1 = "http://url:7002/xxxx/";



    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.copperchimney_desc_screen);

        //getting info from listview to display the name in TopNavigationBarCopperChimneyDescActivityName
        TextView topdisp=(TextView) findViewById(R.id.TopNavigationBarCopperChimneyDescActivityName);
        topdisp.setText(getIntent().getExtras().getString("CC_RES"));


        // Creating JSON Parser instance
        JSONObjParser jParser = new JSONObjParser();

        // getting JSON string from URL
        JSONArray json = jParser.getJSONFromUrl(url);

        // getting JSON string from URL
        JSONArray json1 = jParser.getJSONFromUrl(url1);


        try {
            for (int i = 0; i < json1.length(); i++) {
                JSONObject c = json1.getJSONObject(i);


                // Storing each json item in variable
                int id = c.getInt("_id");
                String TIME = c.getString("RestaurantTime");

                TimeMap.put(id, TIME);

            }
        } catch (JSONException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }


        try {
            for (int i = 0; i < json.length(); i++) {
                JSONObject c = json.getJSONObject(i);

                           // remaining code


            }
        } catch (JSONException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        Button BACKBUTTON=(Button) findViewById(R.id.TopNavigationBarCopperChimneyDescActivityBackButton);
        BACKBUTTON.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                Intent emp1=new Intent(CopperChimneyDesc.this,MainActivity.class);
                startActivity(emp1);
            }
        });


        Button PHOTOBUTTON=(Button) findViewById(R.id.CopperChimneyPhotosButton);

Upvotes: 1

Views: 60

Answers (2)

codeMagic
codeMagic

Reputation: 44571

You haven't added any extras here

 @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            Intent pt=new Intent(CopperChimneyPhotos.this,CopperChimneyDesc.class);
            startActivity(pt);

in CopperChimneyPhotos so getIntent().getExtras().getString("CC_RES")); will return null.

I'm not sure where you expect that value to come from but it would need to be added to the Intent that starts this Activity so something like

 @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            Intent pt=new Intent(CopperChimneyPhotos.this,CopperChimneyDesc.class);
            pt.putExtra("CC_RES", stringToAdd);
            startActivity(pt);

You also may want to check that getIntent() and getExtras() doesn't return null in your second class before assigning the value to something. You have extras when you go from MainActivity to Desc but not when you go from Photos to Desc.

Upvotes: 2

Marcin Orlowski
Marcin Orlowski

Reputation: 75629

You are getting error because of bug in CopperChimneyDesc.java, line 37:

08-16 22:28:56.293: E/AndroidRuntime(360): Caused by: java.lang.NullPointerException
08-16 22:28:56.293: E/AndroidRuntime(360):  at com.project.findmybuffet.CopperChimneyDesc.onCreate(CopperChimneyDesc.java:37)

Whichever line it is in your pasted sources, you got NullPointerException (aka NPE) there which usually means you try to use either value that is no longer existing, or some elements are not initialized.

Upvotes: 0

Related Questions