无法使用mysql插入数据

时间:2013-05-06 04:33:43

标签: php android mysql

我试图插入数据mysql服务器,但是当我运行我的应用程序时。它立刻强行关闭。在编码方面需要帮助。我把我的logcat txt文件放在下面。请帮我看一下出了什么问题。 感谢。

logcat.txt

05-06 04:23:35.294: E/Trace(837): error opening trace file: No such file or directory (2)
05-06 04:23:36.744: D/dalvikvm(837): GC_FOR_ALLOC freed 59K, 2% free 10795K/11011K, paused 128ms, total 130ms
05-06 04:23:36.824: I/dalvikvm-heap(837): Grow heap (frag case) to 16.065MB for 5760016-byte allocation
05-06 04:23:36.934: D/dalvikvm(837): GC_CONCURRENT freed <1K, 2% free 16420K/16647K, paused 43ms+17ms, total 106ms
05-06 04:23:37.484: D/AndroidRuntime(837): Shutting down VM
05-06 04:23:37.484: W/dalvikvm(837): threadid=1: thread exiting with uncaught exception (group=0x40a13300)
05-06 04:23:37.504: E/AndroidRuntime(837): FATAL EXCEPTION: main
05-06 04:23:37.504: E/AndroidRuntime(837): java.lang.RuntimeException: Unable to start activity ComponentInfo{mp.memberuse/mp.memberuse.LoginRegister}: java.lang.RuntimeException: Your content must have a ListView whose id attribute is 'android.R.id.list'
05-06 04:23:37.504: E/AndroidRuntime(837):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2059)
05-06 04:23:37.504: E/AndroidRuntime(837):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2084)
05-06 04:23:37.504: E/AndroidRuntime(837):  at android.app.ActivityThread.access$600(ActivityThread.java:130)
05-06 04:23:37.504: E/AndroidRuntime(837):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1195)
05-06 04:23:37.504: E/AndroidRuntime(837):  at android.os.Handler.dispatchMessage(Handler.java:99)
05-06 04:23:37.504: E/AndroidRuntime(837):  at android.os.Looper.loop(Looper.java:137)
05-06 04:23:37.504: E/AndroidRuntime(837):  at android.app.ActivityThread.main(ActivityThread.java:4745)
05-06 04:23:37.504: E/AndroidRuntime(837):  at java.lang.reflect.Method.invokeNative(Native Method)
05-06 04:23:37.504: E/AndroidRuntime(837):  at java.lang.reflect.Method.invoke(Method.java:511)
05-06 04:23:37.504: E/AndroidRuntime(837):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
05-06 04:23:37.504: E/AndroidRuntime(837):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
05-06 04:23:37.504: E/AndroidRuntime(837):  at dalvik.system.NativeStart.main(Native Method)
05-06 04:23:37.504: E/AndroidRuntime(837): Caused by: java.lang.RuntimeException: Your content must have a ListView whose id attribute is 'android.R.id.list'
05-06 04:23:37.504: E/AndroidRuntime(837):  at android.app.ListActivity.onContentChanged(ListActivity.java:243)
05-06 04:23:37.504: E/AndroidRuntime(837):  at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:259)
05-06 04:23:37.504: E/AndroidRuntime(837):  at android.app.Activity.setContentView(Activity.java:1867)
05-06 04:23:37.504: E/AndroidRuntime(837):  at mp.memberuse.LoginRegister.onCreate(LoginRegister.java:46)
05-06 04:23:37.504: E/AndroidRuntime(837):  at android.app.Activity.performCreate(Activity.java:5008)
05-06 04:23:37.504: E/AndroidRuntime(837):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1079)
05-06 04:23:37.504: E/AndroidRuntime(837):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2023)
05-06 04:23:37.504: E/AndroidRuntime(837):  ... 11 more
05-06 04:23:40.334: I/Process(837): Sending signal. PID: 837 SIG: 9

LoginRegister.java

package mp.memberuse;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;

import org.apache.http.HttpEntity;
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.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicHttpResponse;
import org.apache.http.message.BasicNameValuePair;

import android.app.Activity;
import android.app.ListActivity;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.provider.Settings.NameValueTable;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TabHost;
import android.widget.TabHost.TabSpec;
import android.widget.TextView;

public class LoginRegister extends ListActivity {

Button btn1, btn2, btn3;
EditText tf1, tf2, tf3, tf4, tf5, tf6, tf7, tf8, tf9, tf10, tf11;
TextView tv1, tv2;
SQLiteDatabase db;

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    TabHost tabs = (TabHost) this.findViewById(R.id.lt2tabhost);
    tabs.setup();

    TabSpec ts1 = tabs.newTabSpec("Login");
    ts1.setIndicator("Login");
    ts1.setContent(R.id.c1);
    tabs.addTab(ts1);

    TabSpec ts2 = tabs.newTabSpec("Register");
    ts2.setIndicator("Register");
    ts2.setContent(R.id.c2);
    tabs.addTab(ts2);

    btn1 = (Button)findViewById(R.id.button1);
    btn2 = (Button)findViewById(R.id.button2);
    btn3 = (Button)findViewById(R.id.button3);

