0
我sakthi ...我會從數據庫中檢索數據,但不能顯示它在android emulator.if編輯我的代碼它顯示錯誤,因爲文件已停止工作。我會以JSON處理數據。如果有誰知道請幫助我的錯...如何從Android數據庫中檢索數據
public class Reportcard extends Activity
{
@SuppressLint("NewApi")
StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
@SuppressLint("NewApi")
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.reportcard);
StrictMode.setThreadPolicy(policy);
String result = null;
InputStream is = null;
Bundle b=getIntent().getExtras();
String id=(String)b.get("a");
EditText e1=(EditText)findViewById(R.id.editText1);
EditText e2=(EditText)findViewById(R.id.editText2);
EditText e3=(EditText)findViewById(R.id.editText3);
EditText e4=(EditText)findViewById(R.id.editText4);
EditText e5=(EditText)findViewById(R.id.editText5);
EditText e6=(EditText)findViewById(R.id.editText6);
ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
nameValuePairs.add(new BasicNameValuePair("id",id));
try
{
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("http://10.0.2.2/marks.php");
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
HttpResponse response = httpclient.execute(httppost);
HttpEntity entity = response.getEntity();
is = entity.getContent();
Log.e("log_tag", "connection success ");
}
catch(Exception e)
{
Log.e("log_tag", "Error in http connection "+e.toString());
Toast.makeText(getApplicationContext(), "Connection fail", Toast.LENGTH_SHORT).show();
}
//convert response to string
try{
BufferedReader reader = new BufferedReader(new InputStreamReader(is,"iso-8859-1"),8);
StringBuilder sb = new StringBuilder();
String line = null;
while ((line = reader.readLine()) != null)
{
sb.append(line + "\n");
}
is.close();
result=sb.toString();
}
catch(Exception e)
{
Log.e("log_tag", "Error converting result "+e.toString());
Toast.makeText(getApplicationContext(), " Input reading fail", Toast.LENGTH_SHORT).show();
}
//parse json data
try{
JSONObject object = new JSONObject(result);
String ch=object.getString("re");
if(ch.equals("success"))
{
JSONObject no = object.getJSONObject("0");
String a=no.getString("cg");
String w= no.getString("mpc");
String x=no.getString("dwdm");
// String my = NumberFormat.getInstance().format(x);
String e=no.getString("dsp");
String y=no.getString("tqm");
String z=no.getString("eefa");
//String myString = NumberFormat.getInstance().format(e);
e1.setText(a);
//tex1.setText(w);
e2.setText(w);
e3.setText(e);
e4.setText(x);
e5.setText(y);
e6.setText(z);
//e1.setText(z);
}
else
{
Toast.makeText(getApplicationContext(), "Record is not available.. Enter valid number", Toast.LENGTH_SHORT).show();
}
}
catch(JSONException e)
{
Log.e("log_tag", "Error parsing data "+e.toString());
Toast.makeText(getApplicationContext(), "JsonArray fail", Toast.LENGTH_SHORT).show();
}
}
}
這是我logcat的錯誤...
03-29 14:09:08.516: E/AndroidRuntime(1057): FATAL EXCEPTION: main
03-29 14:09:08.516: E/AndroidRuntime(1057): Process: com.example.hell, PID: 1057
03-29 14:09:08.516: E/AndroidRuntime(1057): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.hell/com.example.hell.Reportcard}: java.lang.NullPointerException
03-29 14:09:08.516: E/AndroidRuntime(1057): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2176)
03-29 14:09:08.516: E/AndroidRuntime(1057): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2226)
03-29 14:09:08.516: E/AndroidRuntime(1057): at android.app.ActivityThread.access$700(ActivityThread.java:135)
03-29 14:09:08.516: E/AndroidRuntime(1057): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1397)
03-29 14:09:08.516: E/AndroidRuntime(1057): at android.os.Handler.dispatchMessage(Handler.java:102)
03-29 14:09:08.516: E/AndroidRuntime(1057): at android.os.Looper.loop(Looper.java:137)
03-29 14:09:08.516: E/AndroidRuntime(1057): at android.app.ActivityThread.main(ActivityThread.java:4998)
03-29 14:09:08.516: E/AndroidRuntime(1057): at java.lang.reflect.Method.invokeNative(Native Method)
03-29 14:09:08.516: E/AndroidRuntime(1057): at java.lang.reflect.Method.invoke(Method.java:515)
03-29 14:09:08.516: E/AndroidRuntime(1057): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:777)
03-29 14:09:08.516: E/AndroidRuntime(1057): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:593)
03-29 14:09:08.516: E/AndroidRuntime(1057): at dalvik.system.NativeStart.main(Native Method)
03-29 14:09:08.516: E/AndroidRuntime(1057): Caused by: java.lang.NullPointerException
03-29 14:09:08.516: E/AndroidRuntime(1057): at com.example.hell.Reportcard.onCreate(Reportcard.java:107)
03-29 14:09:08.516: E/AndroidRuntime(1057): at android.app.Activity.performCreate(Activity.java:5243)
03-29 14:09:08.516: E/AndroidRuntime(1057): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
03-29 14:09:08.516: E/AndroidRuntime(1057): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2140)
03-29 14:09:08.516: E/AndroidRuntime(1057): ... 11 more