gpt4 book ai didi

android - 为什么我的 Dialog Seekbar 崩溃了?空指针异常

转载 作者:太空宇宙 更新时间:2023-11-03 12:11:18 26 4
gpt4 key购买 nike

我在对话框上有一个搜索栏,它崩溃了。它说有一个 NullPointerException。但是我找不到它!我的错误在哪里?我评论了 NullPointerException 所在的位置。

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>

标记.java

在 Public Class mark 中我是这样定义的:

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)

最佳答案

试试这个

seek_gewicht = (SeekBar)findViewById(R.id.seekBar_wertung);

应该是

seek_gewicht = (SeekBar)dialog.findViewById(R.id.seekBar_wertung);

关于android - 为什么我的 Dialog Seekbar 崩溃了?空指针异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9173526/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com