Using a list view in a custom dialogue - android

How would I use a list in this custom dialogue? ive set a list int the .xml of the listview but am unfamiliar with how to use it properly (I want the values for the list to come from a string array)
My main java:
package custom.dialouge.list;
import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;
public class CustomDialougeListTestActivity extends Activity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
final Context mContext = this;
final Context context = this;
Button button;
button = (Button) findViewById(R.id.button01);
// add button listener
button.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
// custom dialog
final Dialog dialog = new Dialog(context);
dialog.setContentView(R.layout.list);
dialog.setTitle("List");
// set the custom dialog components - text, image and button
TextView text = (TextView) dialog.findViewById(R.id.TextView01);
text.setText("Test");
Button dialogButton = (Button) dialog.findViewById(R.id.Button01);
// if button is clicked, close the custom dialog
dialogButton.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
dialog.dismiss();
}
});
dialog.show();
}
});
}
}
My custom dialouge .xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<Button
android:id="#+id/Button01"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:text="Back" />
<TextView
android:id="#+id/TextView01"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:text="TextView" />
<ListView
android:id="#+id/listview1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_above="#+id/Button01"
android:layout_alignParentLeft="true"
android:layout_below="#+id/TextView01" >
</ListView>
</RelativeLayout>

Try something like this:
ArrayList<String> myStringArray = ... // insert your code to get/create the array here
ListView view = dialog.findViewById(R.id.listview1);
ArrayAdapter<String> listAdapter = new ArrayAdapter<String> ( this, android.R.layout.simple_list_item_1, myStringArray);
view.setAdapter(listAdapter);
Like so:
public class CustomDialougeListTestActivity extends Activity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
final Context mContext = this;
final Context context = this;
Button button;
ArrayList<String> myStringArray = ... // insert your code to get/create the array here
ArrayAdapter<String> listAdapter = new ArrayAdapter<String> ( this, android.R.layout.simple_list_item_1, myStringArray);
button = (Button) findViewById(R.id.button01);
// add button listener
button.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
// custom dialog
final Dialog dialog = new Dialog(context);
dialog.setContentView(R.layout.list);
dialog.setTitle("List");
// set the custom dialog components - text, image and button
TextView text = (TextView) dialog.findViewById(R.id.TextView01);
text.setText("Test");
ListView view = dialog.findViewById(R.id.listview1);
view.setAdapter(listAdapter);
Button dialogButton = (Button) dialog.findViewById(R.id.Button01);
// if button is clicked, close the custom dialog
dialogButton.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
dialog.dismiss();
}
});
dialog.show();
}
});
}
}

Related

Eclipse Android Checkbox

I would like to create checkbox with text that would behave something like what i have below in pseudo code:
Set in text in EditText field
if Button clicked:
create new Checkbox with entered text.
How do i go about doing that in Android?
So far ive got the button :) ... but dont know how to get new Checkbox with text in it ,if its clicked
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.text);
Button create = (Button) findViewById(R.id.button1);
create.setOnClickListener(this);
EditText textInCheckBox = (EditText) findViewById(R.id.editText1);
}
#Override
public void onClick(View v) {
// TODO Automatisch generierter Methodenstub
switch (v.getId()) {
case R.id.button1:
//after creating new checkbox with text in it
//Editbox should restet text field to null;
break;
}
}
}
Here is some code that will demonstrate how to add checkboxes dynamically with the title you entered into the EditText.
MainActivity.java
package com.example.test;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup.LayoutParams;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.RadioGroup;
import android.widget.Toast;
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final EditText txtTitle = (EditText) findViewById(R.id.txtTitle);
final Button btnCreate = (Button) findViewById(R.id.btnCreate);
final RadioGroup radContainer = (RadioGroup) findViewById(R.id.radContainer);
final LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
btnCreate.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
String name = txtTitle.getText().toString().trim();
if (!name.isEmpty()) {
CheckBox checkBox = new CheckBox(MainActivity.this);
checkBox.setText(name);
checkBox.setLayoutParams(params);
radContainer.addView(checkBox);
} else {
Toast.makeText(MainActivity.this, "Enter a title for the checkbox", Toast.LENGTH_SHORT).show();
}
}
});
}
}
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center_horizontal"
android:orientation="vertical"
android:padding="16dp" >
<EditText
android:id="#+id/txtTitle"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:imeOptions="actionDone" />
<Button
android:id="#+id/btnCreate"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Create Checkbox" />
<RadioGroup
android:id="#+id/radContainer"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>

