0
請看下面的代碼無法將視圖設置爲對話框
用於創建自定義對話框的代碼。這個班級是AlertDialog
的動作班。當用戶點擊那個AlertDialog
上的「是」按鈕時,該類被激發。 私有類PositiveDialogBtnAction實現DialogInterface.OnClickListener {
@Override
public void onClick(DialogInterface arg0, int arg1) {
// TODO Auto-generated method stub
//Toast.makeText(getApplicationContext(), databaseConnector.getStreetAddress(selectedBranch), Toast.LENGTH_LONG).show();
Dialog dialog = new Dialog(getApplicationContext());
dialog.setContentView(R.layout.activity_call_dialog);
dialog.setTitle("Select a Phone Number");
dialog.show();
}
}
activity_call_dialog.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".CallDialog" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:text="@string/hello_world" />
</RelativeLayout>
CallDialog.java
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
public class CallDialog extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_call_dialog);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.activity_call_dialog, menu);
return true;
}
}
當我試圖運行自定義迪阿羅g,出現以下錯誤
02-15 11:04:02.170: E/AndroidRuntime(585): FATAL EXCEPTION: main
02-15 11:04:02.170: E/AndroidRuntime(585): android.view.WindowManager$BadTokenException: Unable to add window -- token null is not for an application
02-15 11:04:02.170: E/AndroidRuntime(585): at android.view.ViewRoot.setView(ViewRoot.java:531)
02-15 11:04:02.170: E/AndroidRuntime(585): at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:177)
02-15 11:04:02.170: E/AndroidRuntime(585): at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:91)
02-15 11:04:02.170: E/AndroidRuntime(585): at android.app.Dialog.show(Dialog.java:241)
02-15 11:04:02.170: E/AndroidRuntime(585): at com.example.esoftcallmanager.Form$PositiveDialogBtnAction.onClick(Form.java:155)
02-15 11:04:02.170: E/AndroidRuntime(585): at com.android.internal.app.AlertController$ButtonHandler.handleMessage(AlertController.java:159)
02-15 11:04:02.170: E/AndroidRuntime(585): at android.os.Handler.dispatchMessage(Handler.java:99)
02-15 11:04:02.170: E/AndroidRuntime(585): at android.os.Looper.loop(Looper.java:123)
02-15 11:04:02.170: E/AndroidRuntime(585): at android.app.ActivityThread.main(ActivityThread.java:3683)
02-15 11:04:02.170: E/AndroidRuntime(585): at java.lang.reflect.Method.invokeNative(Native Method)
02-15 11:04:02.170: E/AndroidRuntime(585): at java.lang.reflect.Method.invoke(Method.java:507)
02-15 11:04:02.170: E/AndroidRuntime(585): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
02-15 11:04:02.170: E/AndroidRuntime(585): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
02-15 11:04:02.170: E/AndroidRuntime(585): at dalvik.system.NativeStart.main(Native Method)
02-15 11:04:05.520: I/Process(585): Sending signal. PID: 585 SIG: 9
這是爲什麼?請幫忙!
您好,我做到了,但現在它說「沒有封閉的類型CallDialog可以在範圍內訪問「!! – 2013-02-15 05:51:50
@Yohan:如果你是在單獨的類中創建對話框,那麼你將需要使用類構造函數來傳遞活動上下文 – 2013-02-15 05:57:02
我不明白 – 2013-02-15 06:07:48