致命异常:main,MainActivity.java

时间:2016-01-23 19:21:41

标签: java android android-studio

Hello我正在开发一个简单的Android应用程序,当我向类添加一些参数并在移动设备上运行应用程序问题显示如下。这是主要类别;

    package com.example.webserviceapp;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

public class MainActivity extends AppCompatActivity {
EditText name,email,pass;
    Button log;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_main);
        name=(EditText)findViewById(R.id.name);
        email=(EditText)findViewById(R.id.email);
        pass=(EditText)findViewById(R.id.pass);
        log = (Button)findViewById(R.id.btn);
        log.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String nm = name.getText().toString();
                String em = email.getText().toString();
                String pas = pass.getText().toString();
                new Logindata().getdata(nm,em,pas);
            }
        });

    }
}

这里是输出

01-23 20:41:37.928 16396-16396/com.example.webserviceapp E/AndroidRuntime: FATAL EXCEPTION: main
01-23 20:41:37.928 16396-16396/com.example.webserviceapp E/AndroidRuntime: Process: com.example.webserviceapp, PID: 16396
01-23 20:41:37.928 16396-16396/com.example.webserviceapp E/AndroidRuntime: android.os.NetworkOnMainThreadException
01-23 20:41:37.928 16396-16396/com.example.webserviceapp E/AndroidRuntime:     at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1147)
01-23 20:41:37.928 16396-16396/com.example.webserviceapp E/AndroidRuntime:     at libcore.io.BlockGuardOs.connect(BlockGuardOs.java:110)
01-23 20:41:37.928 16396-16396/com.example.webserviceapp E/AndroidRuntime:     at libcore.io.IoBridge.connectErrno(IoBridge.java:137)
01-23 20:41:37.928 16396-16396/com.example.webserviceapp E/AndroidRuntime:     at libcore.io.IoBridge.connect(IoBridge.java:122)
01-23 20:41:37.928 16396-16396/com.example.webserviceapp E/AndroidRuntime:     at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:183)
01-23 20:41:37.928 16396-16396/com.example.webserviceapp E/AndroidRuntime:     at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:456)
01-23 20:41:37.928 16396-16396/com.example.webserviceapp E/AndroidRuntime:     at java.net.Socket.connect(Socket.java:882)
01-23 20:41:37.928 16396-16396/com.example.webserviceapp E/AndroidRuntime:     at org.apache.http.conn.scheme.PlainSocketFactory.connectSocket(PlainSocketFactory.java:119)
01-23 20:41:37.928 16396-16396/com.example.webserviceapp E/AndroidRuntime:     at org.apache.http.impl.conn.DefaultClientConnectionOperator.openConnection(DefaultClientConnectionOperator.java:144)
01-23 20:41:37.928 16396-16396/com.example.webserviceapp E/AndroidRuntime:     at org.apache.http.impl.conn.AbstractPoolEntry.open(AbstractPoolEntry.java:164)
01-23 20:41:37.928 16396-16396/com.example.webserviceapp E/AndroidRuntime:     at org.apache.http.impl.conn.AbstractPooledConnAdapter.open(AbstractPooledConnAdapter.java:119)
01-23 20:41:37.928 16396-16396/com.example.webserviceapp E/AndroidRuntime:     at org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:360)
01-23 20:41:37.928 16396-16396/com.example.webserviceapp E/AndroidRuntime:     at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:555)
01-23 20:41:37.928 16396-16396/com.example.webserviceapp E/AndroidRuntime:     at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:487)
01-23 20:41:37.928 16396-16396/com.example.webserviceapp E/AndroidRuntime:     at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:465)
01-23 20:41:37.928 16396-16396/com.example.webserviceapp E/AndroidRuntime:     at com.example.webserviceapp.Loginparser.loginparse(Loginparser.java:26)
01-23 20:41:37.928 16396-16396/com.example.webserviceapp E/AndroidRuntime:     at com.example.webserviceapp.Logindata.getdata(Logindata.java:21)
01-23 20:41:37.928 16396-16396/com.example.webserviceapp E/AndroidRuntime:     at com.example.webserviceapp.MainActivity$1.onClick(MainActivity.java:26)
01-23 20:41:37.928 16396-16396/com.example.webserviceapp E/AndroidRuntime:     at android.view.View.performClick(View.java:4756)
01-23 20:41:37.928 16396-16396/com.example.webserviceapp E/AndroidRuntime:     at android.view.View$PerformClick.run(View.java:19761)
01-23 20:41:37.928 16396-16396/com.example.webserviceapp E/AndroidRuntime:     at android.os.Handler.handleCallback(Handler.java:739)
01-23 20:41:37.928 16396-16396/com.example.webserviceapp E/AndroidRuntime:     at android.os.Handler.dispatchMessage(Handler.java:95)
01-23 20:41:37.928 16396-16396/com.example.webserviceapp E/AndroidRuntime:     at android.os.Looper.loop(Looper.java:135)
01-23 20:41:37.928 16396-16396/com.example.webserviceapp E/AndroidRuntime:     at android.app.ActivityThread.main(ActivityThread.java:5253)
01-23 20:41:37.928 16396-16396/com.example.webserviceapp E/AndroidRuntime:     at java.lang.reflect.Method.invoke(Native Method)
01-23 20:41:37.928 16396-16396/com.example.webserviceapp E/AndroidRuntime:     at java.lang.reflect.Method.invoke(Method.java:372)
01-23 20:41:37.928 16396-16396/com.example.webserviceapp E/AndroidRuntime:     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:900)
01-23 20:41:37.928 16396-16396/com.example.webserviceapp E/AndroidRuntime:     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:695)
无论如何,谢谢你的帮助

1 个答案:

答案 0 :(得分:1)

您正在主应用程序主题上进行互联网活动。在另一个线程上做。使用AsyncTask。