没有触发按钮时,警报对话框不起作用

时间:2014-03-17 07:57:48

标签: java android

我已经写了一个小应用程序,如果它关闭,将切换3G数据和Tethering,如果它打开则相反关闭。一切正常,但没有主屏幕,应用程序只是启动,做它的事情并完成。我现在想在第一次运行时发出介绍性警报(从文件读取运行计数)。

我的问题是虽然执行了警告对话框但没有显示任何内容,所以很明显我错过了一些东西。

我到目前为止看到的所有示例都触发了一个按钮点击,而这个应用程序没有按钮或屏幕。任何帮助将不胜感激。

这是java代码

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>

这里是错误日志

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)

1 个答案:

答案 0 :(得分:0)

单击正数按钮时,您必须关闭对话框

dialog.dismiss();

并删除&#34;尝试{&#34;你放在代码的末尾。首先在对话框构建器工作时编码,然后对其他所有内容进它在我的项目中运作良好。

您可以使用此链接查询有关对话框的问题! http://developer.android.com/guide/topics/ui/dialogs.html

古德勒克。