菜单中的自定义AlertDialog?

时间:2011-11-16 14:13:48

标签: android android-menu android-alertdialog

当我按下将调用自定义AlertDialog:/

的菜单按钮时,

我的应用程序将显示FC

这是我的代码。有人可以帮帮我吗?

package com.winkler.plan;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;

import android.os.Bundle;
import android.webkit.WebView;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.ViewGroup;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

public class StundenplanActivity extends Activity {
    /** Called when the activity is first created. */

    WebView mWebview;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        //Webview
        mWebview = (WebView) findViewById(R.id.webView);
        mWebview.getSettings().setJavaScriptEnabled(true);
        mWebview.loadUrl("http://www.google.at");

    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu)           //Menu Inflater
    {                                                       
        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.menu, menu);
        return true;
    }




    @Override
    public boolean onOptionsItemSelected(MenuItem item) 
    {
        switch (item.getItemId()) 
        {
            case R.id.plan:     // Shortcut MenuButton Stundeplan
                {
                    mWebview.loadUrl("http://www.haufen.at/stundenplan/index.php");


                }
                return true;

            case R.id.about:    // Shortcut MenuButton About
                {   

                    AlertDialog.Builder builder;
                    AlertDialog alertDialog;

                    Context mContext = getApplicationContext();
                    LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(LAYOUT_INFLATER_SERVICE);
                    View layout = inflater.inflate(R.layout.custom_dialog,
                                                   (ViewGroup) findViewById(R.id.layout_root));

                    TextView text = (TextView) layout.findViewById(R.id.text);
                    text.setText("Hello, this is a custom dialog!");
                    ImageView image = (ImageView) layout.findViewById(R.id.image);
                    image.setImageResource(R.drawable.image);

                    builder = new AlertDialog.Builder(mContext);
                    builder.setView(layout);
                    alertDialog = builder.create();

                    alertDialog.show();
                }
                return true;

            case R.id.call: // Shortcut MenuButton externe
                {
                    final CharSequence[] items = {"Moodle", "Klassenserver", "2AHEL- Website"};

                    AlertDialog.Builder builder = new AlertDialog.Builder(this);
                    builder.setTitle("Pick a color");
                    builder.setItems(items, new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface dialog, int item) {
                            Toast.makeText(getApplicationContext(), items[item], Toast.LENGTH_SHORT).show();
                        }
                    });
                    AlertDialog alert = builder.create();
                    alert.show ();

                }
                return true;

            default:
                return super.onOptionsItemSelected(item);
        }

    }



    public void customToast ()
    {
        LayoutInflater inflater = getLayoutInflater();
        View layout = inflater.inflate(R.layout.layout_toast,
                                       (ViewGroup) findViewById(R.id.toast_layout_root));

        ImageView image = (ImageView) layout.findViewById(R.id.image);
        image.setImageResource(R.drawable.image);
        TextView text = (TextView) layout.findViewById(R.id.text);
        text.setText("Hallo! ich bin ein Custom Toast");


        Toast toast = new Toast(getApplicationContext());
        toast.setGravity(Gravity.CENTER_VERTICAL, 0, 0);
        toast.setDuration(Toast.LENGTH_LONG);
        toast.setView(layout);
        toast.show();
    }

}

以下是对话框的XML代码:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:id="@+id/layout_root"
              android:orientation="horizontal"
              android:layout_width="fill_parent"
              android:layout_height="fill_parent"
              android:padding="10dp"
              >
    <ImageView android:id="@+id/image"
               android:layout_width="wrap_content"
               android:layout_height="fill_parent"
               android:layout_marginRight="10dp"
               />
    <TextView android:id="@+id/text"
              android:layout_width="wrap_content"
              android:layout_height="fill_parent"
              android:textColor="#FFF"
              />
</LinearLayout>

谢谢!哦,列表对话框不会FC。

1 个答案:

答案 0 :(得分:1)

将第builder = new AlertDialog.Builder(mContext);行更改为builder = new AlertDialog.Builder(this);

类似的情况:Android: ProgressDialog.show() crashes with getApplicationContext
更多:Dialog throwing "Unable to add window — token null is not for an application” with getApplication() as context

PS。对于其他人:Context mContext = getApplicationContext();