2014-03-24 133 views
0

我想設計我的應用程序,當應用程序安裝在設備上時,它應該首先檢查應用程序是否連接到互聯網或不顯示佈局之前。所以在下面討論這個方法。安裝應用程序後檢查互聯網連接Android

@Override 
    protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    connectivity = new ConnectionDetector(getApplicationContext()); 
    alertDialog = new AlertDialogManager(); 
    //Checking for Internet connection 
    if(!connectivity.isConnectingToInternet()){ 
     alertDialog.showAlertDialog(getApplicationContext(), "Internet Connection Error", "Not connected to internet", false); 
     return ; 
    } 
    else{ 
     setContentView(R.layout.activity_register); 

     me = (EditText) findViewById(R.id.fname); 
     email = (EditText) findViewById(R.id.email); 
     age = (EditText) findViewById(R.id.age); 
     address = (EditText) findViewById(R.id.address); 
     state = (EditText) findViewById(R.id.state); 
     country = (EditText) findViewById(R.id.country); 
     done = (Button) findViewById(R.id.done); 
    } 
} 

我ConnectionDector類是如下:

public class ConnectionDetector { 

private Context context; 

public ConnectionDetector(Context context){ 
    this.context = context; 
} 

public boolean isConnectingToInternet(){ 
    ConnectivityManager connectivity = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE); 
    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; 
} 
} 

下面是我的AlertDialogManager類:

public class AlertDialogManager { 

@SuppressWarnings("deprecation") 
public void showAlertDialog(Context context, String title, String message, Boolean status){ 
    AlertDialog alertDialog = new AlertDialog.Builder(context).create(); 
    alertDialog.setTitle(title); 
    alertDialog.setMessage(message); 
    if(status != null){ 
     alertDialog.setIcon((status)?R.drawable.success : R.drawable.fail); 
    } 
    alertDialog.setButton("Okay", new DialogInterface.OnClickListener() { 
     @Override 
     public void onClick(DialogInterface dialog, int which) { 

     } 
    }); 
    alertDialog.show(); 
} 

我的應用程序被安裝正確,如果我連接到無線網絡。如果我沒有其get崩潰當我嘗試安裝它,並提供了以下錯誤:

03-24 17:01:11.169: E/AndroidRuntime(6101): FATAL EXCEPTION: main 
03-24 17:01:11.169: E/AndroidRuntime(6101): Process: com.iriemo.safetyapp, PID: 6101 
03-24 17:01:11.169: E/AndroidRuntime(6101): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.demoApp/com.example.demoApp.RegisterActivity}: java.lang.NullPointerException 
03-24 17:01:11.169: E/AndroidRuntime(6101):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2195) 
03-24 17:01:11.169: E/AndroidRuntime(6101):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245) 
03-24 17:01:11.169: E/AndroidRuntime(6101):  at android.app.ActivityThread.access$800(ActivityThread.java:135) 
03-24 17:01:11.169: E/AndroidRuntime(6101):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196) 
03-24 17:01:11.169: E/AndroidRuntime(6101):  at android.os.Handler.dispatchMessage(Handler.java:102) 
03-24 17:01:11.169: E/AndroidRuntime(6101):  at android.os.Looper.loop(Looper.java:136) 
03-24 17:01:11.169: E/AndroidRuntime(6101):  at android.app.ActivityThread.main(ActivityThread.java:5017) 
03-24 17:01:11.169: E/AndroidRuntime(6101):  at java.lang.reflect.Method.invokeNative(Native Method) 
03-24 17:01:11.169: E/AndroidRuntime(6101):  at java.lang.reflect.Method.invoke(Method.java:515) 
03-24 17:01:11.169: E/AndroidRuntime(6101):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779) 
03-24 17:01:11.169: E/AndroidRuntime(6101):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595) 
03-24 17:01:11.169: E/AndroidRuntime(6101):  at dalvik.system.NativeStart.main(Native Method) 
03-24 17:01:11.169: E/AndroidRuntime(6101): Caused by: java.lang.NullPointerException 
03-24 17:01:11.169: E/AndroidRuntime(6101):  at RegisterActivity.onCreate(RegisterActivity.java:26) 
03-24 17:01:11.169: E/AndroidRuntime(6101):  at  android.app.Activity.performCreate(Activity.java:5231) 
03-24 17:01:11.169: E/AndroidRuntime(6101):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087) 
03-24 17:01:11.169: E/AndroidRuntime(6101):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2159) 
03-24 17:01:11.169: E/AndroidRuntime(6101):  ... 11 more 
+0

alertDialog爲空。 –

+0

你在RegisterActivity的第26行中得到空指針異常。那裏有什麼? – gilonm

+0

我實際上已經創建了自己的AlertDialog類,名爲AlertDialogManager,我將在我的問題中添加它 – ik024

回答

1

添加alertDialog = new AlertDialog(getApplicationContext());這將解決null pointer exception

+0

我沒有使用默認警報對話框我在我的問題中添加了我的AlertDialogManager類 – ik024

0

改變這一點。

@Override 
protected void onCreate(Bundle savedInstanceState) { 
super.onCreate(savedInstanceState); 
setContentView(R.layout.activity_register); 
connectivity = new ConnectionDetector(this); 

//Checking for Internet connection 
if(!connectivity.isConnectingToInternet()){ 
    alertDialog.showAlertDialog(getApplicationContext(), "Internet Connection Error", "Not connected to internet", false); 
    return ; 
} 
else{ 
    // Something else. 
} 

me = (EditText) findViewById(R.id.fname); 
email = (EditText) findViewById(R.id.email); 
age = (EditText) findViewById(R.id.age); 
address = (EditText) findViewById(R.id.address); 
state = (EditText) findViewById(R.id.state); 
country = (EditText) findViewById(R.id.country); 
done = (Button) findViewById(R.id.done);