[英]App crashing when trying to check network connection
I have just started doing some android programming, as I downloaded the AIDE for my phone. 我刚刚开始做一些Android编程,因为我为手机下载了AIDE。 I am trying to make an app which requires a connection to a website, but every time I run the app and try any network related operations, it just crashes.
我试图制作一个需要连接到网站的应用程序,但是每次我运行该应用程序并尝试进行任何与网络相关的操作时,它都会崩溃。
I have set the permissions for INTERNET and ACCESS_NETWORK_STATE in the manifest file. 我已经在清单文件中设置了INTERNET和ACCESS_NETWORK_STATE的权限。
I would show a log of the error, but i am unaware about how to do this with the AIDE app, or on an android device, as I don't have access to proper IDE on a computer. 我将显示错误的日志,但是我不知道如何使用AIDE应用程序或在android设备上执行此操作,因为我无法访问计算机上的正确IDE。
Help would be appreciated 帮助将不胜感激
public class MainActivity extends Activity{
private TextView text;
@Override
public void onCreate ( Bundle savedInstanceState ){
super onCreate( savedInstanceState );
setContextView( R.layout.main );
textView = ( TextView )findViewById( R.id.myText );
}
public void checkConn(){
ConnectivityManager connMgr = ( ConnectivityManager ) getSystemService ( Context.CONNECTIVITY_SERVICE);
NetworkInfo networkInfo = connMgr getActiveNetworkInfo();
if ( networkInfo != null && networkInfo.idconnected() ){
textView.setText( "conection" );
}else{
textView.setText( "no connection" );
}
EDIT: I fixed the check connection issue, but now I added the method to connect to my desired website, and now my app keeps crashing again. 编辑:我修复了检查连接问题,但是现在我添加了连接到所需网站的方法,现在我的应用程序再次崩溃。
private void makeConnection(){
try{
URL url = new URL("website");
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.connect();
}catch (MalformedURLException e) {
textView.setText( "no connection" );
}catch ( IOExcpetion e ){
textView.setText( "no connection" );
}
}
Try this: 尝试这个:
MainActivity.java MainActivity.java
import android.app.Activity;
import android.content.Context;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.Bundle;
import android.widget.TextView;
public class MainActivity extends Activity {
private TextView textView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main );
textView = ( TextView )findViewById( R.id.myText );
checkConn();
}
public void checkConn(){
ConnectivityManager cm = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo netInfo = cm.getActiveNetworkInfo();
if (netInfo != null && netInfo.isConnectedOrConnecting()) {
textView.setText( "conection" );
}else{
textView.setText( "no connection" );
}
}
}
main.xml: main.xml中:
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.SPTechnos.ambassador.Home"
tools:ignore="MergeRootFrame" >
<TextView
android:id="@+id/myText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</FrameLayout>
Don't forget to add this permission in the application manifest: 不要忘记在应用程序清单中添加此权限:
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
you are define as like this 你是这样定义的
private TextView text
And you are using as 而您正在使用
textView = ( TextView )findViewById( R.id.myText );
then definitely its crash. 然后肯定是崩溃。 Change like this
像这样改变
private TextView textView;
and try to run and check... 并尝试运行并检查...
Hope It helps. 希望能帮助到你。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.