Android 为什么我的对话Seekbar会崩溃?空指针异常
我有一个seekbar在对话中,它正在崩溃。它说有一个Android 为什么我的对话Seekbar会崩溃?空指针异常,android,nullpointerexception,seekbar,Android,Nullpointerexception,Seekbar,我有一个seekbar在对话中,它正在崩溃。它说有一个NullPointerException。 但是我找不到它!我的错在哪里? 我对NullPointerException的位置进行了注释 对话框\u context\u mark.xml: <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/add_hw_dialog" android:layo
NullPointerException
。
但是我找不到它!我的错在哪里?
我对NullPointerException
的位置进行了注释
对话框\u context\u 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>
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.setonseekbarchaneglistener(您的seekbarlistener);)
滑动条时的LogCat:78为gewich.setText(进度+“%”)
试试这个
seek_gewicht = (SeekBar)findViewById(R.id.seekBar_wertung);
应该是
seek_gewicht = (SeekBar)dialog.findViewById(R.id.seekBar_wertung);
嗯。请尝试此代码。。
seek\u gewict=(SeekBar)dialog.findViewById(R.id.SeekBar\u wertung)代码>
而不是
seek\u gewicht=(SeekBar)findViewById(R.id.SeekBar\u wertung)代码>
用于第二个异常。初始化文本视图
。与SeekBar
相同
gewicht=(TextView)dialog.findviewbyd(R.id.txt\u gewicht)代码>
您还必须初始化其他组件。能否从LogCat添加异常?谢谢。现在我可以打开对话框了。但是当我试着滑动seekbar时,它被另一个空点变形撞坏了。我只是添加了logcat视图,这是我自己想出来的。我应该先看看然后再问。。。和seek_gewicht一样的错误。dialog.findViewByID而不是findviewbydthanks:)它现在可以工作了。但当我试着滑杆的时候它崩溃了。。。我添加了一个LogCat视图。。。
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)
seek_gewicht = (SeekBar)findViewById(R.id.seekBar_wertung);
seek_gewicht = (SeekBar)dialog.findViewById(R.id.seekBar_wertung);