Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/dart/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 如何初始化,我得到空指针异常_Android_Dialog_Nullpointerexception - Fatal编程技术网

Android 如何初始化,我得到空指针异常

Android 如何初始化,我得到空指针异常,android,dialog,nullpointerexception,Android,Dialog,Nullpointerexception,在我的主文件中,在private class MyDiary中,我将onimlongclicklistener设置为: ListView list = getListView(); list.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() { public boolean onItemLongClick(AdapterView<?> parent,

在我的主文件中,在
private class MyDiary
中,我将
onimlongclicklistener
设置为:

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;       
            }
        });
<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>
fragment\u monday
是我第一个使用列表视图的文件的xml,如下所示:

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;       
            }
        });
<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>
我还尝试将第一段代码从私有类MyDialog移动到私有类MyAdapter Extendes BaseAdapter,但没有任何更改

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();
}
}
package com.example.classorganizer;
导入java.util.List;
导入android.app.Dialog;
导入android.content.Context;
导入android.os.Bundle;
导入android.view.view;
导入android.widget.TextView;
类EditListItemDialog扩展对话框实现View.OnClickListener{
私有视图编辑文本;
公共EditListItemDialog(上下文){
超级(上下文);
}
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.edit_text_dialog);//这是带有EditText和“Ok”和“Cancel”按钮的xml
查看btnOk=findviewbyd(R.id.button_ok);
editText=findviewbyd(R.id.edit\u text);
btnOk.setOnClickListener(这个);
}
星期一的私人名单;
公共EditListItemDialog(上下文上下文,列表片段){
超级(上下文);
this.fragment\u monday=fragment\u monday;
}
@凌驾
公共void onClick(视图v){
fragment_monday.add(((TextView)v.getText().toString());//这是您更新(或未更新)的文本
解雇();
}
}

EditListItemDialog
中有两个构造函数,其中只有其他构造函数初始化
fragment\u monday
。在
onItemLongClick()
代码中,您使用的是不初始化该变量的1-arg构造函数
onClick()
使用
fragment\u monday
这就是您的NPE

也要解决它

  • 删除1-arg构造函数并仅使用2-arg构造函数,或

  • 在1-arg构造函数中将
    fragment\u monda
    初始化为合理的默认值


据我所知,EditListItemDialog有两个构造函数。一个是星期一,另一个是星期一。所以,如果在第一个构造函数的帮助下创建对象,fragment_monday等于null

替换

public EditListItemDialog(Context context) {
    super(context);
}
用这个

public EditListItemDialog(Context context) {
    super(context);
    this.fragment_monday = new  List<String>();
}
公共EditListItemDialog(上下文){ 超级(上下文); this.fragment_monday=新列表(); }
我不知道是否一切正常,但它将防止空指针异常

“碎片星期一”是什么?@PareshMayani 30秒碎片星期一可能应该是一种列表,不是吗?你在哪里实例化它?@lisoslaw这30秒是什么?我要求你发布
logcat
输出,以便我们能够分析它并提出解决方案。@PareshMayani我说了30秒,它将被发布。现在发布了。正如laalto所说的,只需停止使用第一个构造函数。如果您想使用fragment_monday,您需要在EditListItemDialog构造函数中传递正确的参数List fragment_monday。您是否了解使用fragment_monday变量的目的?将新数据导入列表I have private List fragment_monday;在EditListItemDialogy中,是的。但如果您使用第一个构造函数创建EditListItemDialog,它将不会初始化。如果对其他类不重要,可以在第一个构造函数中添加字符串this.fragment_monday=new List()来初始化此变量。