SignUp形式:使用Android 4.0将数据插入MySQL

时间:2012-01-04 19:57:40

标签: android mysql login http-post

目前,我正在为Android(本机应用)中的新用户开发注册表单,并使用.php文件。通过runnig代码我得到以下错误:“不幸的是,应用程序已停止”。我尝试以多种方式更改代码来解决它,但没有任何结果。这是php代码:

<?php 
$dbcnx = @mysql_connect("localhost", "root", "root");
$db = "dbname";
mysql_select_db($db, $dbcnx);
$user_id=$_POST['user'];
$passwd=$_POST['passwd'];
$query = 'INSERT INTO `users` VALUES (NULL, \''.$user_id.'\', \''.$passwd.'\')';
$result = mysql_query($query) or die ("<b>Query failed:</b> " . mysql_error());

mysql_free_result($result);
mysql_close($dbcnx);
?>

和.java文件:

import java.util.ArrayList;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.protocol.HTTP;
import android.app.ListActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

public class Register extends ListActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.register);

    Button Insert=(Button)findViewById(R.id.send_button);
    Insert.setOnClickListener(new View.OnClickListener() {          
        public void onClick(View v) {
            // TODO Auto-generated method stub
            try {
                EditText Text1=(EditText)findViewById(R.id.user_id);
                EditText Text2=(EditText)findViewById(R.id.passwd);                 

                ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
                nameValuePairs.add(new BasicNameValuePair("user", ""+Text1.getText().toString()+""));
                nameValuePairs.add(new BasicNameValuePair("passwd", ""+Text2.getText().toString()+""));

                Log.e(""+Text1.getText().toString(),"0");
                Log.e(""+Text2.getText().toString(),"0");
                HttpClient httpclient = new DefaultHttpClient();
                HttpPost httppost = new HttpPost("http://10.0.2.2/jku/user_reg.php");
                httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs, HTTP.UTF_8));
                HttpResponse response = httpclient.execute(httppost);
            }
            catch(Exception e) {
                Log.e("log_tag", "Error in http connection "+e.toString());
            }

        }
    });

}   

}

我注意到我的错误,所以我是android的初学者,任何建议都会有所帮助。

我已经检查过LogCat,但没有理解。所以这是日志故事:

01-04 21:37:13.275: D/gralloc_goldfish(552): Emulator without GPU emulation detected.
01-04 21:37:13.345: W/TextLayoutCache(552): computeValuesWithHarfbuzz -- need to force to single run
01-04 21:37:47.385: D/AndroidRuntime(552): Shutting down VM
01-04 21:37:47.385: W/dalvikvm(552): threadid=1: thread exiting with uncaught exception (group=0x409961f8)
01-04 21:37:47.476: E/AndroidRuntime(552): FATAL EXCEPTION: main
01-04 21:37:47.476: E/AndroidRuntime(552): java.lang.RuntimeException: Unable to start activity ComponentInfo{jku.project.kusss/jku.project.kusss.Register}: java.lang.RuntimeException: Your content must have a ListView whose id attribute is 'android.R.id.list'
01-04 21:37:47.476: E/AndroidRuntime(552):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1955)
01-04 21:37:47.476: E/AndroidRuntime(552):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1980)
01-04 21:37:47.476: E/AndroidRuntime(552):  at android.app.ActivityThread.access$600(ActivityThread.java:122)
01-04 21:37:47.476: E/AndroidRuntime(552):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1146)
01-04 21:37:47.476: E/AndroidRuntime(552):  at android.os.Handler.dispatchMessage(Handler.java:99)
01-04 21:37:47.476: E/AndroidRuntime(552):  at android.os.Looper.loop(Looper.java:137)
01-04 21:37:47.476: E/AndroidRuntime(552):  at android.app.ActivityThread.main(ActivityThread.java:4340)
01-04 21:37:47.476: E/AndroidRuntime(552):  at java.lang.reflect.Method.invokeNative(Native Method)
01-04 21:37:47.476: E/AndroidRuntime(552):  at java.lang.reflect.Method.invoke(Method.java:511)
01-04 21:37:47.476: E/AndroidRuntime(552):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
01-04 21:37:47.476: E/AndroidRuntime(552):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
01-04 21:37:47.476: E/AndroidRuntime(552):  at dalvik.system.NativeStart.main(Native Method)
01-04 21:37:47.476: E/AndroidRuntime(552): Caused by: java.lang.RuntimeException: Your content must have a ListView whose id attribute is 'android.R.id.list'
01-04 21:37:47.476: E/AndroidRuntime(552):  at android.app.ListActivity.onContentChanged(ListActivity.java:243)
01-04 21:37:47.476: E/AndroidRuntime(552):  at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:254)
01-04 21:37:47.476: E/AndroidRuntime(552):  at android.app.Activity.setContentView(Activity.java:1835)
01-04 21:37:47.476: E/AndroidRuntime(552):  at jku.project.kusss.Register.onCreate(Register.java:27)
01-04 21:37:47.476: E/AndroidRuntime(552):  at android.app.Activity.performCreate(Activity.java:4465)
01-04 21:37:47.476: E/AndroidRuntime(552):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049)
01-04 21:37:47.476: E/AndroidRuntime(552):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1919)
01-04 21:37:47.476: E/AndroidRuntime(552):  ... 11 more

1 个答案:

答案 0 :(得分:1)

您的答案在错误堆栈中:

Caused by: java.lang.RuntimeException: Your content must have a ListView whose id
attribute is 'android.R.id.list'

由于您没有显示所有代码,因此我们假设您正在使用ListActivity或ListFragment或其他任何代码。要使用它,视图必须具有ID为android.R.id.list的ListView子项。

阅读this d.android.com link中“屏幕布局”下的段落。