Own layout in Dialog Fragment

Well, I'm testing "Dialog Fragment" and I've done a little program that when it starts show a Dialog Fragment with 3 options, the TextView on the main activity will show which option was selected. So basicly it's communication between Dialog and Activity. I was following an example with 2 buttons(positive and negative) but now I'm testing with my own layout with 3 button and I don't know how to continue...
Let's see the code:
3 Button in dialog_layout.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="1" />
<Button
android:id="#+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="2" />
<Button
android:id="#+id/button3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="3" />
</LinearLayout>
Then the DialogFragment class
import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.support.v4.app.DialogFragment;
import android.view.LayoutInflater;
import android.webkit.WebView.FindListener;
import android.widget.Button;
public class TwoActionButtonsDialog extends DialogFragment {
private DialogListener listener;
public interface DialogListener {
public void onDialogOption1(DialogFragment dialog);
public void onDialogOption2(DialogFragment dialog);
public void onDialogOption3(DialogFragment dialog);
}
// Override the Fragment.onAttach() method to instantiate the
// NoticeDialogListener
#Override
public void onAttach(Activity activity) {
super.onAttach(activity);
// Verify that the host activity implements the callback interface
try {
// Instantiate the NoticeDialogListener so we can send events to the
// host
listener = (DialogListener) activity;
} catch (ClassCastException e) {
// The activity doesn't implement the interface, throw exception
throw new ClassCastException(activity.toString()
+ " must implement DialogListener");
}
}
#Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
// Use the Builder class for convenient dialog construction
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
LayoutInflater inflater = getActivity().getLayoutInflater();
builder.setTitle("Test").setView(
inflater.inflate(R.layout.dialog_layout, null));
Button btn1 = ((Button) findViewById(R.id.button1));
Button btn2 = (Button) findViewById(R.id.button2);
Button btn3 = (Button) findViewById(R.id.button3);
// Create the AlertDialog object and return it
return builder.create();
}
}
And the main activity
import android.os.Bundle;
import android.support.v4.app.DialogFragment;
import android.support.v4.app.FragmentActivity;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends FragmentActivity implements
TwoActionButtonsDialog.DialogListener {
private static final String TAG = "dialog";
private TextView texto = null;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
texto = (TextView) findViewById(R.id.textView1);
showTwoActionButton();
}
public void showTwoActionButton() {
DialogFragment dialog = new TwoActionButtonsDialog();
dialog.show(getSupportFragmentManager(), TAG);
}
// The dialog fragment receives a reference to this Activity through the
// Fragment.onAttach() callback, which it uses to call the following methods
// defined by the NoticeDialogFragment.NoticeDialogListener interface
#Override
public void onDialogOption1(DialogFragment dialog) {
// User touched the dialog's positive button
texto.setText("1");
}
#Override
public void onDialogOption2(DialogFragment dialog) {
// User touched the dialog's negative button
texto.setText("2");
}
#Override
public void onDialogOption3(DialogFragment dialog) {
// TODO Auto-generated method stub
texto.setText("3");
}
}
I'm not sure how to manage the buttons beacause I can't do:
Button btn1 = ((Button) findViewById(R.id.button1));
Button btn2 = (Button) findViewById(R.id.button2);
Button btn3 = (Button) findViewById(R.id.button3);
it throws an error: "The method findViewById(int) is undefined for the type TwoActionButtonsDialog". If I've inflated the layout why I can't acces to them?
What should I do?
After you inflate your view for a Dialog, save a result:
LayoutInflater inflater = getActivity().getLayoutInflater();
View v = inflater.inflate(R.layout.dialog_layout, null)
builder.setTitle("Test").setView(v);
After that you can traverse your view for buttons:
Button btn1 = (Button) v.findViewById(R.id.button1);

Android close custom dialog within layout

