自定义对话框getview返回null

时间:2017-12-23 20:22:01

标签: android

我是Android新手,我尝试学习自定义对话框。

从我对文档的理解:

我创建了一个扩展iMusicDialog的{​​{1}}类,这就是我的代码。

DialogFragment

iMusicDialog.java

现在我有一个活动,我想展示它。这就是我所做的:

public class iMusicDialog extends DialogFragment { @SuppressLint("InflateParams") @Override public Dialog onCreateDialog(Bundle savedInstanceState) { AlertDialog.Builder builder=new AlertDialog.Builder(getActivity()); LayoutInflater layoutInflater=getActivity().getLayoutInflater(); View v=layoutInflater.inflate(R.layout.imusicplaydialog,null); builder.setView(v); return builder.create(); } }

Myactivity.java

logcat的:

  protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mMusicDialog=new iMusicDialog();
        View v=mMusicDialog.getView();
        Log.e("TAG_POSN","outside");


        if(v!=null) {
            playpause=v.findViewById(R.id.dialogControl);
            playpause.setOnClickListener(this);
            seekBar=v.findViewById(R.id.dialogSeekbar);
            seekBar.setOnSeekBarChangeListener(this);
            Log.e("TAG_POSN","inside");
            mSongDisplay=v.findViewById(R.id.dialogTextview);
            mImage=v.findViewById(R.id.dialogThumnbnail);
        }
        seekBar.setMax(100);
        intent=getIntent();
        if (intent == null) {
            Toast.makeText(this,"Audio playback Error",Toast.LENGTH_LONG).show();
            finish();
        }
        mMusicDialog.show(getFragmentManager(),"some text");
    }

日志显示我没有从12-24 01:49:57.288 30063-30063/? E/TAG_POSN: outside 12-24 01:49:57.336 30063-30063/? E/ExtMediaPlayer-JNI: env->IsInstanceOf fails 12-24 01:49:57.336 30063-30063/? E/MediaPlayer-JNI: JNIMediaPlayerFactory: bIsQCMediaPlayerPresent 0 12-24 01:49:57.336 30063-30063/? E/ExtMediaPlayer-JNI: env->IsInstanceOf fails 12-24 01:49:57.336 30063-30063/? E/MediaPlayer-JNI: JNIMediaPlayerFactory: bIsQCMediaPlayerPresent 0 12-24 01:49:57.580 30063-30063/? E/AndroidRuntime: FATAL EXCEPTION: main Process: com.example.tilak.imusicplay, PID: 30063 java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.SeekBar.setMax(int)' on a null object 类获得视图。

我如何得到它?我做得对吗?

更新

iMusicDialog

更新2:

public class iMusicDialogActivity extends FragmentActivity implements  View.OnClickListener,SeekBar.OnSeekBarChangeListener ,MediaPlayer.OnPreparedListener,MediaPlayer.OnErrorListener,MediaPlayer.OnCompletionListener,OnAudioFocusChangeListener{
    private static final String TAG_LOG = "Debug";
    private Intent intent=null;
    private SeekBar seekBar;
    private Song song;
    private ToggleButton playpause;
    private MediaPlayer player;
    private boolean paused=false;
    private static final int OSVERSION = Build.VERSION.SDK_INT;
    private TextView mSongDisplay;
    private ImageView mImage;
    private iMusicDialog mMusicDialog;

布局文件只是

3 个答案:

答案 0 :(得分:0)

NullPointerException表示

Attempt to invoke virtual method 'void android.widget.SeekBar.setMax(int)' on a null object

这意味着您在null SeekBar上调用了setMax(int)。

答案 1 :(得分:0)

尝试更换:

seekbar.setMax(100)

使用:

seekBar.setMax(100)

您忘了在seekBar中将B大写。

答案 2 :(得分:0)

尝试获取此类对话框视图

seekBar = (SeekBar) mMusicDialog.getDialog().findViewById(R.id.dialogSeekbar);