DialogFragment中的SeekBar

时间:2013-06-20 13:55:15

标签: android

我正在尝试从Android中的SeekBar中读取DialogFragment中的内容。它一直在崩溃。单独使用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的操作时,它都会一直崩溃。当我评论整个部分时,它很好。问题是以某种方式覆盖这些功能,即使它们是空的。当整个部分被注释时,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();
}
}

1 个答案:

答案 0 :(得分:0)

根据您的评论

  

'错误是NullPointerException,它出现在第51行,即   我给你的片段中的第二行   (“numColumnsControl.setOnSeekBarChangeListener(新   OnSeekBarChangeListener()“)'

检查findViewById(R.id.Seekbar1)是否不返回null。如果是这样,请确保在请求布局中的任何视图元素之前,对话框布局已膨胀。 R引用也可能出现问题(尝试项目 - &gt;在eclipse中清理)。

编辑我不确定它是否有效,但尝试覆盖onViewCreated()并在此处放置findViewById和setOnSeekBarChangeListener代码。

以某种方式

编辑 onViewCreated()不会被调用(Android DialogFragment onViewCreated not called)。这使解决方案变得更加困难。您需要先膨胀视图并在膨胀的视图上执行findViewById。

View menuView = inflater.inflate(R.layout.settings_menu);
numColumnsControl = (SeekBar) menuView.findViewById(R.id.seekBar1, null);
numColumnsControl.setOnSeekBarChangeListener ...
builder.setView(menuView);