0

我根据他们从一个活动到另一个活动的点击在列表视图中传递参数,它在传递参数行 firs activity.java 的日志 cat 中显示错误

public class SouthIndianvegrecipes1  extends Activity {
     @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.main);

            ArrayList<Recipedetails> image_details = GetSearchResults();

            final ListView lv1 = (ListView) findViewById(R.id.listV_main);
            lv1.setAdapter(new ItemListBaseAdapter(this, image_details));

            lv1.setOnItemClickListener(new OnItemClickListener() {
                @Override
                public void onItemClick(AdapterView<?> a, View v, int position, long id) { 
                    Object o = lv1.getItemAtPosition(position);
                    Recipedetails obj_itemDetails = (Recipedetails)o;
                    Toast.makeText(SouthIndianvegrecipes1.this, "You have chosen : " + " " + obj_itemDetails.getName(), Toast.LENGTH_LONG).show();
                     Intent newActivity = new Intent(SouthIndianvegrecipes1.this, 
                                SouthIndianvegmanualrecipes.class );  



                    newActivity.putExtra("name", obj_itemDetails.getName());
                                startActivity(newActivity);


                } 
            });
        }

        private ArrayList<Recipedetails> GetSearchResults(){
            ArrayList<Recipedetails> results = new ArrayList<Recipedetails>();

            Recipedetails item_details = new Recipedetails();
            item_details.setName("Godhumai veg adai");
            item_details.setItemDescription("This is very good for dibets people");
            //item_details.setUrlWiki("https://www.youtube.com/watch?v=C_r0mFvMSfU");
            item_details.setImageNumber(12);
            results.add(item_details);

            item_details = new Recipedetails();
            item_details.setName("Soyamore koozh");
            item_details.setItemDescription("Tasty to have this as main dish");
            //item_details.setUrlWiki("https://www.youtube.com/watch?v=C_r0mFvMSfU");
            item_details.setImageNumber(13);
            results.add(item_details);

            item_details = new Recipedetails();
            item_details.setName("Tomato kulambu");
            item_details.setItemDescription("Good side dish for rice and dinner");
            //item_details.setUrlWiki("https://www.youtube.com/watch?v=C_r0mFvMSfU");
            item_details.setImageNumber(14);
            results.add(item_details);

            item_details = new Recipedetails();
            item_details.setName("Tomato upma");
            item_details.setItemDescription("Evening time  dinner for kids");
            //item_details.setUrlWiki("https://www.youtube.com/watch?v=C_r0mFvMSfU");
            item_details.setImageNumber(15);
            results.add(item_details);




            item_details = new Recipedetails();
            item_details.setName("Navratri special vada");
            item_details.setItemDescription("Good snacks at the function");
            //item_details.setUrlWiki("https://www.youtube.com/watch?v=C_r0mFvMSfU");
            item_details.setImageNumber(16);

            results.add(item_details);


            item_details = new Recipedetails();
            item_details.setName("Egg kurma");
            item_details.setItemDescription("Wonderful dish made by egg");
            //item_details.setUrlWiki("https://www.youtube.com/watch?v=C_r0mFvMSfU");
            item_details.setImageNumber(17);
            results.add(item_details);

            item_details = new Recipedetails();
            item_details.setName("Milagu kuzhambu");
            item_details.setItemDescription("Good for digestion purpose");
            //item_details.setUrlWiki("https://www.youtube.com/watch?v=C_r0mFvMSfU");
            item_details.setImageNumber(18);
            results.add(item_details);

            item_details = new Recipedetails();
            item_details.setName("Rasam");
            item_details.setItemDescription("Last and famous dish in lunch,good for health also");
            //item_details.setUrlWiki("https://www.youtube.com/watch?v=C_r0mFvMSfU");
            item_details.setImageNumber(19);
            results.add(item_details);

            item_details = new Recipedetails();
            item_details.setName("Vegetable kootu");
            item_details.setItemDescription("Nice and healthy one");
            //item_details.setUrlWiki("https://www.youtube.com/watch?v=C_r0mFvMSfU");
            item_details.setImageNumber(20);
            results.add(item_details);

            item_details = new Recipedetails();
            item_details.setName("Avial");
            item_details.setItemDescription("Easy to prepare and lots of vegetable");
            //item_details.setUrlWiki("https://www.youtube.com/watch?v=C_r0mFvMSfU");
            item_details.setImageNumber(21);
            results.add(item_details);

            item_details = new Recipedetails();
            item_details.setName("kothavarangaicurry");
            item_details.setItemDescription("Tamilnadu special and healthy too");
            //item_details.setUrlWiki("https://www.youtube.com/watch?v=C_r0mFvMSfU");
            item_details.setImageNumber(22);
            results.add(item_details);


            return results;
        }

}
passing the arguments after toasting ,passing their names
second activity.java



    public class SouthIndianvegmanualrecipes  extends Activity {



        @Override
            public void onCreate(Bundle savedInstanceState) {
                super.onCreate(savedInstanceState);
                setContentView(R.layout.recipe_manual);
}

           String value =null; 
          Bundle extras = getIntent().getExtras();
 {
      if (extras != null) {
         if (extras.containsKey("name")) {
            value = extras.getString("name");
         }
      } 



        {
        //int obj_itemDetails.getImageNumber() = extras.getIntArray("numbers");

            //private ArrayList<Recipedetails2> GetSearchResults(){
                //ArrayList<Recipedetails2> results = new ArrayList<Recipedetails2>();

            if(value.equals("Godhumai veg adai"))
            {
                Recipedetails2 item_details = new Recipedetails2();
                item_details.setName("Godhumai veg adai");
                item_details.setIngredients("This is very good for dibets people");
                item_details.setMethods("This is very good for dibets people");

            }
            else if(value.equals("Soyamore koozh"))
            {
                Recipedetails2 item_detail1 = new Recipedetails2();
                item_detail1.setName("Soyamore koozh");
                item_detail1.setIngredients("Tasty to have this as main dish");
                item_detail1.setMethods("This is very good for dibets people");
            }
            else if(value.equals("Tomato kulambu"))
            {
                Recipedetails2 item_detail3 = new Recipedetails2();
                item_detail3.setName("Tomato kulambu");
                item_detail3.setIngredients("Good side dish for rice and dinner");
                item_detail3.setMethods("This is very good for dibets people");

            }
            else if(value.equals("Navratri special vada"))
            {
                Recipedetails2 item_detail5 = new Recipedetails2();
                item_detail5.setName("Navratri special vada");
                item_detail5.setIngredients("Good snacks at the function");
                item_detail5.setMethods("This is very good for dibets people");

            }
            else if(value.equals("Egg kurma"))
            {
                Recipedetails2 item_detail6 = new Recipedetails2();
                item_detail6.setName("Egg kurma");
                item_detail6.setIngredients("Wonderful dish made by egg");
                item_detail6.setMethods("This is very good for dibets people");

            }
            else if(value.equals("Milagu kuzhambu"))
            {
                Recipedetails2 item_detail7 = new Recipedetails2();
                item_detail7.setName("Milagu kuzhambu");
                item_detail7.setIngredients("Good for digestion purpose");
                item_detail7.setMethods("This is very good for dibets people");

            }

            else if(value.equals("Rasam"))
            {
                Recipedetails2 item_detail8 = new Recipedetails2();
                item_detail8.setName("Rasam");
                item_detail8.setIngredients("Last and famous dish in lunch,good for health also");
                item_detail8.setMethods("This is very good for dibets people");

            }

            else if(value.equals("Vegetable kootu"))
            {
                Recipedetails2 item_detail9 = new Recipedetails2();
                item_detail9.setName("Vegetable kootu");
                item_detail9.setIngredients("Nice and healthy one");
                item_detail9.setMethods("This is very good for dibets people");

            }

            else if(value.equals("Avial"))
            {
                Recipedetails2 item_detail10 = new Recipedetails2();
                item_detail10.setName("Avial");
                item_detail10.setIngredients("Easy to prepare and lots of vegetable");
                item_detail10.setMethods("This is very good for dibets people");

            }

            else 
            {
                Recipedetails2 item_detail11 = new Recipedetails2();
                item_detail11.setName("kothavarangaicurry");
                item_detail11.setIngredients("Tamilnadu special and healthy too");
                item_detail11.setMethods("This is very good for dibets people");

            }


            }
    }

它在这一行中显示错误 String value =getIntent().getStringExtra("name"); 我的日志猫错误是

03-08 04:52:09.754: E/AndroidRuntime(1048): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.example.recipestutors/com.example.recipestutors.SouthIndianvegmanualrecipes}: java.lang.NullPointerException
    03-08 04:52:09.754: E/AndroidRuntime(1048):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2106)
    03-08 04:52:09.754: E/AndroidRuntime(1048):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
    03-08 04:52:09.754: E/AndroidRuntime(1048):     at android.app.ActivityThread.access$600(ActivityThread.java:141)
    03-08 04:52:09.754: E/AndroidRuntime(1048):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
    03-08 04:52:09.754: E/AndroidRuntime(1048):     at android.os.Handler.dispatchMessage(Handler.java:99)
    03-08 04:52:09.754: E/AndroidRuntime(1048):     at android.os.Looper.loop(Looper.java:137)
    03-08 04:52:09.754: E/AndroidRuntime(1048):     at android.app.ActivityThread.main(ActivityThread.java:5039)
    03-08 04:52:09.754: E/AndroidRuntime(1048):     at java.lang.reflect.Method.invokeNative(Native Method)
    03-08 04:52:09.754: E/AndroidRuntime(1048):     at java.lang.reflect.Method.invoke(Method.java:511)
    03-08 04:52:09.754: E/AndroidRuntime(1048):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
    03-08 04:52:09.754: E/AndroidRuntime(1048):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
    03-08 04:52:09.754: E/AndroidRuntime(1048):     at dalvik.system.NativeStart.main(Native Method)
    03-08 04:52:09.754: E/AndroidRuntime(1048): Caused by: java.lang.NullPointerException
    03-08 04:52:09.754: E/AndroidRuntime(1048):     at com.example.recipestutors.SouthIndianvegmanualrecipes.<init>(SouthIndianvegmanualrecipes.java:79)
    03-08 04:52:09.754: E/AndroidRuntime(1048):     at java.lang.Class.newInstanceImpl(Native Method)
    03-08 04:52:09.754: E/AndroidRuntime(1048):     at java.lang.Class.newInstance(Class.java:1319)
    03-08 04:52:09.754: E/AndroidRuntime(1048):     at android.app.Instrumentation.newActivity(Instrumentation.java:1054)
    03-08 04:52:09.754: E/AndroidRuntime(1048):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2097)
    03-08 04:52:09.754: E/AndroidRuntime(1048):     ... 11 more
