mj1261829
mj1261829

Reputation: 1319

Android java.lang.NullPointerException at FindViewById in a dialog

I saw many similar questions to this but still cannot resolve mine.

Here is the log:

01-26 00:30:49.714: ERROR/AndroidRuntime(616): FATAL EXCEPTION: main
        java.lang.NullPointerException
        at com.app.newItemList.onDialogPositiveClick(newItemList.java:39)
        at com.app.itemDialog$2.onClick(itemDialog.java:39)
        at com.android.internal.app.AlertController$ButtonHandler.handleMessage(AlertController.java:166)
        at android.os.Handler.dispatchMessage(Handler.java:99)
        at android.os.Looper.loop(Looper.java:137)
        at android.app.ActivityThread.main(ActivityThread.java:4745)
        at java.lang.reflect.Method.invokeNative(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:511)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
        at dalvik.system.NativeStart.main(Native Method)

Here is the relevant Java files:

 package com.app;

import android.app.DialogFragment;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.ListView;

import java.util.ArrayList;

/**
 * Created with IntelliJ IDEA.
 * User: Aljarhi
 * Date: 1/24/13
 * Time: 10:22 PM
 * To change this template use File | Settings | File Templates.
 */

public class newItemList extends FragmentActivity
        implements itemDialog.NoticeDialogListener{

    public void showNoticeDialog() {
        // Create an instance of the dialog fragment and show it
        DialogFragment dialog = new itemDialog();
        dialog.show(getFragmentManager(), "NoticeDialogFragment");
    }

    // 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 onDialogPositiveClick(DialogInterface dialog, int id) {
        // User touched the dialog's positive button
        EditText et = (EditText) findViewById(R.id.editText1);
        adapter.add(et.getText().toString());
    }

    @Override
    public void onDialogNegativeClick(DialogInterface dialog, int id) {
        // User touched the dialog's negative button
        dialog.cancel();
    }

    ArrayAdapter<String> adapter;

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.new_list);

        ListView lvc = (ListView) findViewById(R.id.listView3);

        adapter = new ArrayAdapter<String>(this, R.id.textView2, new ArrayList<String>());
        lvc.setAdapter(adapter);
    }

    public void addItem(View clickedButton)
    {
        showNoticeDialog();
    }
}

package com.app;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.app.DialogFragment;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.widget.EditText;

/**
 * Created with IntelliJ IDEA.
 * User: Aljarhi
 * Date: 1/25/13
 * Time: 3:11 PM
 * To change this template use File | Settings | File Templates.
 */
public class itemDialog extends DialogFragment
{

    public interface NoticeDialogListener {
        public void onDialogPositiveClick(DialogInterface dialog, int id);
        public void onDialogNegativeClick(DialogInterface dialog, int id);
    }

    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
        // Get the layout inflater
        LayoutInflater inflater = getActivity().getLayoutInflater();

        // Inflate and set the layout for the dialog
        // Pass null as the parent view because its going in the dialog layout
        builder.setView(inflater.inflate(R.layout.dialog_item, null))
                // Add action buttons
                .setPositiveButton("ok", new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int id) {
                        mListener.onDialogPositiveClick(dialog, id);
                    }
                })
                .setNegativeButton("cancel", new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int id) {
                        mListener.onDialogNegativeClick(dialog, id);
                    }
                });
        return builder.create();
    }

    // Use this instance of the interface to deliver action events
    NoticeDialogListener mListener;

    // 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
            mListener = (NoticeDialogListener) activity;
        } catch (ClassCastException e) {
            // The activity doesn't implement the interface, throw exception
            throw new ClassCastException(activity.toString()
                    + " must implement NoticeDialogListener");
        }
    }
}

here is the relevant xml files:

<?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:orientation="vertical" android:gravity="center_horizontal|top">

    <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Mezzo List"
            android:id="@+id/textView2" android:layout_gravity="center_horizontal|top"/>
    <ListView
            android:layout_width="wrap_content"
            android:layout_height="263dp"
            android:id="@+id/listView3" android:layout_gravity="center_horizontal|top" android:choiceMode="none"/>

    <Button
            android:layout_width="81dp"
            android:layout_height="wrap_content"
            android:text="Add Item"
            android:id="@+id/button4" android:onClick="addItem"/>

</LinearLayout>

<?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">

    <TextView
            android:layout_width="wrap_content"
            android:layout_height="25dp"
            android:text="Mezzo List"
            android:id="@+id/textView4" android:layout_gravity="center"/>
    <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Enter Item Name:"
            android:id="@+id/textView5" android:layout_gravity="center" android:textSize="20dp"/>
    <EditText
            android:layout_width="205dp"
            android:layout_height="wrap_content"
            android:text="New EditText"
            android:id="@+id/editText1" android:layout_gravity="center" android:inputType="text"
            android:hint="Item Name"
            />
</LinearLayout>

I found the problem to be in the edittext1 id not recognised or as an edittext although it appeared in the list of choices and I selected it form there. I am using intellij Idea 12.0.2. Any help will be appreciated. Thanks in advance.

Upvotes: 1

Views: 3126

Answers (2)

mj1261829
mj1261829

Reputation: 1319

Actually, I found the solution to my problem.

Inorder to for findViewById to work correctly you have first to select the view which inflates or has its content first set to the corresondong xml file. So, here is the solution:

final View textEntryView = inflater.inflate(R.layout.dialog_item, null);
EditText et = (EditText) textEntryView.findViewById(R.id.editText1);

Thanks all for your help.

Upvotes: 3

dmon
dmon

Reputation: 30168

In this line:

adapter = new ArrayAdapter<String>(this, R.id.textView2, new ArrayList<String>());

You probably don't want R.id.textView2 there. You generally want to pass in a layout which contains a textview (with the other id parameter). e.g.

adapter = new ArrayAdapter<String>(this, R.layout.somelayout, R.id.someTextViewInThatLayout, new ArrayList<String>());

Upvotes: 0

Related Questions