我查看了许多具有相同标题的帖子,但每个人似乎都有不同的解决方案。在保存和编译时,我在Eclipse中没有出现错误。
我们不想要logcat。我们想要成功演员。这个问题是什么问题?如果你知道的话。请告诉我为什么?
Register.java
package com.example.tab;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
public class Register extends Activity {
String myId, myPassword, myEmail, myResult;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.register);
}
public void RegisterClicked(View v) {
Intent intent = null;
switch(v.getId()) {
case R.id.btn_register:
intent = new Intent(getApplicationContext(), Register.class);
startActivity(intent);
finish();
myId = ((EditText)(findViewById(R.id.registerName))).getText().toString();
myPassword = ((EditText)(findViewById(R.id.registerPassword))).getText().toString();
myEmail = ((EditText)(findViewById(R.id.registerEmail))).getText().toString();
httpPostData();
}
}
public void httpPostData() {
// TODO Auto-generated method stub
try {
URL url = new URL("http://localhost/register.php");
HttpURLConnection http = (HttpURLConnection) url.openConnection();
http.setDefaultUseCaches(false);
http.setDoInput(true);
http.setDoOutput(true);
http.setRequestMethod("POST");
http.setRequestProperty("content-type", "application/x-www-form-urlencoded");
StringBuffer buffer = new StringBuffer();
buffer.append("id").append("=").append(myId).append("&");
buffer.append("password").append("=").append(myPassword).append("&");
buffer.append("email").append("=").append(myEmail).append("&");
OutputStreamWriter outStream = new OutputStreamWriter(http.getOutputStream(), "UTF-8");
PrintWriter writer = new PrintWriter(outStream);
writer.write(buffer.toString());
writer.flush();
InputStreamReader tmp = new InputStreamReader(http.getInputStream(), "UTF-8");
BufferedReader reader = new BufferedReader(tmp);
StringBuilder builder = new StringBuilder();
String str;
while ((str = reader.readLine()) != null) {
builder.append(str + "\n");
}
} catch (MalformedURLException e) {
} catch (IOException e) {
}
}
}
register.xml
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#3b3b3b" >
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:padding="10dip" >
<!-- View Title Label -->
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="10dip"
android:text="REGISTER"
android:textSize="25sp"
android:textStyle="bold" />
<!-- Name Label -->
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Name" />
<!-- Name TextField -->
<EditText
android:id="@+id/registerName"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
<!-- Email Label -->
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Email" />
<!-- Email TextField -->
<EditText
android:id="@+id/registerEmail"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
<!-- Password Label -->
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="15dip"
android:text="Password" />
<!-- Password TextField -->
<EditText
android:id="@+id/registerPassword"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:password="true" />
<!-- Error message -->
<TextView android:id="@+id/register_error"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textColor="#e30000"
android:paddingTop="10dip"
android:textStyle="bold"/>
<!-- Login Button -->
<Button
android:id="@+id/btn_register"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="20dip"
android:onClick="RegisterClicked"
android:text="Register" />
<!-- Link to Login Screen -->
<Button
android:id="@+id/btnLinkToLoginScreen"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="40dip"
android:background="@null"
android:text="Already registred. Login Me!"
android:textColor="#21dbd4"
android:textStyle="bold" />
</LinearLayout>
</ScrollView>
我的Logcat
06-02 04:46:56.694: E/AndroidRuntime(2396): FATAL EXCEPTION: main
06-02 04:46:56.694: E/AndroidRuntime(2396): java.lang.IllegalStateException: Could not execute method of the activity
06-02 04:46:56.694: E/AndroidRuntime(2396): at android.view.View$1.onClick(View.java:3597)
06-02 04:46:56.694: E/AndroidRuntime(2396): at android.view.View.performClick(View.java:4202)
06-02 04:46:56.694: E/AndroidRuntime(2396): at android.view.View$PerformClick.run(View.java:17340)
06-02 04:46:56.694: E/AndroidRuntime(2396): at android.os.Handler.handleCallback(Handler.java:725)
06-02 04:46:56.694: E/AndroidRuntime(2396): at android.os.Handler.dispatchMessage(Handler.java:92)
06-02 04:46:56.694: E/AndroidRuntime(2396): at android.os.Looper.loop(Looper.java:137)
06-02 04:46:56.694: E/AndroidRuntime(2396): at android.app.ActivityThread.main(ActivityThread.java:5039)
06-02 04:46:56.694: E/AndroidRuntime(2396): at java.lang.reflect.Method.invokeNative(Native Method)
06-02 04:46:56.694: E/AndroidRuntime(2396): at java.lang.reflect.Method.invoke(Method.java:511)
06-02 04:46:56.694: E/AndroidRuntime(2396): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
06-02 04:46:56.694: E/AndroidRuntime(2396): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
06-02 04:46:56.694: E/AndroidRuntime(2396): at dalvik.system.NativeStart.main(Native Method)
06-02 04:46:56.694: E/AndroidRuntime(2396): Caused by: java.lang.reflect.InvocationTargetException
06-02 04:46:56.694: E/AndroidRuntime(2396): at java.lang.reflect.Method.invokeNative(Native Method)
06-02 04:46:56.694: E/AndroidRuntime(2396): at java.lang.reflect.Method.invoke(Method.java:511)
06-02 04:46:56.694: E/AndroidRuntime(2396): at android.view.View$1.onClick(View.java:3592)
06-02 04:46:56.694: E/AndroidRuntime(2396): ... 11 more
06-02 04:46:56.694: E/AndroidRuntime(2396): Caused by: android.os.NetworkOnMainThreadException
06-02 04:46:56.694: E/AndroidRuntime(2396): at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1117)
06-02 04:46:56.694: E/AndroidRuntime(2396): at java.net.InetAddress.lookupHostByName(InetAddress.java:385)
06-02 04:46:56.694: E/AndroidRuntime(2396): at java.net.InetAddress.getAllByNameImpl(InetAddress.java:236)
06-02 04:46:56.694: E/AndroidRuntime(2396): at java.net.InetAddress.getAllByName(InetAddress.java:214)
06-02 04:46:56.694: E/AndroidRuntime(2396): at libcore.net.http.HttpConnection.<init>(HttpConnection.java:70)
06-02 04:46:56.694: E/AndroidRuntime(2396): at libcore.net.http.HttpConnection.<init>(HttpConnection.java:50)
06-02 04:46:56.694: E/AndroidRuntime(2396): at libcore.net.http.HttpConnection$Address.connect(HttpConnection.java:340)
06-02 04:46:56.694: E/AndroidRuntime(2396): at libcore.net.http.HttpConnectionPool.get(HttpConnectionPool.java:87)
06-02 04:46:56.694: E/AndroidRuntime(2396): at libcore.net.http.HttpConnection.connect(HttpConnection.java:128)
06-02 04:46:56.694: E/AndroidRuntime(2396): at libcore.net.http.HttpEngine.openSocketConnection(HttpEngine.java:316)
06-02 04:46:56.694: E/AndroidRuntime(2396): at libcore.net.http.HttpEngine.connect(HttpEngine.java:311)
06-02 04:46:56.694: E/AndroidRuntime(2396): at libcore.net.http.HttpEngine.sendSocketRequest(HttpEngine.java:290)
06-02 04:46:56.694: E/AndroidRuntime(2396): at libcore.net.http.HttpEngine.sendRequest(HttpEngine.java:240)
06-02 04:46:56.694: E/AndroidRuntime(2396): at libcore.net.http.HttpURLConnectionImpl.connect(HttpURLConnectionImpl.java:81)
06-02 04:46:56.694: E/AndroidRuntime(2396): at libcore.net.http.HttpURLConnectionImpl.getOutputStream(HttpURLConnectionImpl.java:197)
06-02 04:46:56.694: E/AndroidRuntime(2396): at com.example.tab.Register.httpPostData(Register.java:65)
06-02 04:46:56.694: E/AndroidRuntime(2396): at com.example.tab.Register.RegisterClicked(Register.java:40)
06-02 04:46:56.694: E/AndroidRuntime(2396): ... 14 more
06-02 04:46:56.940: E/Trace(2420): error opening trace file: No such file or directory (2)
AndroidManifast.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.tab"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="18" />
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.example.tab.MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".MyTabActivity" />
<activity
android:name=".pants10_1Activity" />
<activity
android:name=".PhotoMainFragment" />
<activity
android:name=".shirt10_1Activity" />
<activity
android:name=".TabMain" />
<activity
android:name=".Login" />
<activity
android:name=".Register" />
<activity
android:name=".Logout" />
</application>
</manifest>
答案 0 :(得分:0)
您无法在主线程上运行网络IO。您需要在AsyncTask或Thread中运行它。