I am looking for a way to close a custom dialog with a button that is inside the xml used in the dialog, alternatively closing it by pressing anywhere on the dialog. What I have is this; a layout with a Image Button that brings up the custom dialog with the content. I have setCanceledOnTouchOutside(true); and that works, but I need the dialog to fill up most of the screen and it can be hard for the user to click in the small space that is available. So how do I do this?
My java code:
import android.app.Activity;
import android.app.Dialog;
import android.content.pm.ActivityInfo;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageButton;
public class Rose extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
this.setRequestedOrientation(
ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
setContentView(R.layout.rose);
ImageButton b = (ImageButton) findViewById(R.id.imageButton1);
b.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
Dialog d1 = new Dialog(Rose.this);
d1.setContentView(R.layout.tariquet);
d1.setCanceledOnTouchOutside(true);
d1.show();
}
});
}
}
And my XML:
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:fitsSystemWindows="true"
android:isScrollContainer="true"
android:minHeight="1100dp"
android:minWidth="650dp">
<ImageView
android:src="#drawable/rose_tariquet"
android:id="#+id/imageView1"
android:layout_height="fill_parent"
android:layout_width="fill_parent"></ImageView>
<Button android:text="X"
android:layout_height="wrap_content"
android:id="#+id/button1"
android:layout_width="55dp"
android:layout_gravity="right"></Button>
</FrameLayout>
public class CustomizeDialog extends Dialog implements OnClickListener {
Button close;
TextView tv;
public CustomizeDialog(Context context,String Stringcontent) {
super(context);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.custom_diolog_main);
tv=(TextView) findViewById(R.id.content);
tv.setText(Stringcontent);
close = (Button) findViewById(R.id.close);
close.setOnClickListener(this);
}
#Override
public void onClick(View v) {
if (v == close)
dismiss();
}
}
called:
CustomizeDialog customizeDialog = new CustomizeDialog(CustomDialog.this,"clickme");
customizeDialog.show();

EditText.getText Java.lang nullpointer exception

