在Android中创建自定义对话框

发布于 2024-09-18 00:38:26 字数 565 浏览 5 评论 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

我已经尝试制作自定义对话框很多天了,但我无法调出它。我什至尝试了在论坛上获得的解决方案,但这似乎也不起作用。 给我一些好的代码或一些可以使用的建议...对此的任何建议都是值得赞赏的。

I found all things working with alert box,dialog box but when i try creating things with my own custom dialog box it gives me problems. Though i followed the instructions as per the dev guide: http://developer.android.com/intl/de/guide/topics/ui/dialogs.html i could'nt reach with my results just it displays a force close with the following error message.

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

I have been trying to make my custom dialog box for many days but i couldnt bring it up. I even tried with the solutions that i got on forums but that too doesnt seems of working.
Give me some piece of good code or some suggestion to work with... Any suggestions on this is appreciable.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

烂柯人 2024-09-25 00:38:26

Android 对话框 - 困惑看看这个问题,看起来与你的类似。此外,您还必须共享导致错误的代码,否则很难提供帮助。

Android Dialog - confused take a look on the question, looks similar to yours. Also you have to share a code which results in the error otherwise it is hard to help.

ゞ记忆︶ㄣ 2024-09-25 00:38:26

创建 custom_dialog xml

<?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

create custom_dialog xml

<?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>

Add custom dialog View in 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();
            }
        });
    }

}

here is your 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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文