0

我正在尝试使用带有 JSON 和 php/mysql 的 android 上的 HTTP 请求来显示来自 mysql 数据库的一些数据。

该应用程序只需按下一个按钮,它将在日志中显示从数据库中选择的数据,并使用意图在模拟器上显示第二页..

问题是,当我尝试运行应用程序时,系统在将请求发送到 http 之前会崩溃,我使用 localhost 服务器(wamp)所以我使用的 URL 是:http: //10.0.2.2/studentservice/StudentService/ getStudentByID.php

在我按下按钮后,这是

日志猫

10-15 14:07:28.160: I/dalvikvm(854): threadid=3: reacting to signal 3
10-15 14:07:28.320: I/dalvikvm(854): Wrote stack traces to '/data/anr/traces.txt'
10-15 14:07:28.630: D/gralloc_goldfish(854): Emulator without GPU emulation detected.
10-15 14:07:28.660: I/dalvikvm(854): threadid=3: reacting to signal 3
10-15 14:07:28.670: I/dalvikvm(854): Wrote stack traces to '/data/anr/traces.txt'
10-15 14:07:35.800: E/Error befor http(854): Error before the http request
10-15 14:07:36.210: I/dalvikvm(854): threadid=3: reacting to signal 3
10-15 14:07:36.230: I/dalvikvm(854): Wrote stack traces to '/data/anr/traces.txt'
10-15 14:07:36.730: I/dalvikvm(854): threadid=3: reacting to signal 3
10-15 14:07:36.750: I/dalvikvm(854): Wrote stack traces to '/data/anr/traces.txt'
10-15 14:07:37.250: I/dalvikvm(854): threadid=3: reacting to signal 3
10-15 14:07:37.270: I/dalvikvm(854): Wrote stack traces to '/data/anr/traces.txt'
10-15 14:07:37.770: I/dalvikvm(854): threadid=3: reacting to signal 3
10-15 14:07:37.780: I/dalvikvm(854): Wrote stack traces to '/data/anr/traces.txt'
10-15 14:07:38.281: I/dalvikvm(854): threadid=3: reacting to signal 3
10-15 14:07:38.300: I/dalvikvm(854): Wrote stack traces to '/data/anr/traces.txt'
10-15 14:07:38.800: I/dalvikvm(854): threadid=3: reacting to signal 3
10-15 14:07:38.821: I/dalvikvm(854): Wrote stack traces to '/data/anr/traces.txt'
10-15 14:07:48.400: I/dalvikvm(854): threadid=3: reacting to signal 3
10-15 14:07:48.460: I/dalvikvm(854): Wrote stack traces to '/data/anr/traces.txt'
10-15 14:10:25.630: I/dalvikvm(854): threadid=3: reacting to signal 3
10-15 14:10:25.650: I/dalvikvm(854): Wrote stack traces to '/data/anr/traces.txt'

在它粉碎日志猫展示之后

10-15 14:35:13.781: E/AndroidRuntime(906): FATAL EXCEPTION: main
10-15 14:35:13.781: E/AndroidRuntime(906): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.lebdev.fitguide.activities/com.lebdev.fitguide.activities.SecondaryActivity}: java.lang.NullPointerException: println needs a message
10-15 14:35:13.781: E/AndroidRuntime(906):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1956)
10-15 14:35:13.781: E/AndroidRuntime(906):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1981)
10-15 14:35:13.781: E/AndroidRuntime(906):  at android.app.ActivityThread.access$600(ActivityThread.java:123)
10-15 14:35:13.781: E/AndroidRuntime(906):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1147)
10-15 14:35:13.781: E/AndroidRuntime(906):  at android.os.Handler.dispatchMessage(Handler.java:99)
10-15 14:35:13.781: E/AndroidRuntime(906):  at android.os.Looper.loop(Looper.java:137)
10-15 14:35:13.781: E/AndroidRuntime(906):  at android.app.ActivityThread.main(ActivityThread.java:4424)
10-15 14:35:13.781: E/AndroidRuntime(906):  at java.lang.reflect.Method.invokeNative(Native Method)
10-15 14:35:13.781: E/AndroidRuntime(906):  at java.lang.reflect.Method.invoke(Method.java:511)
10-15 14:35:13.781: E/AndroidRuntime(906):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
10-15 14:35:13.781: E/AndroidRuntime(906):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
10-15 14:35:13.781: E/AndroidRuntime(906):  at dalvik.system.NativeStart.main(Native Method)
10-15 14:35:13.781: E/AndroidRuntime(906): Caused by: java.lang.NullPointerException: println needs a message
10-15 14:35:13.781: E/AndroidRuntime(906):  at android.util.Log.println_native(Native Method)
10-15 14:35:13.781: E/AndroidRuntime(906):  at android.util.Log.e(Log.java:231)
10-15 14:35:13.781: E/AndroidRuntime(906):  at com.lebdev.fitguide.controller.HttpManager.getResponseFromURL(HttpManager.java:70)
10-15 14:35:13.781: E/AndroidRuntime(906):  at com.lebdev.fitguide.activities.SecondaryActivity.onCreate(SecondaryActivity.java:54)
10-15 14:35:13.781: E/AndroidRuntime(906):  at android.app.Activity.performCreate(Activity.java:4465)
10-15 14:35:13.781: E/AndroidRuntime(906):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049)
10-15 14:35:13.781: E/AndroidRuntime(906):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1920)
10-15 14:35:13.781: E/AndroidRuntime(906):  ... 11 more
10-15 14:35:14.341: I/dalvikvm(906): threadid=3: reacting to signal 3
10-15 14:35:14.370: I/dalvikvm(906): Wrote stack traces to '/data/anr/traces.txt'

