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

时间:2019-02-11 22:23:39

标签: android android-arrayadapter android-alertdialog android-context

当我从适配器的ViewHolder中按下按钮时,我试图显示一个AlertDialog。但是当我使用下一条消息启动崩溃时。

  

E / Android运行时:致命异常:主要       进程: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();
    }
    [...]

4 个答案:

答案 0 :(得分:2)

传递您全局声明的上下文

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

并将以下行更改为

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

adapter = new CustomAdapter(row_models, this);

我希望这会有所帮助。

答案 1 :(得分:1)

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

  1. 在CustomAdapter中首先传递“ this”。

    adapter = new CustomAdapter(row_models,this);
    
  2. 在CustomAdapter中获取活动。

    public CustomAdapter(ArrayList<RowModel> data, Activity activity) {
    super(context, R.layout.list_item_main, data);
    this.DataSet = data;
    this.context = context;
    }
    
  3. 使用“活动”创建AlertDialoge。

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

答案 2 :(得分:0)

您可以像这样从Context获得View

@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; 
    } 
}

答案 3 :(得分:0)

您将必须使用Activity实例,或者可以使用TypeCast ContextActivity,如下所示:

AlertDialog.Builder builder = new AlertDialog.Builder(YourActivity.this);

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

,然后在显示AlertDialog的地方进行类似的操作

if(!((Activity)context).isFinishing())
{
    builder.show(); 
} 

您不会收到该异常