這是我的MainActivity類別中創建一個AlertDialog我的靜態內部類:可點擊超鏈接在DialogFragment
public static class AboutDialogFragment extends DialogFragment {
public static AboutDialogFragment newInstance() {
AboutDialogFragment frag = new AboutDialogFragment();
return frag;
}
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
return new AlertDialog.Builder(getActivity())
.setIcon(R.drawable.ic_dialog_about)
.setTitle(R.string.about)
.setMessage(R.string.about_message)
..........
.create();
}
}
而且我展示它,當你按下它裏面MainActivity菜單項:
case R.id.about:
DialogFragment aboutFragment = AboutDialogFragment.newInstance();
aboutFragment.show(getSupportFragmentManager(), "about_dialog");
// Make links clickable
((TextView) aboutFragment.getDialog().findViewById(android.R.id.message)).setMovementMethod(LinkMovementMethod.getInstance());
return true;
我正在嘗試使用註釋行使消息文本中的鏈接可點擊。
我發現這種方法here它使用普通的對話框(沒有片段)時工作。
但是,這是我第一次嘗試在DialogFragment上使用它,並且在嘗試查找視圖時總是遇到NullPointerException。
我也試過aboutFragment.getView().findViewById(android.R.id.message)
但是也返回null。
也許我太早/在錯誤的地方調用代碼?
任何想法都會很棒!
編輯:只是嘗試((TextView) v.getRootView().findViewById(android.R.id.message)).setMovementMethod(LinkMovementMethod.getInstance());
和 ((TextView) v.findViewById(android.R.id.message)).setMovementMethod(LinkMovementMethod.getInstance());
在onCreateView(),並沒有成功也試過在onCreateDialog()。
仍然得到空指針異常......
請參閱我的編輯。在調用View v = super.onCreateView(...)之後,我在onCreateDialog()和onCreateView()方法中都嘗試過了,但它不起作用! – Kavi 2012-02-04 19:32:18