我正在尝试从 DialogFragment 中的 Android 中的 SeekBar 中读取内容。它一直在崩溃。单独的 SeekBar 没有问题,但是每当我尝试覆盖函数时,我可以将 SeekBar 用于它不断崩溃的东西。我基本上尝试复制我看到其他人发布的工作代码(尽管不在 DialogFragment 中),但它不起作用。有人可以告诉我这里有什么问题吗?
numColumnsControl = (SeekBar) getActivity().findViewById(R.id.seekBar1);
numColumnsControl.setOnSeekBarChangeListener(new OnSeekBarChangeListener()
{
//@Override
public void onProgressChanged(SeekBar arg0, int progress, boolean fromTouch)
{
((TextView) (getActivity().findViewById(R.id.textView2))).setText(String.valueOf(progress));
}
//@Override
public void onStartTrackingTouch(SeekBar arg0)
{
// TODO Auto-generated method stub
}
//@Override
public void onStopTrackingTouch(SeekBar arg0)
{
// TODO Auto-generated method stub
}
});`
每当我执行打开 DialogFragment 的操作时,它都会崩溃。当我注释掉整个部分时,这很好。问题在于以某种方式覆盖了这些函数,即使它们是空的。当这整个部分被评论时,视图会出现 SeekBars 和 TextViews。
我不认为这很重要,但这里是 DialogFragment 的 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" >
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="fill_parent" >
<TextView
android:id="@+id/titleText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:text="Settings"
android:textAppearance="?android:attr/textAppearanceLarge" />
<TextView
android:id="@+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/textView2"
android:layout_below="@+id/seekBar1"
android:layout_marginTop="20dp"
android:text="NumShifts"
android:textAppearance="?android:attr/textAppearanceSmall" />
<SeekBar
android:id="@+id/seekBar1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="@+id/textView2"
android:gravity="center"
android:layout_gravity="center"
android:max="15"
android:progress="6"
android:secondaryProgress="0"/>
<SeekBar
android:id="@+id/SeekBar01"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="@+id/textView3"
android:gravity="center"
android:layout_gravity="center"
android:max="9"
android:progress="3"
android:secondaryProgress="0"/>
<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignRight="@+id/titleText"
android:layout_below="@+id/titleText"
android:layout_marginTop="18dp"
android:text="NumColumns"
android:textAppearance="?android:attr/textAppearanceSmall" />
</RelativeLayout>
</LinearLayout>
这是该课程的全部代码:
package com.example.[I have to hide this name];
import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.app.DialogFragment;
import android.content.Context;
import android.content.DialogInterface;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.widget.LinearLayout;
import android.widget.SeekBar;
import android.widget.SeekBar.OnSeekBarChangeListener;
import android.widget.TextView;
public class SettingsDialogueFragment extends DialogFragment
{
private int numColums;
private int numShifts;
private Context ctx;
private SeekBar numColumnsControl = null;
@Override
public Dialog onCreateDialog(Bundle savedInstanceState)
{
ctx = getActivity();
// Use the Builder class for convenient dialog construction
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
LayoutInflater inflater= getActivity().getLayoutInflater();
builder.setView(inflater.inflate(R.layout.settings_menu, null))
.setPositiveButton("DONE", new DialogInterface.OnClickListener()
{
public void onClick(DialogInterface dialog, int id)
{
//done
}
})
.setNegativeButton("CANCEL", new DialogInterface.OnClickListener()
{
public void onClick(DialogInterface dialog, int id)
{
// User cancelled the dialog
}
});
// Create the AlertDialog object and return it
LinearLayout settings = new LinearLayout(ctx);
numColumnsControl = (SeekBar) getActivity().findViewById(R.id.seekBar1);
Activity myActivity = getActivity();
if(myActivity == null)
{
Log.e("bill", "bill");
}
if(numColumnsControl == null)
{
Log.e("smith", "smith");
}
numColumnsControl.setOnSeekBarChangeListener(new OnSeekBarChangeListener()
{
//@Override
public void onProgressChanged(SeekBar arg0, int progress, boolean fromTouch)
{
((TextView) (getActivity().findViewById(R.id.textView2))).setText(String.valueOf(progress));
Log.e("asd", "asd");
}
//@Override
public void onStartTrackingTouch(SeekBar arg0)
{
Log.e("asdf", "asdf");
// TODO Auto-generated method stub
}
//@Override
public void onStopTrackingTouch(SeekBar arg0)
{
Log.e("asdfg", "asdfg");
// TODO Auto-generated method stub
}
});
return builder.create();
}
}