我正在嘗試從Android中的SeekBar中讀取東西,它位於DialogFragment中。它不斷崩潰。 SeekBar單獨出現沒有問題,但每當我嘗試重寫函數時,我都可以使用SeekBar來查找不斷崩潰的東西。我試圖複製基本上我見過其他人作爲工作代碼發佈的內容(儘管不在DialogFragment中),但它不起作用。有人能告訴我這裏有什麼問題嗎?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時,它都會崩潰。當我評論整個部分時,它很好。這個問題在某種程度上覆蓋了這些功能,即使它們是空的。當整個部分被評論時,View會出現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();
}
}
你可以發佈錯誤的痕跡嗎? –
錯誤是一個NullPointerException,它發生在第51行,這是我給你的片段中的第二行(「numColumnsControl.setOnSeekBarChangeListener(new OnSeekBarChangeListener()」) – user2471612
因此錯誤與numColumnsControl有關。該片段? –