    tf1=(EditText) findViewById(R.id.editText1);
    tf2=(EditText) findViewById(R.id.editText2);
    tf3=(EditText) findViewById(R.id.editText3);
    tf4=(EditText) findViewById(R.id.editText4);
    tf5=(EditText) findViewById(R.id.editText5);
    tf6=(EditText) findViewById(R.id.editText6);
    tf7=(EditText) findViewById(R.id.editText7);
    tf8=(EditText) findViewById(R.id.editText8);
    tf9=(EditText) findViewById(R.id.editText9);
    tf10=(EditText) findViewById(R.id.editText10);

    tv1=(TextView) findViewById(R.id.login);
    tv2=(TextView) findViewById(R.id.register);



    btn1.setOnClickListener(new View.OnClickListener(){
        public void onClick(View v)
        {
            String username, password;
            username = tf1.getText().toString();
            password = tf2.getText().toString();

            /**try
            {
                String sql="select * from Members;";
                Cursor c1= db.rawQuery(sql, new String[]{});
                String sqlusername, sqlpassword, fullname;


                while(c1.moveToNext())
                {
                    sqlusername = c1.getString(0);
                    sqlpassword = c1.getString(1);
                    fullname = c1.getString(2);

                    if(username.equals(sqlusername) && password.equals(sqlpassword))
                    {
                        SharedPreferences prefs = getSharedPreferences("myPreferences",Context.MODE_PRIVATE);
                        SharedPreferences.Editor editor = prefs.edit();
                        editor.putString("fullname", fullname);
                        editor.commit();

                        Intent intent = new Intent(LoginRegister.this, SendMessage.class);
                        startActivity(intent);
                    }
                    else
                    {
                        tv1.setText("Invalid user");
                    }
                }
            }
            catch(Exception e)
            {
            }**/
        }
    });

    btn2.setOnClickListener(new View.OnClickListener(){
        public void onClick(View v)
        {
            String username, password, cpassword, fullname, nric, address, phone, email;
            username = tf3.getText().toString();
            password = tf4.getText().toString();
            cpassword = tf5.getText().toString();
            fullname = tf6.getText().toString();
            nric = tf7.getText().toString();
            address = tf8.getText().toString();
            phone = tf9.getText().toString();
            email = tf10.getText().toString();

            if(!password.equals(cpassword))
            {
                tv2.setText("Password & Confirm Password does not match.");
            }
            else if(username.equals("") || password.equals("") || cpassword.equals("") || fullname.equals("") || nric.equals("") || address.equals("") || phone.equals("") || email.equals(""))
            {
                tv2.setText("Do not leave any field empty.");
            }
            else
            {
                InputStream is = null;
                ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();       
                nameValuePairs.add(new BasicNameValuePair("username",username));
                nameValuePairs.add(new BasicNameValuePair("password",password));
                nameValuePairs.add(new BasicNameValuePair("fullname",fullname));
                nameValuePairs.add(new BasicNameValuePair("nric",nric));
                nameValuePairs.add(new BasicNameValuePair("address",address));
                nameValuePairs.add(new BasicNameValuePair("phone",phone));
                nameValuePairs.add(new BasicNameValuePair("email",email));

                try
                {
                    HttpClient httpclient = new DefaultHttpClient();
                    HttpPost httppost = new HttpPost("http://localhost:81/Connections.php");
                    httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
                    HttpResponse response = httpclient.execute(httppost);
                    HttpEntity entity = response.getEntity();
                    is = entity.getContent();
                }
                catch(Exception e)
                {
                    Log.e("log_tag", "Error in http connection"+e.toString());
                }
                    tv2.setText("Register Complete.");
                    tf3.setText("");
                    tf4.setText("");
                    tf5.setText("");
                    tf6.setText("");
                    tf7.setText("");
                    tf8.setText("");
                    tf9.setText("");
                    tf10.setText(""); 
            }
        }
    });

    btn3.setOnClickListener(new View.OnClickListener(){
        public void onClick(View v)
        {
            tf3.setText("");
            tf4.setText("");
            tf5.setText("");
            tf6.setText("");
            tf7.setText("");
            tf8.setText("");
            tf9.setText("");
            tf10.setText("");           
        }
    });
}
}

PHP代码

    <?php

    $hostname_localhost ="localhost";
    $database_localhost ="members";
    $username_localhost ="root";
    $password_localhost ="";

    mysql_connect($hostname_localhost,$username_localhost,$password_localhost);


mysql_select_db("members");

$sql=mysql_query("INSERT INTO members(Username, Password, Fullname, NRIC, Address, Phone, Email)
        VALUES('".$_POST['username']."', '".$_POST['password']."', '".$_POST['fullname']."', '".$_POST['nric']."', '".$_POST['address']."', '".$_POST['phone']."', '".$_POST['email']."')");

$r=mysql_query($sql);

if(!$r)

    echo "Error in query: ".mysql_error();

mysql_close();
    ?>

2 个答案:

答案 0 :(得分:1)

在日志中:

  

引起:java.lang.RuntimeException:您的内容必须有   ListView的id属性为'android.R.id.list'

表示您需要在xml布局中将ListView ID声明为android:id="@+id/android:list"

答案 1 :(得分:1)

您必须使用此IP地址10.0.2.2连接到localhost

new HttpPost("http://localhost:81/Connections.php");

应该是

new HttpPost("http://10.0.2.2:81/Connections.php");

Documentation