2012-01-04 127 views
0

目前,我正在为Android(原生应用程序)中的新用户以及使用.php文件开发注册表单。通过runnig代码我得到以下错误:“不幸的是,应用程序已停止”。我试图用多种方式改变代码来解决它,但没有任何结果。这里是PHP代码:SignUp表单:使用Android 4.0将数据插入到MySQL

<?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 
+0

使用logcat获取有关错误的信息并添加堆栈跟踪。 – fkerber 2012-01-04 19:59:59

+0

你能发布错误的堆栈跟踪吗?你可以从eclipse或“adb logcat”获得这个输出。另外,出于安全原因,您应该在与SQL进行SQL交互时查看PDO。 http://ca3.php.net/pdo它让你的生活更容易编写更安全的SQL交互。 – jlindenbaum 2012-01-04 20:00:02

+0

我已经在LogCat之上添加了。顺便说一下,代码程序是正确的?没有错误? – Dozent 2012-01-04 20:39:49

回答

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中'Screen Layout'下的段落。

+0

**克里斯**,谢谢你的建议。我只是将.java ListActivity更改为“活动”,并且运行时没有错误。但是在点击注册按钮时没有任何事情发生:(但是,即没有将数据保存到mysql中。有人可以帮忙吗? – Dozent 2012-01-05 07:53:06

+0

任何人都可以检查这个代码过程吗?它不工作:( – Dozent 2012-01-07 23:56:49