2014-03-24 3 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 클래스는 다음과 같다 :의 I가 무선 랜에 연결하고있는 경우

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(); 
} 

내 응용 프로그램이 제대로 설치됩니다 :

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 클래스입니다. 내가 아니라고하면 그 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가 null입니다. –

+0

RegisterActivity에서 26 번째 줄 포인터 예외가 발생합니다. 거기에있는 항목은 무엇입니까? – gilonm

+0

실제로 AlertDialogManager라는 자체 AlertDialog 클래스를 만들었습니다. 내 질문에 추가 할 것입니다. – ik024

답변

1

이이 null pointer exception

+0

기본 경고 대화 상자를 사용하고 있지 않습니다. 위의 질문에 AlertDialogManager 클래스를 추가했습니다. – ik024

0

변경이 문제를 해결합니다 alertDialog = new AlertDialog(getApplicationContext()); 추가합니다.

@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);