首页 > 解决方案 > 无法根据我在 AlertDialog 中给出的位置购买名称

问题描述

   AllArea.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
    //get the place values as popup window
    String[] items = {"All-Area","Cheruvathur", "Kanhangad","Nileswaram","Panathur","Periya","Thrikkaripur","Vellarikkundu"};
    AlertDialog.Builder  builder=new AlertDialog.Builder(context);
    builder.setTitle("Choose Area: ");
    builder.setItems(items, new DialogInterface.OnClickListener() {
    @Override
    public void onClick(DialogInterface dialog, int item)
    {
    //AllArea.setText(items[item]);
    if(items[item].equals("All-Area"))
    {
    AllArea.setText(items[item].toString());
    //System.out.println(AllArea);
    String getAllArea=AllArea.getText().toString();
    Intent i=new Intent(SecondPage.this,Order.class);
    i.putExtra("getAllArea", getAllArea);
    startActivity(i);
    Toast.makeText(getApplicationContext(),"All-Area",Toast.LENGTH_SHORT).show();
    }
    else if(items[item].equals("Cheruvathur"))
    {
    AllArea.setText(items[item].toString());
    Intent j=new Intent(SecondPage.this,Order.class);
    String getCheruvathur=AllArea.getText().toString();
    j.putExtra("getCheruvathur", getCheruvathur);
    startActivity(j);
    //System.out.println(AllArea);
    Toast.makeText(getApplicationContext(),"Cheruvathur",Toast.LENGTH_SHORT).show();
    }
    }
    }
    });

无法根据给定位置查看商店名称并在另一个活动中显示其名称,

请帮我找出问题所在

基于位置的店铺选值代码

Intent All=getIntent();
String Allnew=All.getStringExtra("getAllArea");
            if (Allnew.equals("All-Area")) {

                list = new ArrayList<>();
                list.add("3G mobile : Chevtr");
                list.add("Spice World : kngd");
                list.add("Dotcom : nslrm");
                list.add("Pv Stores: Pya");
                list.add("Sky World : Pntur");
                list.add("Mobile Galaxy : trkppr");
                list.add("Dream Mobile : vlkdu");
                list.add("Tele World : Chevtr");
                list.add("Info Tech Solutions : Pya");
                list.add("MYG Mobile : trkppr");
              // adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, list);
             // listView.setAdapter(adapter);
            }




 Intent Cheru=getIntent();
        String Cherunew=Cheru.getStringExtra("getCheruvathur");
       if (Cherunew.equals("Cheruvathur")) {

          list1 = new ArrayList<>();
          list1.add("3G mobile : Chevtr");
          list1.add("Tele World : Chevtr");

         //  adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, list);
          // listView.setAdapter(adapter);
       }

请帮我找出问题日志 cat 显示这是错误

NullPointerException: Attempt to invoke virtual method 'boolean java.lang.String.equalsIgnoreCase(java.lang.String)' on a null object reference

一世

标签: javaandroidarraylistandroid-listviewandroid-alertdialog

解决方案


这里的问题是,在你的Order类中,你的代码试图同时获取StringExtra两者getAllAreaCheruvathur这将导致崩溃,因为它们中只有一个存在,因为你Intent一次调用一个。因此,一种解决方案是,您可以简单地检查IntentsStringExtra是否不存在null,并且只需对您的代码进行少量修改,它就可以正常工作,试试这个。

Intent intent=getIntent();
if(intent.getStringExtra("getAllArea")!= null)
                list = new ArrayList<>();
                list.add("3G mobile : Chevtr");
                list.add("Spice World : kngd");
                list.add("Dotcom : nslrm");
                list.add("Pv Stores: Pya");
                list.add("Sky World : Pntur");
                list.add("Mobile Galaxy : trkppr");
                list.add("Dream Mobile : vlkdu");
                list.add("Tele World : Chevtr");
                list.add("Info Tech Solutions : Pya");
                list.add("MYG Mobile : trkppr");
              // adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, list);
             // listView.setAdapter(adapter);
            } 

else if(intent.getStringExtra("Cheruvathur")!= null)
          list1 = new ArrayList<>();
          list1.add("3G mobile : Chevtr");
          list1.add("Tele World : Chevtr"); 

         //  adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, list);
          // listView.setAdapter(adapter);
       }

推荐阅读