I'm having a problem with my android program.
I have about every thing working what i want, but now im trying to add a password dialog.
I want to show a Dialog with a EditText(enter password) and two buttons (ok, cancel).
(working fine till so far)
When clicking on the OK button the password should be saved in mij SendData Class.
But every time I try to get the content of the EditText it gives a Java.lang NullPointerException.
Can some one help me please?
(I don't have a stack trace as i don't know where to find it :( because i'm testing on android emulator.)
If there any thing else you need, please feel free to ask.
Here is the XML-FILE
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<requestFocus />
<EditText
android:id="#+id/txt_wachtwoord"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:inputType="textPassword" >
<requestFocus />
</EditText>
<Button
android:id="#+id/DialogOK"
android:layout_width="100dp"
android:layout_height="wrap_content"
android:layout_alignRight="#+id/txt_wachtwoord"
android:layout_below="#+id/txt_wachtwoord"
android:layout_marginTop="22dp"
android:text="OK" />
<Button
android:id="#+id/Cancel"
android:layout_width="100dp"
android:layout_height="wrap_content"
android:layout_alignBaseline="#+id/DialogOK"
android:layout_alignBottom="#+id/DialogOK"
android:layout_toRightOf="#+id/DialogOK"
android:text="Cancel" />
</RelativeLayout>
Here is the code:
package SebApp.Phone.Remote;
import java.net.DatagramSocket;
import android.app.Activity;
import android.app.Dialog;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
import android.widget.EditText;
import android.widget.Toast;
import android.os.Vibrator;
import android.app.AlertDialog;
import android.content.DialogInterface;
public class PhoneRemote extends Activity {
private static final int VIBRATE_TIME = 100;
/** Called when the activity is first created. */
public Vibrator vibrator = null;
public sendData sd = new sendData();
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
vibrator = (Vibrator)getSystemService(VIBRATOR_SERVICE);
//button to call the Dialog
Button ww = (Button) findViewById(R.id.ww);
//listener password button
ww.setOnClickListener(wwOnClickListener);
}
Button.OnClickListener wwOnClickListener = new Button.OnClickListener() {
public void onClick(View v) {
try{
final EditText ww = (EditText) findViewById(R.id.txt_wachtwoord);
//set up dialog
Dialog dialog = new Dialog(PhoneRemote.this);
dialog.setContentView(R.layout.wachtwoord);
dialog.setTitle("This is my custom dialog box");
dialog.setCancelable(true);
//there are a lot of settings, for dialog, cheout!
//set up button
Button button = (Button) dialog.findViewById(R.id.DialogOK);
button.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
try{
Toast.makeText(v.getContext(),"PWfield contains: " + ww.getText().toString(), Toast.LENGTH_SHORT).show();
//finish();
}catch(Exception e)
{
Toast.makeText(v.getContext(),e.toString(), Toast.LENGTH_SHORT).show();
}
}
});
dialog.show();
}catch(Exception E){
Toast.makeText(v.getContext(),E.toString(), Toast.LENGTH_SHORT).show();
}
}
};
}
Button.OnClickListener wwOnClickListener = new Button.OnClickListener() {
public void onClick(View v) {
try{
final EditText ww = (EditText) findViewById(R.id.txt_wachtwoord);
//set up dialog
Dialog dialog = new Dialog(PhoneRemote.this);
dialog.setContentView(R.layout.wachtwoord);
dialog.setTitle("This is my custom dialog box");
dialog.setCancelable(true);
....
instead use
Button.OnClickListener wwOnClickListener = new Button.OnClickListener() {
public void onClick(View v) {
try{
Dialog dialog = new Dialog(PhoneRemote.this);
dialog.setContentView(R.layout.wachtwoord);
final EditText ww = (EditText) dialog.findViewById(R.id.txt_wachtwoord);
//set up dialog
dialog.setTitle("This is my custom dialog box");
dialog.setCancelable(true);
....
you had declared EditText before setting the dialog view
I have yet to run your code. However, I would say the variable wwOnClickListener is being initialized before the onCreate method.
Therefore the layout is not set, as the layout is set within the onCreate method. To fix this I would pull out the following
final EditText ww = (EditText) findViewById(R.id.txt_wachtwoord);
From the buttonlistener and make ww a global variable which is initialized in the onCreate method.
I would expect that will resolve the issue.

Android App Crashes on Startup due to java.lang.NullPointerException at onClick

I want to make it so that a user clicks a button to launch a dialog. It crashes when I click the button. Here's the logcat:
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) at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:882) at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:882) at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:882)
Here's 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);
setContentView(R.layout.main);
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);
Button button1main = (Button) findViewById(R.id.add);
button1main.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
final Dialog additem = new Dialog(ShoppingList.this);
additem.setContentView(R.layout.maindialog);
additem.setTitle("Type your item");
additem.setCancelable(true);
et.setHint("Type the name of an item...");
Button button = (Button) findViewById(R.id.cancel);
button.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
additem.dismiss();
}
});
additem.show();
Button ok = (Button) findViewById(R.id.ok);
ok.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
items.add(et.getText().toString());
adapter.notifyDataSetChanged();
additem.dismiss();
et.setText("");
}
}
);
}
});
}
}
Here's my main.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#FF9900"
android:padding="10dp"
android:orientation="vertical">
<Button
android:id="#+id/add"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Add item..."
android:layout_gravity="center"/>
<ListView android:id="#android:id/list"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#FFCC00"/>
</LinearLayout>
Here's my dialog xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="#+id/layout_root"
android:orientation="vertical"
android:background="#FFFFFF"
android:minHeight="100dp"
android:minWidth="300dp">
<EditText
android:id="#+id/edittext"
android:layout_width="fill_parent"
android:layout_height="wrap_content"/>
<LinearLayout
android:id="#+id/button_layout"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="horizontal"
android:layout_gravity="bottom">
<Button
android:id="#+id/ok"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Ok"/>
<Button
android:id="#+id/cancel"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Cancel"/>
</LinearLayout>
</LinearLayout>
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
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);
Button button1main = (Button) findViewById(R.id.add);
In your code, you use setContentView to R.layout.main. Then you try to find R.id.edittext in the R.layout.main but edittext exists in maindialog.xml.
So when you call
final EditText et = (EditText) findViewById(R.id.edittext);
et will be null and any method call on et will result of a NullPointerException
Edit
Try this:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
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);
Button button1main = (Button) findViewById(R.id.add);
button1main.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
final Dialog additem = new Dialog(ShoppingList.this);
additem.setContentView(R.layout.maindialog);
final EditText et = (EditText)additem.findViewById(R.id.edittext);
additem.setTitle("Type your item");
additem.setCancelable(true);
et.setHint("Type the name of an item...");
Button button = (Button) additem.findViewById(R.id.cancel);
button.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
additem.dismiss();
}
});
additem.show();
Button ok = (Button) additem.findViewById(R.id.ok);
ok.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
items.add(et.getText().toString());
adapter.notifyDataSetChanged();
additem.dismiss();
et.setText("");
}
});
}
});
If I've got my line numbers correct, the null pointer exception is occurring in this block:
new OnClickListener() {
#Override
public void onClick(View v) {
items.add(et.getText().toString());
adapter.notifyDataSetChanged();
additem.dismiss();
et.setText("");
}
The error is saying that one of the variables you are accessing is null. In this function you are accessing et, et.getText(), items, adapter, and additem. I'd suggest debugging this section (through eclipse, or printing log statements) to determine which item is null.

Categories

Resources