Question

My App will FC when I press the menu button that will call the Custom AlertDialog :/

Here is my code. Could somebody could help me out?

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();
    }

}

Here is my XML Code for the dialog:

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

Thanks! Oh and the list-dialog will not FC.

Was it helpful?

Solution

Change line builder = new AlertDialog.Builder(mContext); to builder = new AlertDialog.Builder(this);

A similar situation: Android: ProgressDialog.show() crashes with getApplicationContext
More: Dialog throwing "Unable to add window — token null is not for an application” with getApplication() as context

PS. for others: Context mContext = getApplicationContext();

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top