java - 无法根据我在 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
一世
解决方案
这里的问题是,在你的Order
类中,你的代码试图同时获取StringExtra
两者getAllArea
,Cheruvathur
这将导致崩溃,因为它们中只有一个存在,因为你Intent
一次调用一个。因此,一种解决方案是,您可以简单地检查Intent
sStringExtra
是否不存在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);
}
推荐阅读
- angularjs - 使用 JWT 和 AngularJS 处理页面刷新以维护用户数据的最佳方法是什么
- angularjs - 如何使用 ADAL js 前端管理多个租户、ClientID 和端点?
- css - 用于 SCSS 的 IntelliJ FileWatcher 无法正常工作
- sed - 使用 sed 清理 SchemaSync 输出
- java - JPA - 带有 in 子句和不区分大小写的规范
- html - 移动到下一个表单控件后如何更改 mat-input 文本颜色?
- angular - 所需的请求部分“文件”不存在 - Angular 4
- android - Places API 设置在 build.gradle 设置期间出错
- moodle - 在 Moodle Dashboard 中按类别在两个块中显示课程
- c# - 如何计算 C# 中的类属性?