在android中导航活动

时间:2013-08-16 17:07:21

标签: android

您好我正在尝试学习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);

2 个答案:

答案 0 :(得分:2)

您尚未在此处添加任何其他内容

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

因此getIntent().getExtras().getString("CC_RES"));将返回null

我不确定您希望该值来自何处,但需要将Intent添加到Activity这样的内容,例如

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

您还可能需要检查getIntent()getExtras()在第二节课中没有返回null,然后再将值分配给某些内容。当您从extras转到MainActivity时,您有Desc,但从Photos转到Desc则没有。

答案 1 :(得分:0)

由于CopperChimneyDesc.java第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)

无论您在粘贴的资源中使用哪一行,都会在其中获得NullPointerException(又名NPE),这通常意味着您尝试使用不再存在的值,或者某些元素未初始化。