主要活动

package com.lebdev.fitguide.activities;

import com.lebdev.fitguide.businessModel.Student;
import com.lebdev.fitguide.businessModel.Subject;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends Activity {

    EditText txtMessage;
    TextView lblMessage;

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

        txtMessage = (EditText) findViewById(R.id.txtMessage);
        lblMessage = (TextView) findViewById(R.id.lblMessage);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

    public void Onclick_btnSend(View v) {

         Intent intent  = new Intent(this, SecondaryActivity.class);
         //intent.putExtra("message", txtMessage.getText().toString());
         Student std = new Student(txtMessage.getText().toString(),1234, 1);
         std.getSubjects().add(new Subject("itc", 5));
         intent.putExtra("Student", std);
         startActivity(intent);

         Toast.makeText(this, "i am sending a message here", Toast.LENGTH_SHORT).show();

    }
}

JsonObjectMapper

package com.lebdev.fitguide.om;

import java.util.ArrayList;
import java.util.List;

import org.json.JSONObject;

import android.util.Log;

import com.lebdev.fitguide.businessModel.Student;

public class JsonObjectMapper {

    public static Student jsonToStudent(JSONObject jsonObject) {

        Student std = null;

        try {

            std = new Student(jsonObject.getJSONArray("Student")
                    .getJSONObject(0).getString("Name"),
                    Integer.parseInt(jsonObject.getJSONArray("Student")
                            .getJSONObject(0).getString("index")),
                    Integer.parseInt(jsonObject.getJSONArray("Student")
                            .getJSONObject(0).getString("ID")));
        } catch (Exception ex) {
            Log.e("Error Json Converter", ex.getMessage());
        }

        return std;
    }

    public static List<Student> jsonToStudentList(JSONObject jsonObject) {

        List<Student> stdList = new ArrayList<Student>();
        try {

            for (int i = 0; i < jsonObject.getJSONArray("Student").length(); i++) {

                stdList.add(new Student(jsonObject.getJSONArray("Student")
                        .getJSONObject(i).getString("Name"), Integer
                        .parseInt(jsonObject.getJSONArray("Student")
                                .getJSONObject(i).getString("index")), Integer
                        .parseInt(jsonObject.getJSONArray("Student")
                                .getJSONObject(i).getString("ID"))));
            }
        } catch (Exception ex) {

            Log.e("Error Json Converter", ex.getMessage());
        }

        return stdList;
    }
}

次要对象

package com.lebdev.fitguide.activities;

import java.util.ArrayList;
import java.util.List;

import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;
import org.json.JSONObject;

import com.lebdev.fitguide.businessModel.Student;
import com.lebdev.fitguide.controller.HttpManager;
import com.lebdev.fitguide.controller.JSONParser;
import com.lebdev.fitguide.om.JsonObjectMapper;

import android.app.Activity;
import android.os.Bundle;
import android.os.StrictMode;
import android.util.Log;
import android.view.Menu;
import android.widget.TextView;
import android.widget.Toast;

public class SecondaryActivity extends Activity {

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

        StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder()
                .permitAll().build();
        StrictMode.setThreadPolicy(policy);

        List<NameValuePair> params = new ArrayList<NameValuePair>();
        params.add(new BasicNameValuePair("sid", "2"));

        Log.e("Error befor http", "Error before the http request");
        JSONObject jsonObj = JSONParser
                .parseJSONFromString(new HttpManager()
                        .getResponseFromURL(
                                "http://10.0.2.2/studentservice/StudentService/getStudentByID.php",
                                params));

        Log.e("Error after http", "Error after the http request");

        Student std = JsonObjectMapper.jsonToStudent(jsonObj);

        Log.e("STUDENT DATA", "ID:" + std.getID() +     "Name:" + std.getName()
                + "Index: " + std.getIndex());
/*
        Bundle bundle = getIntent().getExtras();
        if (bundle != null) {
            TextView lblMessage = (TextView) findViewById(R.id.lblSentMessage);
            Student student = bundle.getParcelable("Student");

            lblMessage.setText("I 've recieved a student with a name "
                    + student.getName() + " and has a Subject "
                    + student.getSubjects().get(0).getName() + " with a grade "
                    + student.getSubjects().get(0).getGrade());
        }
*/
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.secondary, menu);
        return true;
    }

}
4

1 回答 1

0

10-15 14:35:13.781: E/AndroidRuntime(906): at com.lebdev.fitguide.controller.HttpManager.getResponseFromURL(HttpManager.java:70)

这是指向您的代码、行号和所有内容的指针,关于空指针(到 Log.e() 的空消息)来自何处并导致崩溃。

这从你的 logcat 中很明显,所以你正在做一些事情

JSONObject jsonObj = JSONParser .parseJSONFromString(new HttpManager() .getResponseFromURL( "http://10.0.2.2/studentservice/StudentService/getStudentByID.php", params));

这导致将空味精写入 Log.e()。

于 2013-10-15T14:59:19.010 回答