gpt4 book ai didi

java - 从另一个类调用 Internet Detect 无效

转载 作者:行者123 更新时间:2023-12-02 06:25:48 25 4
gpt4 key购买 nike

我正在制作互联网检测器类,因此当互联网关闭时,它会显示一个带有两个选项的对话框,一个是退出应用程序,另一个是刷新正在检查互联网的空白。单击刷新时应用程序强制关闭。看一下代码。

fragment 类

public void check(){
DetectConnection dc = new DetectConnection(getActivity());
if(dc.isConnectedToInternet()==true) {
String url = "...";
new DownloadFilesTask().execute(url);
} else {
FragmentManager ft = getActivity().getFragmentManager();
MyAlertDialogWIndow alert = new MyAlertDialogWIndow();
alert.show(ft, "Alert");
}
}

MyAlertDialogWIndow 类

public class MyAlertDialogWIndow extends DialogFragment {

Fragment2 fragment2 = new Fragment2();
DetectConnection detectConnection;
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
OnClickListener positiveClick = new OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {

fragment2.check();
}
};
}

OnClickListener negativeClick = new OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {


}
};

AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
builder.setMessage("Device is not connected to the internet. Check your connection and click refresh");
builder.setNegativeButton("Exit", negativeClick);
builder.setPositiveButton("Refresh", positiveClick);
builder.setTitle("No Connection");
Dialog dialog = builder.create();
return dialog;
}

DetectConnection 类

public class DetectConnection {

private Context _context;

public DetectConnection(Context context){
this._context = context;
}

public boolean isConnectedToInternet(){
ConnectivityManager connectivity = (ConnectivityManager) _context.getSystemService(Context.CONNECTIVITY_SERVICE);
if (connectivity != null)
{
NetworkInfo[] info = connectivity.getAllNetworkInfo();
if (info != null)
for (int i = 0; i < info.length; i++)
if (info[i].getState() == NetworkInfo.State.CONNECTED)
{
return true;
}

}
return false;
}
}

错误

com.example.example.DetectConnection.isConnectedToInternet(DetectConnection.java:16)

这是公共(public)类MyAlertDialogWIndow扩展DialogFragment

.Fragment2.check(Fragment2.java:93),即if(dc.isConnectedToInternet()==true)

完整的日志

 12-12 14:48:31.445: W/dalvikvm(3211): threadid=1: thread exiting with uncaught    exception
12-12 14:48:31.450: E/AndroidRuntime(3211): FATAL EXCEPTION: main
12-12 14:48:31.450: E/AndroidRuntime(3211): java.lang.NullPointerException
12-12 14:48:31.450: E/AndroidRuntime(3211):at com.example.example.DetectConnection.isConnectedToInternet(DetectConnection.java:16)
12-12 14:48:31.450: E/AndroidRuntime(3211): at com.example.example.Fragment2.check(Fragment2.java:93)
12-12 14:48:31.450: E/AndroidRuntime(3211): at com.example.example.MyAlertDialogWIndow$1.onClick(MyAlertDialogWIndow.java:27)
12-12 14:48:31.450: E/AndroidRuntime(3211): at com.android.internal.app.AlertController$ButtonHandler.handleMessage(AlertController.java:167)
12-12 14:48:31.450: E/AndroidRuntime(3211): at android.os.Handler.dispatchMessage(Handler.java:99)
12-12 14:48:31.450: E/AndroidRuntime(3211): at android.os.Looper.loop(Looper.java:137)
12-12 14:48:31.450: E/AndroidRuntime(3211): at android.app.ActivityThread.main(ActivityThread.java:4898)
12-12 14:48:31.450: E/AndroidRuntime(3211): at java.lang.reflect.Method.invokeNative(Native Method)
12-12 14:48:31.450: E/AndroidRuntime(3211): at java.lang.reflect.Method.invoke(Method.java:511)
12-12 14:48:31.450: E/AndroidRuntime(3211): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1006)
12-12 14:48:31.450: E/AndroidRuntime(3211): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:773)
12-12 14:48:31.450: E/AndroidRuntime(3211): at dalvik.system.NativeStart.main(Native Method)

最佳答案

您的问题是您正在对尚未附加到 ActivityFragment 调用 getActivity()

当您实例化 Fragment 类时,如下所示

Fragment2 fragment2 = new Fragment2();

它将为getActivity()返回null。我建议不要在那里实例化您的 Fragment ,而是从您计划使用它的 Activity 调用 FragmentTransaction 并附加 首先分段

编辑

另一个可能更简单的选择是使互联网检测方法成为您的Activity而不是Fragment的一部分。这样你就可以执行以下操作:

OnClickListener positiveClick = new OnClickListener() {          @Override        public void onClick(DialogInterface dialog, int which) {           ((YourActivityName)getActivity()).check();    }};

关于java - 从另一个类调用 Internet Detect 无效,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20545284/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com