4

3 回答 3

0

请不要直接调用意图,而是尝试执行以下操作以检查一切是否正确,然后再尝试获取您正在寻找的值

String value =null; 

 Bundle extras = getIntent().getExtras();
      if (extras != null) {
         if (extras.containsKey("name")) {
            value = extras.getString("name");
         }
      }

尝试在目标类(SouthIndianvegmanualrecipes)中定义公共静态字符串。例如: public static String PRODUCT_NAME = "PRODUCT NAME PLACEHOLDER";

然后在你的第一堂课上

newActivity.putExtra(SouthIndianvegmanualrecipes.PRODUCT_NAME, obj_itemDetails.getName());

在你的目标班级

 Bundle extras = getIntent().getExtras();
          if (extras != null) {
             if (extras.containsKey(PRODUCT_NAME)) {
                value = extras.getString(PRODUCT_NAME);
             }
          }
于 2013-03-08T05:28:24.847 回答
0

在第二个活动中,我们必须像这样放置它会起作用

@Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.recipe_manual);
            Intent intent = getIntent();
            String value = intent.getStringExtra("name");
于 2013-03-08T08:04:04.547 回答
0

我猜private Object getIntent;是罪魁祸首。尝试从下面的代码中删除它。

public class SouthIndianvegmanualrecipes  extends Activity {

 private Object getIntent;

@Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.recipe_manual);



String value =getIntent().getStringExtra("name");  
于 2013-03-08T06:01:31.213 回答