我在对话框上有一个搜索栏,它正在崩溃。它说有一个nullpointerexeption。 但我找不到它!我的错误在哪里? 我评论了nullpointer exeption的位置。
dialog_context_mark.xml:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/add_hw_dialog"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:padding="10dp" >
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<TextView
android:id="@+id/txt_gewicht"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Gewicht: " />
<TextView
android:id="@+id/txt_weight_change"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"/>
</RelativeLayout>
<SeekBar
android:id="@+id/seekBar_wertung"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
</LinearLayout>
mark.java
在公共类标记中,我将其定义为:
SeekBar seek_gewicht;
SeekBar seekBar_wertung;
TextView gewicht;
OnSeekBarChangeListener:
OnSeekBarChangeListener yourSeekBarListener = new OnSeekBarChangeListener() {
public void onProgressChanged(SeekBar seekBar, int progress,boolean fromUser) {
// TODO Auto-generated method stub
gewicht.setText(progress+"%");
}
public void onStartTrackingTouch(SeekBar seekBar) {
}
public void onStopTrackingTouch(SeekBar seekBar) {
seekBar.setSecondaryProgress(seekBar.getProgress());
}
};
我的对话:
@Override
protected Dialog onCreateDialog(int id) {
Dialog dialog = null;;
switch(id) {
case ADD_MARK_DIALOG:
dialog = new Dialog(mark.this);
dialog.setContentView(R.layout.dialog_context_mark);
dialog.setTitle("Note hinzufügen");
seek_gewicht = (SeekBar)findViewById(R.id.seekBar_wertung);
/***NULLPOINTEREXEPTION
seek_gewicht.setOnSeekBarChangeListener(yourSeekBarListener);
***/
}
return dialog;
}
private Button.OnClickListener add_mark = new Button.OnClickListener(){
public void onClick(View arg0) {
mDbHelper.open_database_rw();
String txt_insert_markname = insert_markname.getText().toString();
String txt_note_mark = insert_note.getText().toString();
String date_picker_message = date_pick.getDayOfMonth() + "/" + (date_pick.getMonth()+1) + "/" + date_pick.getYear();
Bundle extras = getIntent().getExtras();
String txt_sub_id = extras.getString("IDFach");
if(txt_insert_markname.equals("")){doMessage("Bitte einen Namen eintragen!");}
if(txt_note_mark.equals("")){doMessage("Bitte eine Note eintragen!");}
final String INSERT_MARK = "INSERT INTO tbl_marks ('name', 'subid', 'gewicht', 'mark', 'datum') VALUES ('"+txt_insert_markname+"', '"+txt_sub_id+"', '5', '"+txt_note_mark+"', '"+date_picker_message+"')";
db.execSQL(INSERT_MARK);
insert_note.setText("");
fillData();
}
};
LogCat :(第207行是seek_gewicht.setOnSeekBarChangeListener(yourSeekBarListener);)
02-07 10:13:58.382: D/dalvikvm(9189): GC_EXTERNAL_ALLOC freed 87K, 48% free 2916K/5511K, external 4223K/4230K, paused 21ms
02-07 10:13:58.414: D/AndroidRuntime(9189): Shutting down VM
02-07 10:13:58.414: W/dalvikvm(9189): threadid=1: thread exiting with uncaught exception (group=0x40235568)
02-07 10:13:58.418: E/AndroidRuntime(9189): FATAL EXCEPTION: main
02-07 10:13:58.418: E/AndroidRuntime(9189): java.lang.NullPointerException
02-07 10:13:58.418: E/AndroidRuntime(9189): at test.marco.notenha.mark.onCreateDialog(mark.java:207)
02-07 10:13:58.418: E/AndroidRuntime(9189): at android.app.Activity.onCreateDialog(Activity.java:2506)
02-07 10:13:58.418: E/AndroidRuntime(9189): at android.app.Activity.createDialog(Activity.java:885)
02-07 10:13:58.418: E/AndroidRuntime(9189): at android.app.Activity.showDialog(Activity.java:2581)
02-07 10:13:58.418: E/AndroidRuntime(9189): at android.app.Activity.showDialog(Activity.java:2548)
02-07 10:13:58.418: E/AndroidRuntime(9189): at test.marco.notenha.mark.onOptionsItemSelected(mark.java:113)
02-07 10:13:58.418: E/AndroidRuntime(9189): at android.app.Activity.onMenuItemSelected(Activity.java:2229)
02-07 10:13:58.418: E/AndroidRuntime(9189): at com.android.internal.policy.impl.PhoneWindow.onMenuItemSelected(PhoneWindow.java:755)
02-07 10:13:58.418: E/AndroidRuntime(9189): at com.android.internal.view.menu.MenuItemImpl.invoke(MenuItemImpl.java:143)
02-07 10:13:58.418: E/AndroidRuntime(9189): at com.android.internal.view.menu.MenuBuilder.performItemAction(MenuBuilder.java:857)
02-07 10:13:58.418: E/AndroidRuntime(9189): at com.android.internal.view.menu.IconMenuView.invokeItem(IconMenuView.java:532)
02-07 10:13:58.418: E/AndroidRuntime(9189): at com.android.internal.view.menu.IconMenuItemView.performClick(IconMenuItemView.java:123)
02-07 10:13:58.418: E/AndroidRuntime(9189): at android.view.View$PerformClick.run(View.java:9130)
02-07 10:13:58.418: E/AndroidRuntime(9189): at android.os.Handler.handleCallback(Handler.java:587)
02-07 10:13:58.418: E/AndroidRuntime(9189): at android.os.Handler.dispatchMessage(Handler.java:92)
02-07 10:13:58.418: E/AndroidRuntime(9189): at android.os.Looper.loop(Looper.java:130)
02-07 10:13:58.418: E/AndroidRuntime(9189): at android.app.ActivityThread.main(ActivityThread.java:3703)
02-07 10:13:58.418: E/AndroidRuntime(9189): at java.lang.reflect.Method.invokeNative(Native Method)
02-07 10:13:58.418: E/AndroidRuntime(9189): at java.lang.reflect.Method.invoke(Method.java:507)
02-07 10:13:58.418: E/AndroidRuntime(9189): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:841)
02-07 10:13:58.418: E/AndroidRuntime(9189): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:599)
02-07 10:13:58.418: E/AndroidRuntime(9189): at dalvik.system.NativeStart.main(Native Method)
滑动条形图时的LogCat:78是gewicht.setText(progress +“%”);
02-07 10:18:50.312: I/Process(9235): Sending signal. PID: 9235 SIG: 9
02-07 10:18:53.625: D/dalvikvm(9247): GC_EXTERNAL_ALLOC freed 92K, 48% free 2918K/5511K, external 4223K/4230K, paused 26ms
02-07 10:18:54.668: D/AndroidRuntime(9247): Shutting down VM
02-07 10:18:54.668: W/dalvikvm(9247): threadid=1: thread exiting with uncaught exception (group=0x40235568)
02-07 10:18:54.730: E/AndroidRuntime(9247): FATAL EXCEPTION: main
02-07 10:18:54.730: E/AndroidRuntime(9247): java.lang.NullPointerException
02-07 10:18:54.730: E/AndroidRuntime(9247): at test.marco.notenha.mark$1.onProgressChanged(mark.java:78)
02-07 10:18:54.730: E/AndroidRuntime(9247): at android.widget.SeekBar.onProgressRefresh(SeekBar.java:90)
02-07 10:18:54.730: E/AndroidRuntime(9247): at android.widget.ProgressBar.doRefreshProgress(ProgressBar.java:544)
02-07 10:18:54.730: E/AndroidRuntime(9247): at android.widget.ProgressBar.doRefreshProgress(ProgressBar.java:523)
02-07 10:18:54.730: E/AndroidRuntime(9247): at android.widget.ProgressBar.refreshProgress(ProgressBar.java:558)
02-07 10:18:54.730: E/AndroidRuntime(9247): at android.widget.ProgressBar.setProgress(ProgressBar.java:607)
02-07 10:18:54.730: E/AndroidRuntime(9247): at android.widget.AbsSeekBar.trackTouchEvent(AbsSeekBar.java:368)
02-07 10:18:54.730: E/AndroidRuntime(9247): at android.widget.AbsSeekBar.onTouchEvent(AbsSeekBar.java:313)
02-07 10:18:54.730: E/AndroidRuntime(9247): at android.view.View.dispatchTouchEvent(View.java:3886)
02-07 10:18:54.730: E/AndroidRuntime(9247): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:875)
02-07 10:18:54.730: E/AndroidRuntime(9247): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:875)
02-07 10:18:54.730: E/AndroidRuntime(9247): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:875)
02-07 10:18:54.730: E/AndroidRuntime(9247): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:875)
02-07 10:18:54.730: E/AndroidRuntime(9247): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:875)
02-07 10:18:54.730: E/AndroidRuntime(9247): at com.android.internal.policy.impl.PhoneWindow$DecorView.superDispatchTouchEvent(PhoneWindow.java:1794)
02-07 10:18:54.730: E/AndroidRuntime(9247): at com.android.internal.policy.impl.PhoneWindow.superDispatchTouchEvent(PhoneWindow.java:1132)
02-07 10:18:54.730: E/AndroidRuntime(9247): at android.app.Dialog.dispatchTouchEvent(Dialog.java:642)
02-07 10:18:54.730: E/AndroidRuntime(9247): at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchTouchEvent(PhoneWindow.java:1778)
02-07 10:18:54.730: E/AndroidRuntime(9247): at android.view.ViewRoot.deliverPointerEvent(ViewRoot.java:2208)
02-07 10:18:54.730: E/AndroidRuntime(9247): at android.view.ViewRoot.handleMessage(ViewRoot.java:1892)
02-07 10:18:54.730: E/AndroidRuntime(9247): at android.os.Handler.dispatchMessage(Handler.java:99)
02-07 10:18:54.730: E/AndroidRuntime(9247): at android.os.Looper.loop(Looper.java:130)
02-07 10:18:54.730: E/AndroidRuntime(9247): at android.app.ActivityThread.main(ActivityThread.java:3703)
02-07 10:18:54.730: E/AndroidRuntime(9247): at java.lang.reflect.Method.invokeNative(Native Method)
02-07 10:18:54.730: E/AndroidRuntime(9247): at java.lang.reflect.Method.invoke(Method.java:507)
02-07 10:18:54.730: E/AndroidRuntime(9247): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:841)
02-07 10:18:54.730: E/AndroidRuntime(9247): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:599)
02-07 10:18:54.730: E/AndroidRuntime(9247): at dalvik.system.NativeStart.main(Native Method)
答案 0 :(得分:5)
试试这个
seek_gewicht = (SeekBar)findViewById(R.id.seekBar_wertung);
应该是
seek_gewicht = (SeekBar)dialog.findViewById(R.id.seekBar_wertung);
答案 1 :(得分:3)
确定。试试这个代码..
seek_gewicht = (SeekBar) dialog.findViewById(R.id.seekBar_wertung);
而不是
seek_gewicht = (SeekBar) findViewById(R.id.seekBar_wertung);
第二个例外。初始化您的TextView
。与SeekBar
相同。
gewicht = (TextView) dialog.findViewById(R.id.txt_gewicht);
您还必须初始化其他组件。