0

アラートボックス、ダイアログボックスですべてが機能していることがわかりましたが、独自のカスタムダイアログボックスで作成しようとすると問題が発生します。開発ガイドの指示に従いましたが、http://developer.android.com/intl/de/guide/topics/ui/dialogs.html結果に到達できませんでした。次のエラーメッセージ。

03-04 11:37:08.780: ERROR/AndroidRuntime(726): android.view.WindowManager$BadTokenException: Unable to add window -- token null is not for an application

カスタムダイアログボックスを何日も作成しようとしましたが、表示できませんでした。フォーラムで入手したソリューションを試してみましたが、それもうまくいかないようです。良いコードの一部または使用するための提案をください...これに関する提案はかなりあります。

4

2 に答える 2

0

Androidダイアログ-混乱している質問を見てください。あなたの質問に似ています。また、エラーが発生するコードを共有する必要があります。そうしないと、支援が困難になります。

于 2010-09-06T09:17:30.487 に答える
0

custom_dialogxmlを作成します

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:id="@+id/root"
    android:layout_height="match_parent">
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Custom Dialog"
        android:textColor="#000"
        android:textSize="25dp"/>
    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_alignParentRight="true"
        android:layout_height="wrap_content">
    <Button
        android:id="@+id/ok"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Ok"
        android:textColor="#000"
        android:textSize="19dp"/>
        <Button
            android:id="@+id/cancel"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Cancel"
            android:textColor="#000"
            android:textSize="19dp"/>
    </LinearLayout>
</RelativeLayout>

MainActivity.javaにカスタムダイアログビューを追加します

package techamongus.com.testapplication;

import android.app.Activity;
import android.app.Dialog;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;

public class MainActivity extends Activity {
Dialog customDialog;
    Button ok,cancel;
    Button showDialog;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        showDialog=(Button)findViewById(R.id.show);
        showDialog.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                customDialog.show();
            }
        });
        customDialog=new Dialog(this);
        LayoutInflater customInflater = (LayoutInflater)this.getSystemService(LAYOUT_INFLATER_SERVICE);
        View customLayout=customInflater.inflate(R.layout.custom_dialog, (ViewGroup) findViewById(R.id.root));
        customDialog.setContentView(customLayout);
        ViewGroup.LayoutParams layoutParams2= customLayout.getLayoutParams();
        layoutParams2.height=400;
        ok=(Button)customLayout.findViewById(R.id.ok);
        ok.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //done what do you want to do
                customDialog.dismiss();
            }
        });
        cancel=(Button)customLayout.findViewById(R.id.cancel);
        cancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //done what do you want to do
                customDialog.dismiss();
            }
        });
    }

}

これがmain_activity.xmlです

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/activity_main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gravity="center"
    tools:context="techamongus.com.testapplication.MainActivity">
    <Button
        android:id="@+id/show"
        android:layout_width="200dp"
        android:layout_height="50dp"
        android:text="Show dialog"
        android:layout_gravity="center"
        android:textColor="#000"/>
</LinearLayout>

http://www.techamongus.com/2017/03/android-create-custom-dialog-program.html

于 2017-03-18T03:23:28.707 に答える