在AlertDialog崩溃中启用WiFi

时间:2016-01-01 16:58:03

标签: java android

如果未启用wifi或移动数据,则应询问用户,如果他在AlertDialog中单击是,则会转到设置。

当点击更新按钮时,这是在创建时的主要内部:

from PIL import Image
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.image as mpimg


nir = mpimg.imread('bant4.tif')

A = np.array(nir)

for k in range(0,508):
    for j in range(0,508):
        ilk = A[k][j] + A[k][j+1] + A[k][j+2] + A[k][j+3] + A[k][j+4]
        ikinci = A[k+1][j] + A[k+1][j+1] + A[k+1][j+2] + A[k+1][j+3] + A[k+1][j+4]
        ucuncu = A[k+2][j] + A[k+2][j+1] + A[k+2][j+2] + A[k+2][j+3] + A[k+2][j+4]
        dorduncu = A[k+3][j] + A[k+3][j+1] + A[k+3][j+2] + A[k+3][j+3] + A[k+3][j+4]
        besinci = A[k+4][j] + A[k+4][j+1] + A[k+4][j+2] + A[k+4][j+3] + A[k+4][j+4]

        ort = (ilk + ikinci + ucuncu + dorduncu + besinci) / 5 
        ort.tofile('ort.txt')

这是必要的:

Warning (from warnings module):
File "C:\Users\Celik\Desktop\ödev5.py", line 19
ort = (ilk + ikinci + ucuncu + dorduncu + besinci) / 5
RuntimeWarning: overflow encountered in ubyte_scalars

Warning (from warnings module):
File "C:\Users\Celik\Desktop\ödev5.py", line 17
besinci = A[k+4][j] + A[k+4][j+1] + A[k+4][j+2] + A[k+4][j+3] + A[k+4][j+4]
RuntimeWarning: overflow encountered in ubyte_scalars

Warning (from warnings module):
File "C:\Users\Celik\Desktop\ödev5.py", line 14
ikinci = A[k+1][j] + A[k+1][j+1] + A[k+1][j+2] + A[k+1][j+3] + A[k+1][j+4]
RuntimeWarning: overflow encountered in ubyte_scalars

Warning (from warnings module):
File "C:\Users\Celik\Desktop\ödev5.py", line 15
ucuncu = A[k+2][j] + A[k+2][j+1] + A[k+2][j+2] + A[k+2][j+3] + A[k+2][j+4]
RuntimeWarning: overflow encountered in ubyte_scalars

Warning (from warnings module):
File "C:\Users\Celik\Desktop\ödev5.py", line 13
ilk = A[k][j] + A[k][j+1] + A[k][j+2] + A[k][j+3] + A[k][j+4]
RuntimeWarning: overflow encountered in ubyte_scalars

Warning (from warnings module):
File "C:\Users\Celik\Desktop\ödev5.py", line 16
dorduncu = A[k+3][j] + A[k+3][j+1] + A[k+3][j+2] + A[k+3][j+3] + A[k+3][j+4]
RuntimeWarning: overflow encountered in ubyte_scalars

这是isConnected:

btnUpdate.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Log.v(TAG,"ButtonUpdate clicked");
            connectIfNecessary();
            currentDate = Calendar.getInstance();
            formatter= new SimpleDateFormat("yyyy-MM-dd");

            new UpdateDB(MainActivity.this);
            txtLastUpdateShow.setText(formatter.format(currentDate.getTime()));
    });

在updatedb构造函数中,它将调用asynctask来连接网站。

当我运行它,然后单击更新按钮时,它崩溃了,因为它进入updatedb withpout在构建器中询问我。错误:

 public void connectIfNecessary(){
    Log.v(TAG, "main connectIfNecessary");
    if(!isConnected()){
        final Context ctx = this;
        AlertDialog.Builder builder = new AlertDialog.Builder(ctx);
        builder.setCancelable(true);
        builder.setMessage("need internet");
        builder.setTitle("do you want to enable it now?");
        builder.setPositiveButton("yes", new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int which) {
                ctx.startActivity(new Intent(Settings.ACTION_WIRELESS_SETTINGS));
            }
        });
        builder.setNegativeButton("no", new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int which) {
                return;
            }
        });
        builder.setOnCancelListener(new DialogInterface.OnCancelListener() {
            public void onCancel(DialogInterface dialog) {
                return;
            }
        });
        builder.show();
    }

}

MainActivity.java:106)是builder.show()
MainActivity.java:276)connectIfNecessary();

我几个小时都会收到错误,首先关于appcompat,然后显示然后布局。

我使用真正的平板设备。

1 个答案:

答案 0 :(得分:0)

当设备未连接时,以下是执行流程

  • 进入connectIfNecessary()
  • 创建提醒,要求用户启用互联网。请注意,此时只创建Alter对象,只有在主线程堆栈展开后才会在屏幕上显示。
  • 从connectIfNecessary()返回创建updateDB()的实例。在这个应用程序内部崩溃

很少有简单的更改可以修复您的代码。

    如果在创建updateDB()实例之前进行检查,则
  • 添加 isConnected()。这将涵盖CONNECTED案例
  • 如果设备未连接,用户将显示更改并进入设置屏幕。如果用户从设置启用WIFI并返回,则将调用您活动的onResume()。检查连接并在此处添加updateDB()。