gpt4 book ai didi

java - 适配器内 Intent 的 Android 错误消息

转载 作者:行者123 更新时间:2023-12-02 03:34:02 24 4
gpt4 key购买 nike

我正在尝试让 map Intent 在我的 Json 适配器中工作。但不知怎的,我总是收到错误消息:

java.lang.NullPointerException: Attempt to invoke virtual method 'void android.content.Context.startActivity(android.content.Intent)' on a null object reference

这是我的第二个 Android 项目,我找不到我的错误。我希望你们中的一些人可以帮助我。

class  JSONAdapter extends BaseAdapter implements ListAdapter{

private final Activity activity;
private Context context;
private final JSONArray jsonArray;

JSONAdapter(Activity activity, JSONArray jsonArray) {
assert activity != null;
assert jsonArray != null;

this.jsonArray = jsonArray;
this.activity = activity;
}


@Override public int getCount() {
if(null==jsonArray)
return 0;
else
return jsonArray.length();
}

@Override public JSONObject getItem(int position) {
if(null==jsonArray) return null;
else
return jsonArray.optJSONObject(position);
}

@Override public long getItemId(int position) {
JSONObject jsonObject = getItem(position);

return jsonObject.optLong("id");
}

@Override public View getView(int position, View convertView, ViewGroup parent) {
if (convertView == null)
convertView = activity.getLayoutInflater().inflate(R.layout.row, null);



TextView tBrand =(TextView)convertView.findViewById(R.id.tvbrand);
TextView tStreet =(TextView)convertView.findViewById(R.id.tvstreet);
final TextView tPrice = (TextView)convertView.findViewById(R.id.tvprice);
final TextView tPlace = (TextView)convertView.findViewById(R.id.tvplace);
TextView tOpen = (TextView)convertView.findViewById(R.id.tvopen);



JSONObject json_data = getItem(position);
if(null!=json_data ) {
String brand = null;
String street = null;
String price = null;
String houseNumber = null;
String place = null;
String postCode = null;
boolean open = false;
Double statLng = null;
Double statLat = null;

try {
brand = json_data.getString("brand");
street = json_data.getString("street");
price = Double.toString(json_data.getDouble("price"));
houseNumber = json_data.getString("houseNumber");
place = json_data.getString("place");
postCode = json_data.getString("postCode");
open = json_data.getBoolean("isOpen");
statLng = json_data.getDouble("lng");
statLat = json_data.getDouble("lat");

} catch (JSONException e) {
e.printStackTrace();
}
if (houseNumber.equals("null")) {
houseNumber = "";
}
tBrand.setText(brand);
tPrice.setText(price + "€");
tStreet.setText(street + " " + houseNumber);
tPlace.setText(postCode + " " + place);
if (open == true) {
tOpen.setText("geöffnet");
tOpen.setTextColor(Color.GREEN);
} else {
tOpen.setText("geschlossen");
tOpen.setTextColor(Color.RED);
}


final Double finalStatLng = statLng;
final Double finalStatLat = statLat;
tPrice.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Uri gmmIntentUri = Uri.parse("google.navigation:q=" + finalStatLat + "," + finalStatLng);
Intent mapIntent = new Intent(Intent.ACTION_VIEW, gmmIntentUri);
mapIntent.setPackage("com.google.android.apps.maps");
context.startActivity(mapIntent);

}
});

}

return convertView;
}

最佳答案

您错过了为上下文赋值

context = Activity; 或使用 activity.startActivity(mapIntent);

关于java - 适配器内 Intent 的 Android 错误消息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37663078/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com