A
A
all230819922015-04-02 17:02:30
Android
all23081992, 2015-04-02 17:02:30

Why doesn't the dialog open when called from the list?

There is a list of buttons. A handler is attached to the button click, which opens a dialog (alert.show()).
When you first create an activity, events work without problems. But if you exit the activity and reopen it from another activity, an error occurs when you click on the button: "Unable to add window is not valid; is your activity running". What could be the reason? There is also just a button on the form. it works out the
event_list as it should

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:orientation="vertical"
              android:layout_width="match_parent"
              android:layout_height="match_parent">

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="New Button"
        android:id="@+id/button4"
        android:onClick="sendClick"/>

    <ListView
    android:layout_width="match_parent" android:layout_height="match_parent"
    android:id="@+id/event_listview"
    android:dividerHeight="10dp"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:alpha="1"
    android:divider="#00ffffff">

</ListView>
</LinearLayout>

ListEventActivity
public class ListEventActivity extends Activity {

    static EventsAdapter nonsendedAdapter;


    private static final String TAG = "myLogs";

    public void butClick(View v) {
      //Button b = (Button)findViewById(R.id.button4);
       // b.performClick();
        sendClick(v);
    }
    public void sendClick(View v) {
       try{
        Log.d(TAG, "MainActivity.sendClick");
        Context context = getParent();


        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setTitle("Важное сообщение!")
                .setMessage("Покормите кота!")

                .setCancelable(false)
                .setNegativeButton("ОК, иду на кухню",
                        new DialogInterface.OnClickListener() {
                            public void onClick(DialogInterface dialog, int id) {
                                dialog.cancel();
                            }
                        });
        AlertDialog alert = builder.create();

            alert.show();
        }
        catch (Exception e) {
            Log.d(TAG, "MainActivity.sendClick error " + e.getMessage());
        }
    }


    protected void onResume() {
        super.onResume();
    }
  public void initInterface() {
            ListView nonsendedList = getNonsendedListView();
            if (nonsendedAdapter == null) {
                nonsendedAdapter = new EventsAdapter(this, EventsAdapter.MODE_UNSENDED, nonsendedList, this);
            } else {
                nonsendedAdapter.loadData();
            }

            nonsendedList.setAdapter(nonsendedAdapter);


    }

    public void notifyAdapters() {
        nonsendedAdapter.notifyDataSetChanged();

    }

    public ListView getNonsendedListView() {
        return (ListView)nonsendedView;
    }


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.event_list);
Intent intent = getIntent();
        nonsendedView = (ListView)findViewById(R.id.event_listview);
        initInterface();
    }
    ListView  nonsendedView;


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.menu_maps, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case android.R.id.home:
                this.onBackPressed();
        }
        return super.onOptionsItemSelected(item);
    }
    @Override
    protected void onDestroy() {
        Log.d(TAG, "destroy ");
        nonsendedView.setAdapter(null);
        super.onDestroy();
    }
}

Answer the question

In order to leave comments, you need to log in

1 answer(s)
O
one pavel, 2015-04-02
@all23081992

The problem seems to be that your nonsendedAdapter is static. Inside the EventsAdapter you are passing a reference to the first activity that cannot die when closed. When opening a second activity, an adapter with a link to the first activity is installed in the new ListView. Such nonsense creates a lot of problems.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question