AlertDialog无法在自定义适配器中获取上下文

问题描述 投票:0回答:4

当我从适配器按ViewHolder中的按钮时,我正在尝试显示AlertDialog。但是当我用下一条消息发起这次崩溃时。

E / AndroidRuntime:FATAL EXCEPTION:main进程:cl.abitsoft.todotick,PID:4172 android.view.WindowManager $ BadTokenException:无法添加窗口 - 令牌null无效;你的活动在运行吗?在android.view.ViewRootImpl.setView(ViewRootImpl.java:798)

public class CustomAdapter extends ArrayAdapter<RowModel> implements View.OnClickListener {

    private ArrayList<RowModel> DataSet;
    Context context;

    private static class ViewHolder {
        [...]
    }

    public CustomAdapter(ArrayList<RowModel> data, Context context) {
        super(context, R.layout.list_item_main, data);
        this.DataSet = data;
        this.context = context;
    }

    @Override
    public void onClick(View v) {
        int position = (Integer) v.getTag();
        final Object object = getItem(position);

        switch (v.getId()) {
            case R.id.list_delete_button:
                AlertDialog.Builder builder = new AlertDialog.Builder(getContext());
                builder.show();
                break;
        }
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        [...]
    }

}

编辑:添加了MainActivity.class

public class MainActivity extends AppCompatActivity
        implements NavigationView.OnNavigationItemSelectedListener, View.OnClickListener {

    ListView listview;
    private CustomAdapter adapter;

    private Button no, button_accept;
    private EditText edittext_title;
    private Spinner spinner_classes;

    private RowModel rowModel;

    private ArrayList<RowModel> row_models;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Toolbar toolbar = findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

        FloatingActionButton fab = findViewById(R.id.fab);
        fab.setOnClickListener(this);

        DrawerLayout drawer = findViewById(R.id.drawer_layout);
        ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
                this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
        drawer.addDrawerListener(toggle);
        toggle.syncState();

        NavigationView navigationView = findViewById(R.id.nav_view);
        navigationView.setNavigationItemSelectedListener(this);

        // Se inicializan las variables
        edittext_title = findViewById(R.id.main_edittext_title);
        spinner_classes = findViewById(R.id.main_spinner_classes);
        button_accept = findViewById(R.id.main_button_accept);
        listview = findViewById(R.id.main_listview);
        row_models = new ArrayList<>();
        adapter = new CustomAdapter(row_models, getApplicationContext());
        // AL ListView se le asigna el Adapter con el tipo de objeto que usaremos
        listview.setAdapter(adapter);
        // Creamos un arreglo del tipo String con las variables para el Spinner
        String[] values = {"Pagar", "Cobrar", "Llamar", "Pedir", "Comprar", "Revisar", "Otro"};
        // Agregamos las variables a nuestro Spinner
        spinner_classes.setAdapter(new ArrayAdapter<String>(getApplicationContext(), R.layout.spinner_main, values));
        // Habilitamos el click en nuestro boton
        button_accept.setOnClickListener(this);

        loadRows();
    }
    [...]
android android-arrayadapter android-alertdialog android-context
4个回答
2
投票

传递您已全局声明的上下文

AlertDialog.Builder builder = new AlertDialog.Builder(context);

并将以下行更改为

adapter = new CustomAdapter(row_models, getApplicationContext());

adapter = new CustomAdapter(row_models, this);

我希望这将有所帮助。


2
投票

进行这3项更改以解决您的问题,

  1. 首先在自定义适配器中传递“this”。 qazxsw poi
  2. 获取自定义适配器中的活动 qazxsw poi
  3. 使用“活动”创建AlertDialoge。 adapter = new CustomAdapter(row_models,this);

0
投票

你可以从你的public CustomAdapter(ArrayList<RowModel> data, Activity activity) { super(context, R.layout.list_item_main, data); this.DataSet = data; this.context = context; } 得到AlertDialog.Builder builder = new AlertDialog.Builder(activity);

Context

0
投票

你必须使用View实例或者可以使用TypeCast @Override public void onClick(View v) { int position = (Integer) v.getTag(); final Object object = getItem(position); switch (v.getId()) { case R.id.list_delete_button: AlertDialog.Builder builder = new AlertDialog.Builder(v.getContext()); builder.show(); break; } } Activity,如下所示:

Context

要么

Activity

你在哪里展示AlertDialog.Builder builder = new AlertDialog.Builder(YourActivity.this);然后做这样的事情

AlertDialog.Builder builder = new AlertDialog.Builder((Activity)context);

你不会得到那个例外

© www.soinside.com 2019 - 2024. All rights reserved.