我在这段代码的第37行得到了NullPointer异常,就在它所说的
fragment_monday.add(((TextView) editText).getText().toString());
以下是完整档案:
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) editText).getText().toString());//here is your updated(or not updated) text
dismiss();
}
}
当我单击“对话框”中的“确认”按钮更新表格中的行时,出现此错误。 该行上的某些对象为null但我不知道如何处理这个... 有人可以帮忙吗?
编辑:如果我发布不同的代码,请告诉我。
这是logcat:
04-20 20:44:11.449: E/AndroidRuntime(2272): FATAL EXCEPTION: main
04-20 20:44:11.449: E/AndroidRuntime(2272): java.lang.NullPointerException
04-20 20:44:11.449: E/AndroidRuntime(2272): at com.example.classorganizer.EditListItemDialog.onClick(EditListItemDialog.java:37)
04-20 20:44:11.449: E/AndroidRuntime(2272): at android.view.View.performClick(View.java:2485)
04-20 20:44:11.449: E/AndroidRuntime(2272): at android.view.View$PerformClick.run(View.java:9080)
04-20 20:44:11.449: E/AndroidRuntime(2272): at android.os.Handler.handleCallback(Handler.java:587)
04-20 20:44:11.449: E/AndroidRuntime(2272): at android.os.Handler.dispatchMessage(Handler.java:92)
04-20 20:44:11.449: E/AndroidRuntime(2272): at android.os.Looper.loop(Looper.java:130)
04-20 20:44:11.449: E/AndroidRuntime(2272): at android.app.ActivityThread.main(ActivityThread.java:3687)
04-20 20:44:11.449: E/AndroidRuntime(2272): at java.lang.reflect.Method.invokeNative(Native Method)
04-20 20:44:11.449: E/AndroidRuntime(2272): at java.lang.reflect.Method.invoke(Method.java:507)
04-20 20:44:11.449: E/AndroidRuntime(2272): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:867)
04-20 20:44:11.449: E/AndroidRuntime(2272): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:625)
04-20 20:44:11.449: E/AndroidRuntime(2272): at dalvik.system.NativeStart.main(Native Method)
这里我发布xml文件以显示edit_text在那里:
<?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:orientation="vertical" >
<RelativeLayout
android:id="@+id/relativeLayout4"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1" >
<EditText
android:id="@+id/edit_text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10" >
<requestFocus />
</EditText>
<Button
android:id="@+id/button_ok"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="OK" />
</RelativeLayout>
</LinearLayout>
我设置OnItemLongClickListener的文件:
package com.example.classorganizer;
import java.util.ArrayList;
import android.app.ListActivity;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.TextView;
import com.cookbook.data.Constants;
import com.cookbook.data.MyDB;
public class Monday extends ListActivity {
private static final int MyMenu = 0;
MyDB dba;
DiaryAdapter myAdapter;
private class MyDiary{
public MyDiary(String t, String c){
title=t;
content=c;
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;
}
});
}
public String title;
public String content;
}
@Override
protected void onCreate(Bundle savedInstanceState) {
dba = new MyDB(this);
dba.open();
setContentView(R.layout.fragment_monday);
super.onCreate(savedInstanceState);
myAdapter = new DiaryAdapter(this);
this.setListAdapter(myAdapter);
}
private class DiaryAdapter extends BaseAdapter {
private LayoutInflater mInflater;
private ArrayList<MyDiary> fragment_monday;
public DiaryAdapter(Context context) {
mInflater = LayoutInflater.from(context);
fragment_monday = new ArrayList<MyDiary>();
getdata();
}
public void getdata(){
Cursor c = dba.getdiaries();
startManagingCursor(c);
if(c.moveToFirst()){
do{
String title =
c.getString(c.getColumnIndex(Constants.TITLE_NAME));
String content =
c.getString(c.getColumnIndex(Constants.CONTENT_NAME));
MyDiary temp = new MyDiary(title,content);
fragment_monday.add(temp);
} while(c.moveToNext());
}
}
@Override
public int getCount() {return fragment_monday.size();}
public MyDiary getItem(int i) {return fragment_monday.get(i);}
public long getItemId(int i) {return i;}
public View getView(int arg0, View arg1, ViewGroup arg2) {
final ViewHolder holder;
View v = arg1;
if ((v == null) || (v.getTag() == null)) {
v = mInflater.inflate(R.layout.diaryrow, null);
holder = new ViewHolder();
holder.mTitle = (TextView)v.findViewById(R.id.name);
v.setTag(holder);
} else {
holder = (ViewHolder) v.getTag();
}
holder.mdiary = getItem(arg0);
holder.mTitle.setText(holder.mdiary.title);
v.setTag(holder);
return v;
}
public class ViewHolder {
MyDiary mdiary;
TextView mTitle;
}
}
/** Called when the user clicks the Edit button */
public void visitDiary(View view) {
Intent intent = new Intent(this, Diary.class);
startActivity(intent);
}
/** Called when the user clicks the back button */
public void visitSchedule(View view) {
Intent intent = new Intent(this, DisplayScheduleScreen.class);
startActivity(intent);
}
}
这是fragment_monday xml:
的一部分<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>
答案 0 :(得分:1)
我仍然认为fragment_monday
为空,在此代码段中:
list.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
new EditListItemDialog(view.getContext()).show();
return true;
}
});
您正在使用构造函数EditListItemDialog(Context context)
创建对话框但是在onlick中他正在使用fragment_monday
。哪个没有初始化。