2

Here is my .java file:

package com.shoppinglist;

import java.util.ArrayList;

import android.app.Dialog;
import android.app.ListActivity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;

public class ShoppingList extends ListActivity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        final EditText et = (EditText) findViewById(R.id.edittext);
        ListView lv = (ListView) findViewById(android.R.id.list);
        final ArrayList<String> items = new ArrayList<String>();
        final ArrayAdapter<String> adapter;
        adapter = new ArrayAdapter<String>(
        this, android.R.layout.simple_list_item_1, items);
        lv.setAdapter(adapter);
        setContentView(R.layout.main);

        Button button1main = (Button) findViewById(R.id.add);
        button1main.setOnClickListener(new OnClickListener()  {
            @Override
            public void onClick(View v)  {
            final Dialog dialog = new Dialog(ShoppingList.this);
            dialog.setContentView(R.layout.maindialog);
            dialog.setCancelable(true);

        Button button = (Button) dialog.findViewById(R.id.cancel);
        button.setOnClickListener(new OnClickListener()  {
                @Override
                public void onClick(View v)  {
                    dialog.dismiss();
                }
            });
            dialog.show();

        Button ok = (Button) dialog.findViewById(R.id.ok);
        ok.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                items.add(0, et.getText().toString());
                adapter.notifyDataSetChanged();
                dialog.dismiss();
                et.setText("");
        }

            }
        );
       }
        });
    }
}

2 Answers 2

2

Add

setContentView(R.layout.main);

after

super.onCreate(savedInstanceState);

that will solve your problem.

Sign up to request clarification or add additional context in comments.

3 Comments

Thanks, but now, when I click OK in my dialog, it stops unexpectedly. LogCat has another nullpointerexception, it looks like a bad onClick code (can't copy-paste the LogCat, too many characters).
Button button = (Button) dialog.findViewById(R.id.cancel); is giving you the error..Use Button button = (Button) findViewById(R.id.cancel) instead
Nope, still crashes when I press ok. LogCat has a nullpointerexception at com.shoppinglist.ShoppingList$1$2.onClick(ShoppingList.java:50) at android.view.View.performClick(View.java:2344) at android.view.View.onTouchEvent(View.java:4133) at android.widget.TextView.onTouchEvent(TextView.java:6510) at android.view.View.dispatchTouchEvent(View.java:3672) at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:882) at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:882) at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:882) There are 3 more dispatchtouchevent errors.
0

In Eclipse, use Window > Show View ...> Android > LogCat to display the log message and look for a related exception to explain the crash.

1 Comment

Why not this should be a comment?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.