我正在开发一个显示列表视图的Android应用程序。当我点击listview的一些项目时,它应该打开一个对话框,其中包括另一个(自定义)列表视图。对话框中的列表视图有两个文本视图和一个(图像)按钮(我试过两个)。但我总是得到一个nullpointerException,我不知道,我做错了什么。
这是我的代码
public class Schuldet_mir extends Fragment {
private ListView mainList;
private ListView subList;
.....
MainActivity mainActivity = (MainActivity) getActivity();
.....
public Schuldet_mir(int position) {
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { //onCreate
View v = inflater.inflate(R.layout.fragment_schuldet_mir, container, false);
mainList = (ListView)v.findViewById(R.id.listView1);
imageButton = (ImageButton)v.findViewById(R.id.imageBtn1);
imageButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
openDialog();
}
});
mainList.setOnItemClickListener(new OnItemClickListener() { //Hauptliste klick
@Override
public void onItemClick(AdapterView<?> parent, View v, int position, long idInDB) {
mainListViewClick(parent, v, position, idInDB);
}
});
return v;
}
@Override //onResume
public void onResume() {
super.onResume();
populateListViewFromDB();
}
private void openDialog() {
final Dialog dialog1 = new Dialog(getActivity());
dialog1.setContentView(R.layout.dialog1);
dialog1.setTitle("Schuldner hinzufügen");
dialog1.setCancelable(false);
eintrag_name = (EditText)dialog1.findViewById(R.id.editText_name);
eintrag_betrag = (EditText)dialog1.findViewById(R.id.editText_betrag);
eintrag_grund = (EditText)dialog1.findViewById(R.id.editText1);
ImageButton hinzufügen = (ImageButton)dialog1.findViewById(R.id.imageButton_add);
ImageButton abbrechen = (ImageButton)dialog1.findViewById(R.id.imageButton_cancel);
hinzufügen.setOnClickListener(new OnClickListener() { //Buttonklick Hinzufügen
@Override
public void onClick(View v) {
//do s.th.
}
}
});
abbrechen.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
dialog1.dismiss();
}
});
dialog1.show();
}
private void mainListViewClick(AdapterView<?> parent, View v, int position, long idInDB) {
final Dialog dialog3 = new Dialog(getActivity());
dialog3.setContentView(R.layout.dialog3);
dialog3.setTitle("Schuldner");
dialog3.setCancelable(false);
subList = (ListView)dialog3.findViewById(R.id.dialog3_listView);
Button deleteBtn = (Button)dialog3.findViewById(R.id.dialog3_deleteBtn);
Button allesErhalten = (Button)dialog3.findViewById(R.id.dialog3_btn1);
Button zurück = (Button)dialog3.findViewById(R.id.dialog3_btn2);
populateSubListViewFromDB(idInDB);
subList.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View v, int position, long idInDB) {
subListViewClick(parent, v, position, idInDB);
}
});
deleteBtn.setOnClickListener(new OnClickListener() { **// line:193** /
@Override
public void onClick(View v) {
Log.e("++++++++", "TEST OK");
}
});
allesErhalten.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
}
});
zurück.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
dialog3.dismiss();
}
});
dialog3.show();
}
// --------------------------------------------------------------------
private void subListViewClick(AdapterView<?> parent, View v, int position, long idInDB) { // SubList Item Click
cursor = mainActivity.myDBHandler.getRowinTable1(idInDB);
if(cursor.moveToFirst()) { //informationen der zeile
idDB = cursor.getLong(DataBaseHandler.COL_ROWID);
dialog2_name = cursor.getString(DataBaseHandler.COL_NAME);
dialog2_betrag = cursor.getFloat(DataBaseHandler.COL_BETRAG);
dialog2_grund = cursor.getString(DataBaseHandler.COL_GRUND);
dialog2_datum = cursor.getString(DataBaseHandler.COL_DATUM);
}
final Dialog dialog2 = new Dialog(getActivity()); //open dialog
dialog2.setContentView(R.layout.dialog2);
dialog2.setTitle("Schuldner");
dialog2.setCancelable(false);
TextView dial2_TxtDatum = (TextView)dialog2.findViewById(R.id.dialog2_TxtDatum);
TextView dial2_TxtName = (TextView)dialog2.findViewById(R.id.dialog2_TxtName);
TextView dial2_TxtGrund = (TextView)dialog2.findViewById(R.id.dialog2_TxtGrund);
TextView dial2_TxtBetrag = (TextView)dialog2.findViewById(R.id.dialog2_TxtBetrag);
Button erhalten = (Button)dialog2.findViewById(R.id.dialog2_btnBezahlt);
Button ok = (Button)dialog2.findViewById(R.id.dialog2_btnOk);
NumberFormat currencyFormatter = NumberFormat.getCurrencyInstance(Locale.getDefault());
String currencyOut = currencyFormatter.format(dialog2_betrag);
dial2_TxtDatum.setText("Vom " + dialog2_datum + " Uhr");
dial2_TxtName.setText(dialog2_name);
dial2_TxtGrund.setText(dialog2_grund);
dial2_TxtBetrag.setText(currencyOut);
erhalten.setOnClickListener(new OnClickListener() { //Buttonklick für erhalten
@Override
public void onClick(View v) {
mainActivity.myDBHandler.deleteRowInTable1(idDB);
populateListViewFromDB();
dialog2.dismiss();
}
});
ok.setOnClickListener(new OnClickListener() { //Buttonklick für ok
@Override
public void onClick(View v) {
dialog2.dismiss();
}
});
dialog2.show();
}
// --------------------------------------------------------------------
@SuppressWarnings("deprecation")
public void populateListViewFromDB() { ...
}
// --------------------------------------------------------------------
@SuppressWarnings("deprecation")
public void populateSubListViewFromDB(long idInDB) {
getActivity().startManagingCursor(cursor = mainActivity.myDBHandler.getAllEntriesFromAPerson1(idInDB));
String[] dbSpaltenNamen = new String[] {"d", "b"};
int[] zuListViewIDs = new int[] {R.id.dialog3_datum, R.id.dialog3_lz_betrag};
SimpleCursorAdapter myCursorAdapter
= new SimpleCursorAdapter
(getActivity(), R.layout.dialog3_listzeile, cursor, dbSpaltenNamen, zuListViewIDs);
subList.setAdapter(myCursorAdapter);
}
// --------------------------------------------------------------------
@Override
public void onAttach(Activity activity) {
super.onAttach(activity);
try {
mainActivity = (MainActivity) activity;
}
catch (ClassCastException e) {
throw new ClassCastException(activity.toString() + " Fehler: Keine MainActivity-Instanz!");
}
}
}
logcat消息:
01-10 18:10:19.392: E/AndroidRuntime(14649): FATAL EXCEPTION: main
01-10 18:10:19.392: E/AndroidRuntime(14649): java.lang.NullPointerException
01-10 18:10:19.392: E/AndroidRuntime(14649): at fragments.Schuldet_mir.mainListViewClick(Schuldet_mir.java:193)
01-10 18:10:19.392: E/AndroidRuntime(14649): at fragments.Schuldet_mir.access$1(Schuldet_mir.java:171)
01-10 18:10:19.392: E/AndroidRuntime(14649): at fragments.Schuldet_mir$2.onItemClick(Schuldet_mir.java:90)
01-10 18:10:19.392: E/AndroidRuntime(14649): at android.widget.AdapterView.performItemClick(AdapterView.java:298)
01-10 18:10:19.392: E/AndroidRuntime(14649): at android.widget.AbsListView.performItemClick(AbsListView.java:1111)
01-10 18:10:19.392: E/AndroidRuntime(14649): at android.widget.AbsListView$PerformClick.run(AbsListView.java:2900)
01-10 18:10:19.392: E/AndroidRuntime(14649): at android.widget.AbsListView$1.run(AbsListView.java:3728)
01-10 18:10:19.392: E/AndroidRuntime(14649): at android.os.Handler.handleCallback(Handler.java:615)
01-10 18:10:19.392: E/AndroidRuntime(14649): at android.os.Handler.dispatchMessage(Handler.java:92)
01-10 18:10:19.392: E/AndroidRuntime(14649): at android.os.Looper.loop(Looper.java:137)
01-10 18:10:19.392: E/AndroidRuntime(14649): at android.app.ActivityThread.main(ActivityThread.java:4872)
01-10 18:10:19.392: E/AndroidRuntime(14649): at java.lang.reflect.Method.invokeNative(Native Method)
01-10 18:10:19.392: E/AndroidRuntime(14649): at java.lang.reflect.Method.invoke(Method.java:511)
01-10 18:10:19.392: E/AndroidRuntime(14649): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:790)
01-10 18:10:19.392: E/AndroidRuntime(14649): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:557)
01-10 18:10:19.392: E/AndroidRuntime(14649): at dalvik.system.NativeStart.main(Native Method)
第193行是deleteBtn.setOnClickListener方法。我做错了什么!?提前感谢建议
编辑: dialog3.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:orientation="vertical" >
<ListView
android:id="@+id/dialog3_listView"
android:layout_width="match_parent"
android:layout_height="0dip"
android:layout_weight="0.98" >
</ListView>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="46dp" >
<Button
android:id="@+id/dialog3_btn1"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_gravity="bottom"
android:layout_weight="1"
android:text="Alles erhalten" />
<Button
android:id="@+id/dialog3_btn2"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_gravity="bottom"
android:layout_weight="1"
android:text="Zurück" />
</LinearLayout>
dialog3_listzeile:这是listview的自定义行
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<TextView
android:id="@+id/dialog3_datum"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:text="Medium Text"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textStyle="italic" />
<Button
android:id="@+id/dialog3_deleteBtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:focusable="false"
android:text="Delete" />
<TextView
android:id="@+id/dialog3_lz_betrag"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="@+id/dialog3_deleteBtn"
android:layout_centerHorizontal="true"
android:text="Large Text"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textStyle="bold" />
答案 0 :(得分:0)
您尝试在活动中找到dialog3_deleteBtn
layout
。这不起作用,因为它是row
中ListView
的一部分。
您必须编写一个自定义适配器,它可以处理每行内的按钮。
此外,正如我在评论部分所述,您不应在代码中使用'ä','ü','ö'或类似的字母。 对于'ü',您可以使用'ue'代替。
Zudem solltest dudirangewöhnenmöglichstauchauf Englisch zu'coden',damitmöglichstvieledeinen Code besser verstehen。
答案 1 :(得分:0)
dialog3.xml
没有标识为R.id.dialog3_deleteBtn
findViewById
在当前膨胀的布局中查找id为id的视图。所以它会给你NullPointerException
。
你在dialog3_listzeile.xml
<Button
android:id="@+id/dialog3_deleteBtn"
android:layout_width="wrap_content"
应该是
Button deleteBtn = (Button)dv.findViewById(R.id.dialog3_deleteBtn);
除此之外。
MainActivity mainActivity = (MainActivity) getActivity();
也错了
getActivity()
将返回null。将其移至onCreateView