自定义对话框无法在适配器中运行,崩溃应用程序

问题描述 投票:-1回答:3

我有一个带有requestQueue的listview,它调用POST到mysqli数据库以获取一个数组JSONobject。基础适配器用于listview布局数据。列表上的每个项目都有一个按钮。我试图找出自定义对话框崩溃应用程序的原因。这可能就是我如何调用对话框。我知道显示对话框是导致崩溃的原因。

sensors_activity.Java

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



}

SensorAdapter

package com.example.djwiz.test5;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.Application;
import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
import android.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
                                                                     android.view.WindowManager$BadTokenException: Unable to add window -- token null is not valid; is your activity running?
                                                                         at android.view.ViewRootImpl.setView(ViewRootImpl.java:765)
                                                                         at android.view.WindowManagerGlobal.addView(WindowManagerGlobal.java:356)
                                                                         at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:93)
                                                                         at android.app.Dialog.show(Dialog.java:330)
                                                                         at com.example.djwiz.test5.SensorsAdapter$1.onClick(SensorsAdapter.java:106)
                                                                         at android.view.View.performClick(View.java:6294)
                                                                         at android.view.View$PerformClick.run(View.java:24770)
                                                                         at android.os.Handler.handleCallback(Handler.java:790)
                                                                         at android.os.Handler.dispatchMessage(Handler.java:99)
                                                                         at android.os.Looper.loop(Looper.java:164)
                                                                         at android.app.ActivityThread.main(ActivityThread.java:6494)
                                                                         at java.lang.reflect.Method.invoke(Native Method)
                                                                         at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438)
                                                                         at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807)

任何帮助都会很优雅。谢谢

java android dialog adapter
3个回答
0
投票

首先,你必须改变

    SensorsAdapter sensorsAdapter = new 
SensorsAdapter(Sensors_Activity.this,id,room_id,state,type,value);
                S_lv.setAdapter(sensorsAdapter);

请通过活动而非上下文。然后在适配器中更改

Context TheContext;

Activity theActivity

然后将Activity传递给对话框

 S_LockBuilder = new AlertDialog.Builder(theActivity);

0
投票

Activitys需要Context Dialog。一个Application Context不起作用。


0
投票

像这样写:

   SensorsAdapter sensorsAdapter = new SensorsAdapter(Sensors_Activity.this,id,room_id,state,type,value);
                S_lv.setAdapter(sensorsAdapter);
© www.soinside.com 2019 - 2024. All rights reserved.