문제

In my main file, in private class MyDiary I set OnItemLongClickListener like this:

ListView list = getListView();
        list.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
                public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
                    new EditListItemDialog(view.getContext()).show();

                return true;       
            }
        });

Then, inEditListItemDialog file I attempt to add new value to the table of rows(table is displayed in previous file):

@Override
public void onClick(View v) {
    fragment_monday.add(((TextView) v).getText().toString());//here is your updated(or not updated) text
    dismiss();
}

fragment_monday is an xml for my first file to utilize the list view like this:

<LinearLayout
                    android:id="@+id/linearLayout3"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_alignParentLeft="true"
                    android:layout_alignParentRight="true"
                    android:layout_alignParentTop="true"
                    android:padding="0dp" >



                    <RelativeLayout
                        android:id="@+id/relativeLayout4"
                        android:layout_width="0dp"
                        android:layout_height="match_parent"
                        android:layout_weight="1" >

                         <ListView
    android:layout_width="fill_parent"
                    android:gravity="center"
    android:layout_height="fill_parent"
    android:id="@android:id/list"
    android:longClickable="true"
     >

</ListView>



             </RelativeLayout>



                </LinearLayout>

At the end, when I click confirm button in EditListItemDialog to change the data, the app crashes and I get NullPointer Exception at line 37 which is second snipped, 3rd line. I am guessing that fragment_monday is not set properly but I don't really know what to do with it in order to make this thing work. Can anybody help?

Here is the LogCat output:

04-21 09:18:53.009: E/AndroidRuntime(5483): FATAL EXCEPTION: main
04-21 09:18:53.009: E/AndroidRuntime(5483): java.lang.NullPointerException
04-21 09:18:53.009: E/AndroidRuntime(5483):     at com.example.classorganizer.EditListItemDialog.onClick(EditListItemDialog.java:37)
04-21 09:18:53.009: E/AndroidRuntime(5483):     at android.view.View.performClick(View.java:2485)
04-21 09:18:53.009: E/AndroidRuntime(5483):     at android.view.View$PerformClick.run(View.java:9080)
04-21 09:18:53.009: E/AndroidRuntime(5483):     at android.os.Handler.handleCallback(Handler.java:587)
04-21 09:18:53.009: E/AndroidRuntime(5483):     at android.os.Handler.dispatchMessage(Handler.java:92)
04-21 09:18:53.009: E/AndroidRuntime(5483):     at android.os.Looper.loop(Looper.java:130)
04-21 09:18:53.009: E/AndroidRuntime(5483):     at android.app.ActivityThread.main(ActivityThread.java:3687)
04-21 09:18:53.009: E/AndroidRuntime(5483):     at java.lang.reflect.Method.invokeNative(Native Method)
04-21 09:18:53.009: E/AndroidRuntime(5483):     at java.lang.reflect.Method.invoke(Method.java:507)
04-21 09:18:53.009: E/AndroidRuntime(5483):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:867)
04-21 09:18:53.009: E/AndroidRuntime(5483):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:625)
04-21 09:18:53.009: E/AndroidRuntime(5483):     at dalvik.system.NativeStart.main(Native Method)

I also tried moving first snipped of code from private class MyDiary to private class MyAdapter extends BaseAdapter but no change.

Full code to EditListItemDialog:

package com.example.classorganizer;

import java.util.List;

import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;

class EditListItemDialog extends Dialog implements View.OnClickListener {

private View editText;

public EditListItemDialog(Context context) {
    super(context);
}

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.edit_text_dialog);//here is your xml with EditText and 'Ok' and 'Cancel' buttons
    View btnOk = findViewById(R.id.button_ok);
    editText = findViewById(R.id.edit_text);
    btnOk.setOnClickListener(this);
}

private List<String> fragment_monday;

public EditListItemDialog(Context context, List<String> fragment_monday) {
    super(context);
    this.fragment_monday = fragment_monday;
}

@Override
public void onClick(View v) {
    fragment_monday.add(((TextView) v).getText().toString());//here is your updated(or not updated) text
    dismiss();
}
}
도움이 되었습니까?

해결책

As I understant, you have two constructors for EditListItemDialog. One with fragment_monday and another without it. So, if you create your object with the help of first constructor, fragment_monday was equal to null.

Just replace

public EditListItemDialog(Context context) {
    super(context);
}

With this

public EditListItemDialog(Context context) {
    super(context);
    this.fragment_monday = new  List<String>();
}

I don't know whether all will be ok, but it will prevent Null Pointer Exception;

다른 팁

You have two constructors in EditListItemDialog, out of which only the other initializes fragment_monday. In your onItemLongClick() code you're using the 1-arg constructor that doesn't initialize that variable. onClick() uses fragment_monday and there's your NPE.

To fix it, either

  • remove the 1-arg constructor and use the 2-arg constructor only, or

  • initialize fragment_monda to reasonable defaults in the 1-arg constructor as well.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top