私はアンドロイドに新しいです。私はカスタムダイアログを学ぼうとしました。カスタムダイアログgetviewがnullを返す
私はドキュメントから理解何から:
私はDialogFragment
を拡張iMusicDialog
クラスを作成し、これは私のコードは次のようになります。
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();
}
}
iMusicDialog.java
Myactivity.java
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");
}
Logcat:
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
クラスからのビューを取得していません。
どうすれば入手できますか?私はそれを正しくやっていますか?
更新:
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;
アップデート2:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="100dp"
android:id="@+id/dialogParent1">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/dialogParent2"
android:orientation="horizontal">
<ImageView
android:layout_weight="1"
android:contentDescription="@string/contains_song_thumbnail"
android:layout_width="0dp"
android:layout_height="50dp"
android:id="@+id/dialogThumnbnail"
/>
<TextView
android:layout_weight="6"
android:layout_width="0dp"
android:layout_height="match_parent"
android:gravity="center"
android:id="@+id/dialogTextview"
android:scrollHorizontally="true"
android:freezesText="true"
android:ellipsize="marquee"
android:marqueeRepeatLimit="marquee_forever"
android:focusableInTouchMode="true"
/>
<ToggleButton
android:layout_weight="1"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:text=""
android:textOff=""
android:layout_margin="5dp"
android:id="@+id/dialogControl"
android:background="@drawable/ic_play_pause_small"
android:textOn="" />
</LinearLayout>
<android.support.v4.widget.Space
android:layout_width="match_parent"
android:layout_height="20dp" />
<SeekBar
android:id="@+id/dialogSeekbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
/>
</LinearLayout>
レイアウトファイルだけで包み、NullPointerExceptionが
Attempt to invoke virtual method 'void android.widget.SeekBar.setMax(int)' on a null object
これは、あなたが呼ばれることを意味していること
ようなダイアログのビューを取得してください、あなたはシークバー変数が定義されている場所あなたがそれを作成したところ、あなたは示すことができる表示されないのですか? – Aaron
@Aaronどうぞご覧ください –