当我在Android 2.3中运行我的应用程序时,它工作正常,而像4.2.2,4.4.4这样的较新版本显示错误。我的应用程序正在获取Json数据并将其显示为表格视图。所以请帮助我摆脱这个。下面是所有必要的代码。
MainActivity.java
Button button = (Button) findViewById(R.id.button1);
button.setOnClickListener(new View.OnClickListener()
{
public void onClick(View view)
{
String result = null;
InputStream is = null;
try{
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("http://example.com/selectall.php");
HttpResponse response = httpclient.execute(httppost);
HttpEntity entity = response.getEntity();
is = entity.getContent();
Log.e("log_tag", "connection success ");
// Toast.makeText(getApplicationContext(), "pass", Toast.LENGTH_SHORT).show();
}
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");
// Toast.makeText(getApplicationContext(), "Input Reading pass", Toast.LENGTH_SHORT).show();
}
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
{
JSONArray jArray = new JSONArray(result);
TableLayout tv=(TableLayout) findViewById(R.id.table);
tv.removeAllViewsInLayout();
int flag=1;
for(int i=-1;i<jArray.length()-1;i++)
{
TableRow tr=new TableRow(MainActivity.this);
tr.setLayoutParams(new LayoutParams(
LayoutParams.FILL_PARENT,
LayoutParams.WRAP_CONTENT));
if(flag==1)
{
TextView b6=new TextView(MainActivity.this);
b6.setText("ID");
b6.setTextColor(Color.BLUE);
b6.setTextSize(15);
tr.addView(b6);
TextView b19=new TextView(MainActivity.this);
b19.setPadding(10, 0, 0, 0);
b19.setTextSize(15);
b19.setText("Name");
b19.setTextColor(Color.BLUE);
tr.addView(b19);
TextView b29=new TextView(MainActivity.this);
b29.setPadding(10, 0, 0, 0);
b29.setText("no");
b29.setTextColor(Color.BLUE);
b29.setTextSize(15);
tr.addView(b29);
tv.addView(tr);
final View vline = new View(MainActivity.this);
vline.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.WRAP_CONTENT, 2));
vline.setBackgroundColor(Color.BLUE);
tv.addView(vline);
flag=0;
}
else
{
JSONObject json_data = jArray.getJSONObject(i);
Log.i("log_tag","id: "+json_data.getInt("f1")+
", Username: "+json_data.getString("f2")+
", No: "+json_data.getInt("f3"));
TextView b=new TextView(MainActivity.this);
String stime=String.valueOf(json_data.getInt("f1"));
b.setText(stime);
b.setTextColor(Color.RED);
b.setTextSize(15);
tr.addView(b);
TextView b1=new TextView(MainActivity.this);
b1.setPadding(10, 0, 0, 0);
b1.setTextSize(15);
String stime1=json_data.getString("f2");
b1.setText(stime1);
b1.setTextColor(Color.WHITE);
tr.addView(b1);
TextView b2=new TextView(MainActivity.this);
b2.setPadding(10, 0, 0, 0);
String stime2=String.valueOf(json_data.getInt("f3"));
b2.setText(stime2);
b2.setTextColor(Color.RED);
b2.setTextSize(15);
tr.addView(b2);
tv.addView(tr);
final View vline1 = new View(MainActivity.this);
vline1.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.WRAP_CONTENT, 1));
vline1.setBackgroundColor(Color.WHITE);
tv.addView(vline1);
}
}
}
catch(JSONException e)
{
Log.e("log_tag", "Error parsing data "+e.toString());
Toast.makeText(getApplicationContext(), "JsonArray fail", Toast.LENGTH_SHORT).show();
}
}
});
}
logcat的:
12-28 11:57:38.076: D/OpenGLRenderer(1681): Render dirty regions requested: true
12-28 11:57:38.080: D/(1681): HostConnection::get() New Host Connection established 0xa5d721c0, tid 1681
12-28 11:57:38.180: D/Atlas(1681): Validating map...
12-28 11:57:38.320: D/libEGL(1681): loaded /system/lib/egl/libEGL_genymotion.so
12-28 11:57:38.321: D/(1681): HostConnection::get() New Host Connection established 0xa5d72320, tid 1696
12-28 11:57:38.355: D/libEGL(1681): loaded /system/lib/egl/libGLESv1_CM_genymotion.so
12-28 11:57:38.374: D/libEGL(1681): loaded /system/lib/egl/libGLESv2_genymotion.so
12-28 11:57:38.391: I/OpenGLRenderer(1681): Initialized EGL, version 1.4
12-28 11:57:38.440: D/OpenGLRenderer(1681): Enabling debug mode 0
12-28 11:57:38.485: W/EGL_genymotion(1681): eglSurfaceAttrib not implemented
12-28 11:57:38.485: W/OpenGLRenderer(1681): Failed to set EGL_SWAP_BEHAVIOR on surface 0xa5d260c0, error=EGL_SUCCESS
12-28 11:57:48.394: E/log_tag(1681): Error in http connection android.os.NetworkOnMainThreadException
12-28 11:57:48.396: E/log_tag(1681): Error converting result java.lang.NullPointerException: lock == null
12-28 11:57:48.397: D/AndroidRuntime(1681): Shutting down VM
12-28 11:57:48.398: E/AndroidRuntime(1681): FATAL EXCEPTION: main
12-28 11:57:48.398: E/AndroidRuntime(1681): Process: com.dbselect.dbselect, PID: 1681
12-28 11:57:48.398: E/AndroidRuntime(1681): java.lang.NullPointerException: Attempt to invoke virtual method 'int java.lang.String.length()' on a null object reference
12-28 11:57:48.398: E/AndroidRuntime(1681): at org.json.JSONTokener.nextCleanInternal(JSONTokener.java:116)
12-28 11:57:48.398: E/AndroidRuntime(1681): at org.json.JSONTokener.nextValue(JSONTokener.java:94)
12-28 11:57:48.398: E/AndroidRuntime(1681): at org.json.JSONArray.<init>(JSONArray.java:92)
12-28 11:57:48.398: E/AndroidRuntime(1681): at org.json.JSONArray.<init>(JSONArray.java:108)
12-28 11:57:48.398: E/AndroidRuntime(1681): at com.dbselect.dbselect.MainActivity$1.onClick(MainActivity.java:87)
12-28 11:57:48.398: E/AndroidRuntime(1681): at android.view.View.performClick(View.java:4756)
12-28 11:57:48.398: E/AndroidRuntime(1681): at android.view.View$PerformClick.run(View.java:19749)
12-28 11:57:48.398: E/AndroidRuntime(1681): at android.os.Handler.handleCallback(Handler.java:739)
12-28 11:57:48.398: E/AndroidRuntime(1681): at android.os.Handler.dispatchMessage(Handler.java:95)
12-28 11:57:48.398: E/AndroidRuntime(1681): at android.os.Looper.loop(Looper.java:135)
12-28 11:57:48.398: E/AndroidRuntime(1681): at android.app.ActivityThread.main(ActivityThread.java:5221)
12-28 11:57:48.398: E/AndroidRuntime(1681): at java.lang.reflect.Method.invoke(Native Method)
12-28 11:57:48.398: E/AndroidRuntime(1681): at java.lang.reflect.Method.invoke(Method.java:372)
12-28 11:57:48.398: E/AndroidRuntime(1681): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
12-28 11:57:48.398: E/AndroidRuntime(1681): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)
activity_main.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="com.dbselect.dbselect.MainActivity" >
<Button
android:id="@+id/button1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Button" />
<ScrollView
android:layout_below="@+id/button1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:focusableInTouchMode="true"
android:focusable="true" >
<TableLayout
android:id="@+id/table"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:focusableInTouchMode="true"
android:focusable="true">
</TableLayout>
</ScrollView>
</RelativeLayout>