duanba2001 2013-11-16 12:31
浏览 51
已采纳

android使用php连接到mysql

I am trying to get a variable from my database and I am getting force close:

    11-16 16:33:41.757: E/AndroidRuntime(26305): FATAL EXCEPTION: main
11-16 16:33:41.757: E/AndroidRuntime(26305): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.databaseexample/com.example.databaseexample.MainActivity}: java.lang.NullPointerException
11-16 16:33:41.757: E/AndroidRuntime(26305):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1651)
11-16 16:33:41.757: E/AndroidRuntime(26305):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1667)
11-16 16:33:41.757: E/AndroidRuntime(26305):    at android.app.ActivityThread.access$1500(ActivityThread.java:117)
11-16 16:33:41.757: E/AndroidRuntime(26305):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:935)
11-16 16:33:41.757: E/AndroidRuntime(26305):    at android.os.Handler.dispatchMessage(Handler.java:99)
11-16 16:33:41.757: E/AndroidRuntime(26305):    at android.os.Looper.loop(Looper.java:130)
11-16 16:33:41.757: E/AndroidRuntime(26305):    at android.app.ActivityThread.main(ActivityThread.java:3687)
11-16 16:33:41.757: E/AndroidRuntime(26305):    at java.lang.reflect.Method.invokeNative(Native Method)
11-16 16:33:41.757: E/AndroidRuntime(26305):    at java.lang.reflect.Method.invoke(Method.java:507)
11-16 16:33:41.757: E/AndroidRuntime(26305):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:867)
11-16 16:33:41.757: E/AndroidRuntime(26305):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:625)
11-16 16:33:41.757: E/AndroidRuntime(26305):    at dalvik.system.NativeStart.main(Native Method)
11-16 16:33:41.757: E/AndroidRuntime(26305): Caused by: java.lang.NullPointerException
11-16 16:33:41.757: E/AndroidRuntime(26305):    at org.json.JSONTokener.nextCleanInternal(JSONTokener.java:112)
11-16 16:33:41.757: E/AndroidRuntime(26305):    at org.json.JSONTokener.nextValue(JSONTokener.java:90)
11-16 16:33:41.757: E/AndroidRuntime(26305):    at org.json.JSONArray.<init>(JSONArray.java:87)
11-16 16:33:41.757: E/AndroidRuntime(26305):    at org.json.JSONArray.<init>(JSONArray.java:103)
11-16 16:33:41.757: E/AndroidRuntime(26305):    at com.example.databaseexample.MainActivity.onCreate(MainActivity.java:100)
11-16 16:33:41.757: E/AndroidRuntime(26305):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
11-16 16:33:41.757: E/AndroidRuntime(26305):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1615)
11-16 16:33:41.757: E/AndroidRuntime(26305):    ... 11 more

my android part:

public class MainActivity extends ListActivity {

    private List nameValuePairs;
    private JSONArray jArray;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        String result = null;

         InputStream is = null;

         StringBuilder sb=null;

         String result1=null;

         //http post

         try{

         HttpClient httpclient = new DefaultHttpClient();

         HttpPost httppost = new HttpPost("http://lab.sif.mruni.eu/~sanevys/parduotuve/puslapiai/android.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());

         }

         //convert response to string

         try{

         BufferedReader reader = new BufferedReader(new InputStreamReader(is,"iso-8859-1"),8);

         sb = new StringBuilder();

         sb.append(reader.readLine() + "
");

         String line="";

         while ((line = reader.readLine()) != null) {

         sb.append(line + "
");

         }

         is.close();

         result1=sb.toString();

         }catch(Exception e){

         Log.e("log_tag", "Error converting result "+e.toString());

         }

         //paring data

         int fd_id;

         String fd_name;

         try{

         jArray = new JSONArray(result);

         JSONObject json_data=null;

         for(int i=0;i<jArray.length();i++){

         json_data = jArray.getJSONObject(i);

         fd_id=json_data.getInt("FOOD_ID");

         fd_name=json_data.getString("FOOD_NAME");

         }

         }catch(JSONException e){

         Toast.makeText(getBaseContext(), "No Food Found", Toast.LENGTH_LONG).show();

         }catch (ParseException e){

         e.printStackTrace();

         }

         }

}

My php part:

<?php
# FileName="Connection_php_mysql.htm"
# Type="MYSQL"
# HTTP="true"
$hostname_lab = "localhost";
$database_lab = "database";
$username_lab = "username";
$password_lab = "password";
$lab = mysql_pconnect($hostname_lab, $username_lab, $password_lab) or trigger_error(mysql_error(),E_USER_ERROR); 
mysql_select_db("database");
  $sql=mysql_query("select * from FOOD where FOOD_NAME like 'A%'");
  while($row=mysql_fetch_assoc($sql)) $output[]=$row;
  print(json_encode($output));
  mysql_close();
?>

My xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".MainActivity" >

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/hello_world" />
<ListView         
    android:id="@android:id/list"
    android:layout_height="match_parent"
    android:layout_width="match_parent"/>
</RelativeLayout>

Any help/advice will be appreciated :)

  • 写回答

1条回答 默认 最新

  • doushupu2521 2013-11-16 13:10
    关注

    Since you are extending your Activity from ListActivity your ListView id should be

     android:id="@android:id/list"
    

    Check if you have given proper id in xml file

    Or if you have not added a ListView to xml file, add a ListView with the above mentioned id. All the subclasses of ListActivity should have a ListView.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 oracle集群安装出bug
  • ¥15 关于#python#的问题:自动化测试
  • ¥20 问题请教!vue项目关于Nginx配置nonce安全策略的问题
  • ¥15 教务系统账号被盗号如何追溯设备
  • ¥20 delta降尺度方法,未来数据怎么降尺度
  • ¥15 c# 使用NPOI快速将datatable数据导入excel中指定sheet,要求快速高效
  • ¥15 再不同版本的系统上,TCP传输速度不一致
  • ¥15 高德地图2.0 版本点聚合中Marker的位置无法实时更新,如何解决呢?
  • ¥15 DIFY API Endpoint 问题。
  • ¥20 sub地址DHCP问题