2012-02-19 50 views
1

在我的应用程序中,我通过使用PHP脚本查询数据库。目前,只要应用出现在屏幕上,它就会获取信息。但是,如果您没有互联网连接,它将返回一个NullPointerException,因为它没有获取任何数据。之后我用ConnectivityManager和NetworkInfo更新了我的代码,如果用户没有连接到他们的网络或WiFi,就会显示敬酒。问题是它跳过这个,只是像正常一样执行其余的代码。使用ConnectivityManager和NetworkInfo避免NullPointerException

public class Shc_BalloonSat_Activity extends Activity 
{ 
int historyCountFromUser; 
httpAPI api; 
mapAPI map; 
DecimalFormat df = new DecimalFormat("##.#####"); 
DecimalFormat decimalf = new DecimalFormat("##.######"); 
SharedPreferences pref; 
Editor prefEditor; 
String lastpacketsPHP; 

// User to determine how many packet the user would like to see. 
int userDefinedCount = 5; 

/* 
* Called when the activity is first created. 
*/ 

@Override 
public void onCreate(Bundle savedInstanceState) 
{ 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.main); 
    String returned = ""; 
    lastpacketsPHP = ""; 
    pref = getSharedPreferences("shared_prefs", 0); 
    prefEditor = pref.edit(); 
    prefEditor.putString(lastpacketsPHP, "/* PHP file url goes here */"); 
    prefEditor.commit();  

    if(!isNetworkConnected(this)) 
    { 
     Toast.makeText(this, "Please connect to the internet to access the full functionality of this app.", Toast.LENGTH_LONG).show(); 
    } 

    else 
    { 
     api = new httpAPI(this); 
     map = new mapAPI(this); 

     try 
     { 
      returned = api.getData(); 
     } 

     catch (Exception e) 
     { 
      e.printStackTrace(); 
     } 

     TextView infoTV = (TextView)this.findViewById(R.id.info); 
     infoTV.setText(returned); 
     assignInfoToInfoTextView(); 
     assignInfoToHistoryTextView(); 
    } 

} 

public boolean isNetworkConnected(Context context) 
{ 
    ConnectivityManager connectionManager = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE); 
    if (connectionManager.getActiveNetworkInfo() != null && connectionManager.getActiveNetworkInfo().isAvailable() && connectionManager.getActiveNetworkInfo().isConnected()) 
    { 
     return true; 
    } 

    else 
    { 
     return false; 
    } 
} 

任何想法我做错了什么?

更新(logcat的错误消息):

02-19 13:55:19.450: E/log_tag(443): Error in HTTP connection: java.net.UnknownHostException: Host is unresolved: space.uah.edu:80 
02-19 13:55:19.450: E/log_tag(443): Error converting result:  java.lang.NullPointerException 
02-19 13:55:19.450: E/log_tag(443): Error in HTTP connection: java.net.UnknownHostException: Host is unresolved: space.uah.edu:80 
02-19 13:55:19.450: E/log_tag(443): Error converting result: java.lang.NullPointerException 
02-19 13:55:19.450: E/AndroidRuntime(443): Uncaught handler: thread main exiting due to uncaught exception 
02-19 13:55:19.473: E/AndroidRuntime(443): java.lang.RuntimeException: Unable to start activity ComponentInfo{shc_BalloonSat.namespace/shc_BalloonSat.namespace.Shc_BalloonSat_Activity}: java.lang.NullPointerException: println needs a message 
02-19 13:55:19.473: E/AndroidRuntime(443): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2496) 
02-19 13:55:19.473: E/AndroidRuntime(443): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2512) 
02-19 13:55:19.473: E/AndroidRuntime(443): at android.app.ActivityThread.access$2200(ActivityThread.java:119) 
02-19 13:55:19.473: E/AndroidRuntime(443): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1863) 
02-19 13:55:19.473: E/AndroidRuntime(443): at android.os.Handler.dispatchMessage(Handler.java:99) 
02-19 13:55:19.473: E/AndroidRuntime(443): at android.os.Looper.loop(Looper.java:123) 
02-19 13:55:19.473: E/AndroidRuntime(443): at android.app.ActivityThread.main(ActivityThread.java:4363) 
02-19 13:55:19.473: E/AndroidRuntime(443): at java.lang.reflect.Method.invokeNative(Native Method) 
02-19 13:55:19.473: E/AndroidRuntime(443): at java.lang.reflect.Method.invoke(Method.java:521) 
02-19 13:55:19.473: E/AndroidRuntime(443): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:860) 
02-19 13:55:19.473: E/AndroidRuntime(443): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618) 
02-19 13:55:19.473: E/AndroidRuntime(443): at dalvik.system.NativeStart.main(Native Method) 
02-19 13:55:19.473: E/AndroidRuntime(443): Caused by: java.lang.NullPointerException: println needs a message 
02-19 13:55:19.473: E/AndroidRuntime(443): at android.util.Log.println(Native Method) 
02-19 13:55:19.473: E/AndroidRuntime(443): at android.util.Log.e(Log.java:208) 
02-19 13:55:19.473: E/AndroidRuntime(443): at shc_BalloonSat.namespace.Shc_BalloonSat_Activity.assignInfoToInfoTextView(Shc_BalloonSat_Activity.java:173) 
02-19 13:55:19.473: E/AndroidRuntime(443): at shc_BalloonSat.namespace.Shc_BalloonSat_Activity.onCreate(Shc_BalloonSat_Activity.java:75) 
02-19 13:55:19.473: E/AndroidRuntime(443): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047) 
02-19 13:55:19.473: E/AndroidRuntime(443): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2459) 
02-19 13:55 :19.473: E/AndroidRuntime(443):  ... 11 more 

回答

1

假设你的连接检查电路工作正常,使之作为,if/else语句。如果没有连接显示吐司,否则继续逻辑。

 if(!isNetworkConnected(this))   {  
      Toast.makeText(this, "Please connect to the internet to access the full functionality of this app.", Toast.LENGTH_LONG).show();  
     } 
else 
    {   
    TextView infoTV = (TextView)this.findViewById(R.id.info);  
    infoTV.setText(returned);  
    assignInfoToInfoTextView();  
    assignInfoToHistoryTextView(); 
    } 
+0

我做到了。如果你会注意到我的代码,我有两次相同的if语句。这是因为我把它移到了顶部,忘了删除原文。顶部有一个else语句。 – tylerbhughes 2012-02-19 19:12:34

+0

你看到吐司或没有看到任何东西?如果没有得到烤面包,可能会完成卸载并安装。 – kosa 2012-02-19 19:19:50

+0

我根本看不到烤面包。屏幕是黑色的,它只是坐在那里一分钟,然后崩溃。我尝试卸载它,然后重新安装它,但仍然遇到问题。 – tylerbhughes 2012-02-19 19:29:09