java自定义对话框在适配器内不工作,应用程序崩溃
我有一个带有requestQueue的listview,它调用mysqli数据库的POST来获取数组JSONobject。listview使用基本适配器来布局数据。列表上的每个项目都有一个按钮。我试图找出为什么自定义对话框会使应用程序崩溃。这可能就是我所说的对话。我知道显示对话框是导致崩溃的原因
传感器不支持活动。爪哇
public class Sensors_Activity extends AppCompatActivity
{
@Override
protected void onCreate(final Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_sensors_);
final ListView S_lv = (ListView) findViewById(R.id.S_ListView);
RequestQueue requestQueue;
requestQueue = (RequestQueue) Volley.newRequestQueue(getApplicationContext());
JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(POST, DBStrings.DataUrl, null, new Response.Listener<JSONObject>()
{
@Override
public void onResponse(JSONObject response)
{
....
SensorsAdapter sensorsAdapter = new SensorsAdapter(getApplicationContext(),id,room_id,state,type,value);
S_lv.setAdapter(sensorsAdapter);
....
});//Response ends here
requestQueue.add(jsonObjectRequest);
}//OnCreate ends here
}
传感器适配器
package com.example.djwiz.test5;
import 安卓.app.Activity;
import 安卓.app.AlertDialog;
import 安卓.app.Application;
import 安卓.content.Context;
import 安卓.content.Intent;
import 安卓.view.LayoutInflater;
import 安卓.view.View;
import 安卓.view.ViewGroup;
import 安卓.widget.BaseAdapter;
import 安卓.widget.Button;
import 安卓.widget.TextView;
import 安卓.widget.Toast;
import 安卓.widget.ToggleButton;
import java.security.AccessController;
import java.util.zip.Inflater;
import static java.security.AccessController.getContext;
public class SensorsAdapter extends BaseAdapter
{
LayoutInflater mInflater;
String[] Id_Array;
String[] Room_Id_Array;
String[] State_Array;
String[] Type_Array;
String[] Value_Array;
Context TheContext;
public SensorsAdapter(Context c, String[] I, String[] R, String[] S, String[] T, String[] V)
{
Id_Array = I;
Room_Id_Array = R;
State_Array = S;
Type_Array = T;
Value_Array = V;
TheContext = c;
mInflater = (LayoutInflater.from(c));
}
@Override
public int getCount()
{
return Id_Array.length;
}
@Override
public Object getItem(int position)
{
return Id_Array[position];
}
@Override
public long getItemId(int position)
{
return position;
}
@Override
public View getView(final int positon, View convertView, ViewGroup parent)
{
View v = mInflater.inflate(R.layout.sensor_list, null);
TextView idtv = (TextView) v.findViewById(R.id.S_id_TV);
TextView roomtv = (TextView) v.findViewById(R.id.S_room_id_TV);
final TextView typetv = (TextView) v.findViewById(R.id.S_type_TV);
TextView valuetv = (TextView) v.findViewById(R.id.S_value_TV);
TextView statetv = (TextView) v.findViewById(R.id.S_state_TV);
final Button Setting_btn = (Button) v.findViewById(R.id.S_Settings_Btn);
Setting_btn.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View v)
{
Toast.makeText(TheContext, typetv.getText() +" was click at " + (positon +1) , Toast.LENGTH_SHORT).show();
String type;
type = typetv.getText().toString();
if(type.equals("lock"))
{
AlertDialog.Builder S_LockBuilder;
LayoutInflater inflater = (LayoutInflater) TheContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
S_LockBuilder = new AlertDialog.Builder(TheContext);
final View S_LockView;
S_LockView = inflater.inflate(R.layout.linear_lock_dialog,null);
TextView L_State_TV;
TextView L_Value_TV;
ToggleButton L_T_BTN;
L_T_BTN = (ToggleButton) S_LockView.findViewById(R.id.D_TOGGLE_BTN);
S_LockBuilder.setView(S_LockView);
AlertDialog Lockdialog = S_LockBuilder.create();
Lockdialog.show();
}
}
});
String ID = Id_Array[positon];
String ROOM = Room_Id_Array[positon];
String STATE = State_Array[positon];
String TYPE = Type_Array[positon];
String VALUE = Value_Array[positon];
idtv.setText(ID);
roomtv.setText(ROOM);
typetv.setText(TYPE);
valuetv.setText(VALUE);
statetv.setText(STATE);
return v;
}
}
Logcat
02-20 00:43:22.126 13775-13775/com.example.djwiz.test5 E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.djwiz.test5, PID: 13775
安卓.view.WindowManager$BadTokenException: Unable to add window -- token null is not valid; is your activity running?
at 安卓.view.ViewRootImpl.setView(ViewRootImpl.java:765)
at 安卓.view.WindowManagerGlobal.addView(WindowManagerGlobal.java:356)
at 安卓.view.WindowManagerImpl.addView(WindowManagerImpl.java:93)
at 安卓.app.Dialog.show(Dialog.java:330)
at com.example.djwiz.test5.SensorsAdapter$1.onClick(SensorsAdapter.java:106)
at 安卓.view.View.performClick(View.java:6294)
at 安卓.view.View$PerformClick.run(View.java:24770)
at 安卓.os.Handler.handleCallback(Handler.java:790)
at 安卓.os.Handler.dispatchMessage(Handler.java:99)
at 安卓.os.Looper.loop(Looper.java:164)
at 安卓.app.ActivityThread.main(ActivityThread.java:6494)
at java.lang.reflect.Method.invoke(Native Method)
at com.安卓.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438)
at com.安卓.internal.os.ZygoteInit.main(ZygoteInit.java:807)
任何帮助都是优雅的。 多谢各位
# 1 楼答案
你需要一个}不起作用
Activity
Context
用于Dialog
s。一个Application
{# 2 楼答案
这样写:
# 3 楼答案
首先你必须改变
请传递活动而不是上下文。 然后在适配器中进行更改
到
然后将活动传递给对话框