简体   繁体   中英

Alert dialog doesn't work when no button to trigger from

I've written a small app that will switch 3G Data and Tethering on if its off and conversely off if it's on. That all works fine but there is no home screen, the app just launches, does its thing and finishes. I now want to put an introductory alert the very first time it runs (run count read from a file).

My problem is that the alert dialog although is executed does not display anything, so clearly I'm missing something.

All of the examples I've seen so far trigger off a button click whereas this app doesn't have a button or a screen for that matter. Any help would be appreciated.

Here's the java code

package com.example.hotspot;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.net.wifi.WifiManager;
import android.os.Bundle;
import android.widget.Toast;

public class MainActivity extends Activity {
    //public final static String EXTRA_MESSAGE = "com.example.hotspot.MESSAGE";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState); 

    Integer idata = 0;

    Context context = getApplicationContext();
    idata = readFile("hotspot.dat");

  //if (idata == 1) { 
        AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
        builder.setTitle("Title");
        builder.setPositiveButton("Continue", new DialogInterface.OnClickListener() {

            @Override
            public void onClick(DialogInterface dialog, int which) {
                // TODO Auto-generated method stub
                dialog.dismiss();
            }
        });
        builder.setMessage("Do you want to continue");
        AlertDialog dialog = builder.create();
        dialog.show();
        //} 

    try {  the stuff that works ok ……….

activity_main.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:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal">
</LinearLayout>

AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.hotspot"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="10"
        android:targetSdkVersion="18" />
    <uses-permission android:name="android.permission.CHANGE_WIFI_STATE"/>
    <uses-permission android:name="android.permission.ACCESS_WIFI_STATE"/>
    <uses-permission android:name="android.permission.INTERNET"/>
    <uses-permission android:name="android.permission.CHANGE_NETWORK_STATE"/>
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
   }


    <application
        android:allowBackup="true"
        android:icon="@drawable/myhotspot"
        android:label="@string/app_name"
        android:theme="@android:style/Theme.Dialog">

        <activity
            android:name="com.example.hotspot.MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>          
        </activity>   
     </application>
</manifest>

here's the error log

03-17 21:16:34.530: E/WindowManager(735): android.view.WindowLeaked: Activity com.example.hotspot.MainActivity has leaked window com.android.internal.policy.impl.PhoneWindow$DecorView{b3db15e0 V.E..... R.....ID 0,0-480,291} that was originally added here
03-17 21:16:34.530: E/WindowManager(735):   at android.view.ViewRootImpl.<init>(ViewRootImpl.java:346)
03-17 21:16:34.530: E/WindowManager(735):   at android.view.WindowManagerGlobal.addView(WindowManagerGlobal.java:248)
03-17 21:16:34.530: E/WindowManager(735):   at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:69)
03-17 21:16:34.530: E/WindowManager(735):   at android.app.Dialog.show(Dialog.java:286)
03-17 21:16:34.530: E/WindowManager(735):   at com.example.hotspot.MainActivity.onCreate(MainActivity.java:44)
03-17 21:16:34.530: E/WindowManager(735):   at android.app.Activity.performCreate(Activity.java:5243)
03-17 21:16:34.530: E/WindowManager(735):   at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
03-17 21:16:34.530: E/WindowManager(735):   at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2140)
03-17 21:16:34.530: E/WindowManager(735):   at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2226)
03-17 21:16:34.530: E/WindowManager(735):   at android.app.ActivityThread.access$700(ActivityThread.java:135)
03-17 21:16:34.530: E/WindowManager(735):   at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1397)
03-17 21:16:34.530: E/WindowManager(735):   at android.os.Handler.dispatchMessage(Handler.java:102)
03-17 21:16:34.530: E/WindowManager(735):   at android.os.Looper.loop(Looper.java:137)
03-17 21:16:34.530: E/WindowManager(735):   at android.app.ActivityThread.main(ActivityThread.java:4998)
03-17 21:16:34.530: E/WindowManager(735):   at java.lang.reflect.Method.invokeNative(Native Method)
03-17 21:16:34.530: E/WindowManager(735):   at java.lang.reflect.Method.invoke(Method.java:515)
03-17 21:16:34.530: E/WindowManager(735):   at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:777)
03-17 21:16:34.530: E/WindowManager(735):   at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:593)
03-17 21:16:34.530: E/WindowManager(735):   at dalvik.system.NativeStart.main(Native Method)

You have to dismiss the dialog when the positive button is clicked

dialog.dismiss();

and remove the "try{ " you put at the end of the code. Code the dialog builder first when it works then code everything else. It worked pretty well in my project.

You can use this link for questions about dialogs! http://developer.android.com/guide/topics/ui/dialogs.html

Goodluck.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM