2012-11-04 34 views
1

的Java代碼 -的Android應用程序在表格佈局動態添加行給錯誤

import java.io.BufferedReader; 
import java.io.IOException; 
import java.io.InputStream; 
import java.io.InputStreamReader; 
import java.io.UnsupportedEncodingException; 
import java.util.ArrayList; 
import java.util.HashMap; 
import java.util.List; 
import org.apache.http.HttpEntity; 
import org.apache.http.HttpResponse; 
import org.apache.http.NameValuePair; 
import org.apache.http.client.ClientProtocolException; 
import org.apache.http.client.methods.HttpGet; 
import org.apache.http.client.utils.URLEncodedUtils; 
import org.apache.http.impl.client.DefaultHttpClient; 
import org.json.JSONArray; 
import org.json.JSONException; 
import org.json.JSONObject; 
import org.json.JSONStringer; 
import android.os.AsyncTask; 
import android.os.Bundle; 
import android.annotation.SuppressLint; 
import android.app.Activity; 
import android.app.ProgressDialog; 
import android.content.Intent; 
import android.graphics.Color; 
import android.util.Log; 
import android.view.Menu; 
import android.view.MenuItem; 
import android.view.View; 
import android.view.ViewGroup.LayoutParams; 
import android.widget.TableLayout; 
import android.widget.TableRow; 
import android.widget.TextView; 
import android.widget.Toast; 
import android.support.v4.app.NavUtils; 
public class DisplayStore extends Activity { 
// Progress Dialog 
private ProgressDialog pDialog; 
static InputStream is = null; 
static JSONObject jObj = null; 
static String json = ""; 
// Creating JSON Parser object 
//JSONParser jParser = new JSONParser(); 
ArrayList<HashMap<String, String>> ContriList; 
// url to get all products list 
private static String url_get_contri =  "http://10.0.2.2/annapurna_connect/GetContri.php"; 

// JSON Node names 
private static final String TAG_SUCCESS = "success"; 
/* private static final String TAG_PRODUCTS = "products"; 
private static final String TAG_PID = "pid"; 
private static final String TAG_NAME = "name";*/ 
// products JSONArray 
JSONArray products = null; 
@SuppressLint({ "NewApi", "NewApi" }) 
@Override 
public void onCreate(Bundle savedInstanceState) { 
super.onCreate(savedInstanceState); 
setContentView(R.layout.activity_display_store); 
getActionBar().setDisplayHomeAsUpEnabled(true); 
ContriList = new ArrayList<HashMap<String, String>>(); 
Toast.makeText(getApplicationContext(), url_get_contri,Toast.LENGTH_LONG).show(); 
new LoadContri().execute(); 
} 

/** 
* Background Async Task to Load all product by making HTTP Request 
* */ 
class LoadContri extends AsyncTask<String, String, String> { 
/** 
* Before starting background thread Show Progress Dialog 
* */ 
protected void onPreExecute() { 
super.onPreExecute(); 
Toast.makeText(getApplicationContext(), "here i am",Toast.LENGTH_LONG).show(); 
pDialog = new ProgressDialog(DisplayStore.this); 
pDialog.setMessage("Loading Information. Please wait..."); 
pDialog.setIndeterminate(false); 
pDialog.setCancelable(false); 
pDialog.show(); 
} 
/** 
* getting All products from url 
* */ 
@SuppressWarnings("deprecation") 
protected String doInBackground(String... args) { 
// Building Parameters 
try { 

List<NameValuePair> params = new ArrayList<NameValuePair>(); 
DefaultHttpClient httpClient = new DefaultHttpClient(); 
String paramString = URLEncodedUtils.format(params, "utf-8"); 
url_get_contri += "?" + paramString; 
HttpGet httpGet = new HttpGet(url_get_contri); 
HttpResponse httpResponse = httpClient.execute(httpGet); 
HttpEntity httpEntity = httpResponse.getEntity(); 
is = httpEntity.getContent(); 

} 
catch (UnsupportedEncodingException e) { 
e.printStackTrace(); 
} catch (ClientProtocolException e) { 
e.printStackTrace(); 
} catch (IOException e) { 
e.printStackTrace(); 
} 
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(); 
json = sb.toString(); 
} catch (Exception e) { 
Log.e("Buffer Error", "Error converting result " + e.toString()); 
} 
/*catch (UnsupportedEncodingException e) { 
e.printStackTrace(); 
} catch (ClientProtocolException e) { 
e.printStackTrace(); 
} catch (IOException e) { 
e.printStackTrace(); 
}*/ 

// Log.d("checking",sb);  
// Check your log cat for JSON reponse 
Log.d("All Products: ",json); 
String category,type,city,state,abc; 
String test=""; 
int serves; 
try 
{ 
//String json=sb.toString(); 
// String result=json_decode(json); 
// String test=""; 
JSONArray jArray = new JSONArray(json); 
test+="here"; 
JSONObject json_data=null; 
test+="here1"; 
TableLayout tv=(TableLayout)findViewById(R.id.Table); 
tv.removeAllViewsInLayout(); 
int flag=1; 
test+="here2"; 

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

Log.d("i",String.valueOf(i)); 
TableRow tr=new TableRow(getApplicationContext()); 
tr.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.WRAP_CONTENT)); 
test+="here4"; 
if(flag==1) 
// this will be executed once 
{ 
test+="here5"; 
TextView b3=new TextView(getApplicationContext()); 
b3.setText("Category"); 
b3.setTextColor(Color.BLUE); 
b3.setTextSize(15); 
tr.addView(b3); 

TextView b4=new TextView(getApplicationContext()); 
b4.setPadding(10, 0, 0, 0); 
b4.setTextSize(15); 
b4.setText("Type"); 
b4.setTextColor(Color.BLUE); 
tr.addView(b4); 
test+="after1col"; 
TextView b5=new TextView(getApplicationContext()); 
b5.setPadding(10, 0, 0, 0); 
b5.setText("Serves"); 
b5.setTextColor(Color.BLUE); 
b5.setTextSize(15); 
tr.addView(b5); 

TextView b6=new TextView(getApplicationContext()); 
b6.setText("City"); 
b6.setTextColor(Color.BLUE); 
b6.setTextSize(15); 
tr.addView(b6); 
TextView b7=new TextView(getApplicationContext()); 
b7.setText("State"); 
b7.setTextColor(Color.BLUE); 
b7.setTextSize(15); 
tr.addView(b7); 
test+="heresettext"; 
tv.addView(tr); 
test+="aftertv"; 
final View vline = new View(getApplicationContext()); 
test+="aftervline"; 
vline.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.FILL_PARENT,LayoutParams.WRAP_CONTENT)); 
test+="afterlayout"; 
vline.setBackgroundColor(Color.BLUE); 
test+="aftersetback"; 
tv.addView(vline); // add line below heading 
//tv.addView(tr,new TableLayout.LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.WRAP_CONTENT)); 
test+="addview"; 
flag=0; 
test+="aftersettext"; 
Log.d("check","here"); 

} 
else 
{ Log.d("check","here"); 

test+="here6"; 
json_data = jArray.getJSONObject(i); 
test+="here7"; 

TextView b=new TextView(getApplicationContext()); 
category=String.valueOf(json_data.getString("Category")); 
b.setText(category); 
b.setTextColor(Color.RED); 
b.setTextSize(15); 
tr.addView(b); 
TextView b1=new TextView(getApplicationContext()); 
b1.setPadding(10, 0, 0, 0); 
b1.setTextSize(15); 
type=json_data.getString("Type"); 
b1.setText(type); 
b1.setTextColor(Color.WHITE); 
tr.addView(b1); 
test+="after1col"; 

TextView b2=new TextView(getApplicationContext()); 
b2.setPadding(10, 0, 0, 0); 
serves=json_data.getInt("Serves"); 
test+="getInt"; 
abc=String.valueOf(serves); 
test+=abc; 
b2.setText(abc); 
test+="after2col"; 
b2.setTextColor(Color.RED); 
b2.setTextSize(15); 
tr.addView(b2); 
TextView b3=new TextView(getApplicationContext()); 
b3.setPadding(10, 0, 0, 0); 
b3.setTextSize(15); 
city=json_data.getString("City"); 
b3.setText(type); 
b3.setTextColor(Color.WHITE); 
tr.addView(b3); 
test+="after23col"; 
TextView b4=new TextView(getApplicationContext()); 
b4.setPadding(10, 0, 0, 0); 
b4.setTextSize(15); 
state=json_data.getString("State"); 
b4.setText(type); 
b4.setTextColor(Color.WHITE); 
tr.addView(b4); 
tv.addView(tr); 

final View vline1 = new View(getApplicationContext()); 
vline1.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.FILL_PARENT,LayoutParams.WRAP_CONTENT)); 
vline1.setBackgroundColor(Color.WHITE); 
tv.addView(vline1); // add line below each row 
// tv.addView(tr,new TableLayout.LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.WRAP_CONTENT)); 
Log.d("check","here2"); 
} 
} 

} catch (Exception e) { 
Log.d("in JSon Exception",test); 
e.printStackTrace(); 
} 
return null; 
} 
/** 
* After completing background task Dismiss the progress dialog 
* **/ 
protected void onPostExecute(String file_url) { 
// dismiss the dialog after getting all products 
Log.d("in post","wats the problem"); 
pDialog.dismiss(); 
} 
} 
@Override 
public boolean onCreateOptionsMenu(Menu menu) { 
getMenuInflater().inflate(R.menu.activity_display_store, menu); 
return true; 
} 

@Override 
public boolean onOptionsItemSelected(MenuItem item) { 
switch (item.getItemId()) { 
case android.R.id.home: 
NavUtils.navigateUpFromSameTask(this); 
return true; 
} 
return super.onOptionsItemSelected(item); 
} 


} 

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" > 
<RelativeLayout 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
android:background="@drawable/backimg" > 
<TableLayout 
xmlns:android="http://schemas.android.com/apk/res/android" 
android:id="@+id/Table" 
android:layout_width="match_parent" 
android:layout_height="50dp" 
android:layout_alignParentRight="true" 
android:background="#ffffff" 
android:shrinkColumns="*" 
android:stretchColumns="*" > 
</TableLayout> 
</RelativeLayout> 
<TextView 
android:id="@+id/textView2" 
android:layout_width="wrap_content" 
android:layout_height="wrap_content" 
android:layout_alignParentBottom="true" 
android:layout_alignParentLeft="true" 
android:layout_marginBottom="49dp" 
android:layout_toLeftOf="@+id/textView1" 
android:text="Access Through OrgID: " 
android:textAppearance="?android:attr/textAppearanceMedium" 
android:textStyle="bold|italic" /> 
<EditText 
android:id="@+id/editText1" 
android:layout_width="wrap_content" 
android:layout_height="wrap_content" 
android:layout_alignBaseline="@+id/textView2" 
android:layout_alignBottom="@+id/textView2" 
android:layout_toRightOf="@+id/textView2" 
android:ems="10" > 
<requestFocus /> 
</EditText> 
<Button 
android:id="@+id/button1" 
style="?android:attr/buttonStyleSmall" 
android:layout_width="wrap_content" 
android:layout_height="wrap_content" 
android:layout_alignParentBottom="true" 
android:layout_alignRight="@+id/textView1" 
android:text="Go" 
android:textStyle="bold|italic" /> 
</RelativeLayout> 

錯誤MESSAGE-

11-04 15:48:21.687: E/Trace(695): error opening trace file: No such file or directory (2) 
11-04 15:48:23.498: D/dalvikvm(695): GC_FOR_ALLOC freed 40K, 3% free 8013K/8259K, paused 94ms, total 98ms 
11-04 15:48:23.530: I/dalvikvm-heap(695): Grow heap (frag case) to 8.625MB for 787816-byte allocation 
11-04 15:48:23.857: D/dalvikvm(695): GC_CONCURRENT freed <1K, 4% free 8782K/9095K, paused 67ms+191ms, total 332ms 
11-04 15:48:23.857: D/dalvikvm(695): WAIT_FOR_CONCURRENT_GC blocked 2ms 
11-04 15:48:23.918: D/dalvikvm(695): GC_FOR_ALLOC freed 0K, 4% free 8782K/9095K, paused 46ms, total 46ms 
11-04 15:48:23.948: I/dalvikvm-heap(695): Grow heap (frag case) to 10.317MB for 1774384-byte allocation 
11-04 15:48:24.148: D/dalvikvm(695): GC_CONCURRENT freed 0K, 4% free 10515K/10887K, paused 109ms+28ms, total 203ms 
11-04 15:48:24.428: D/dalvikvm(695): GC_FOR_ALLOC freed 776K, 11% free 9778K/10887K, paused 66ms, total 68ms 
11-04 15:48:24.458: I/dalvikvm-heap(695): Grow heap (frag case) to 10.933MB for 1400016-byte allocation 
11-04 15:48:24.678: D/dalvikvm(695): GC_CONCURRENT freed <1K, 10% free 11145K/12295K, paused 111ms+21ms, total 219ms 
11-04 15:48:25.450: I/Choreographer(695): Skipped 85 frames! The application may be doing too much work on its main thread. 
11-04 15:48:25.568: D/gralloc_goldfish(695): Emulator without GPU emulation detected. 
11-04 15:48:31.867: D/dalvikvm(695): GC_FOR_ALLOC freed 12K, 9% free 11227K/12295K, paused 39ms, total 42ms 
11-04 15:48:31.887: I/dalvikvm-heap(695): Grow heap (frag case) to 12.348MB for 1400016-byte allocation 
11-04 15:48:32.067: D/dalvikvm(695): GC_CONCURRENT freed 2K, 9% free 12592K/13703K, paused 79ms+19ms, total 173ms 
11-04 15:48:32.308: D/dalvikvm(695): GC_FOR_ALLOC freed <1K, 2% free 13502K/13703K, paused 46ms, total 48ms 
11-04 15:48:32.347: I/dalvikvm-heap(695): Grow heap (frag case) to 15.902MB for 2797584-byte allocation 
11-04 15:48:32.537: D/dalvikvm(695): GC_CONCURRENT freed <1K, 2% free 16233K/16455K, paused 105ms+20ms, total 191ms 
11-04 15:48:36.737: I/Choreographer(695): Skipped 199 frames! The application may be doing too much work on its main thread. 
11-04 15:48:37.738: I/Choreographer(695): Skipped 246 frames! The application may be doing too much work on its main thread. 
11-04 15:48:38.327: I/Choreographer(695): Skipped 129 frames! The application may be doing too much work on its main thread. 
11-04 15:48:38.517: I/Choreographer(695): Skipped 41 frames! The application may be doing too much work on its main thread. 
11-04 15:48:38.677: I/Choreographer(695): Skipped 40 frames! The application may be doing too much work on its main thread. 
11-04 15:48:39.217: I/Choreographer(695): Skipped 31 frames! The application may be doing too much work on its main thread. 
11-04 15:48:39.658: I/Choreographer(695): Skipped 72 frames! The application may be doing too much work on its main thread. 
11-04 15:48:39.917: I/Choreographer(695): Skipped 66 frames! The application may be doing too much work on its main thread. 
11-04 15:48:40.178: I/Choreographer(695): Skipped 54 frames! The application may be doing too much work on its main thread. 
11-04 15:48:40.347: I/Choreographer(695): Skipped 33 frames! The application may be doing too much work on its main thread. 
11-04 15:48:40.667: I/Choreographer(695): Skipped 30 frames! The application may be doing too much work on its main thread. 
11-04 15:48:41.327: I/Choreographer(695): Skipped 31 frames! The application may be doing too much work on its main thread. 
11-04 15:48:41.837: I/Choreographer(695): Skipped 30 frames! The application may be doing too much work on its main thread. 
11-04 15:48:43.018: I/Choreographer(695): Skipped 44 frames! The application may be doing too much work on its main thread. 
11-04 15:48:43.178: I/Choreographer(695): Skipped 41 frames! The application may be doing too much work on its main thread. 
11-04 15:48:43.357: I/Choreographer(695): Skipped 45 frames! The application may be doing too much work on its main thread. 
11-04 15:48:43.578: I/Choreographer(695): Skipped 41 frames! The application may be doing too much work on its main thread. 
11-04 15:48:43.798: I/Choreographer(695): Skipped 56 frames! The application may be doing too much work on its main thread. 
11-04 15:48:44.638: I/Choreographer(695): Skipped 35 frames! The application may be doing too much work on its main thread. 
11-04 15:48:45.548: I/Choreographer(695): Skipped 31 frames! The application may be doing too much work on its main thread. 
11-04 15:48:45.870: D/All Products:(695): [{"ContriID":"100037","Category":"V","Type":"U","Serves":"5","City":"hhhh","State":"jjjg"}] 
11-04 15:48:46.179: D/i(695): -1 
11-04 15:48:46.368: I/Choreographer(695): Skipped 37 frames! The application may be doing too much work on its main thread. 
11-04 15:48:47.418: D/in JSon Exception(695): herehere1here2here4here5after1colheresettext 
11-04 15:48:48.058: W/System.err(695): android.view.ViewRootImpl$CalledFromWrongThreadException: Only the original thread that created a view hierarchy can touch its views. 
11-04 15:48:48.128: I/Choreographer(695): Skipped 32 frames! The application may be doing too much work on its main thread. 
11-04 15:48:48.388: W/System.err(695): at android.view.ViewRootImpl.checkThread(ViewRootImpl.java:4607) 
11-04 15:48:48.459: W/System.err(695): at android.view.ViewRootImpl.requestLayout(ViewRootImpl.java:835) 
11-04 15:48:48.518: W/System.err(695): at android.view.View.requestLayout(View.java:15129) 
11-04 15:48:48.578: W/System.err(695): at android.view.View.requestLayout(View.java:15129) 
11-04 15:48:48.648: W/System.err(695): at android.view.View.requestLayout(View.java:15129) 
11-04 15:48:48.698: W/System.err(695): at android.view.View.requestLayout(View.java:15129) 
11-04 15:48:48.828: W/System.err(695): at android.widget.RelativeLayout.requestLayout(RelativeLayout.java:292) 
11-04 15:48:48.868: W/System.err(695): at android.view.View.requestLayout(View.java:15129) 
11-04 15:48:48.938: W/System.err(695): at android.widget.RelativeLayout.requestLayout(RelativeLayout.java:292) 
11-04 15:48:49.008: W/System.err(695): at android.view.View.requestLayout(View.java:15129) 
11-04 15:48:49.248: W/System.err(695): at android.widget.TableLayout.requestLayout(TableLayout.java:226) 
11-04 15:48:49.309: I/Choreographer(695): Skipped 32 frames! The application may be doing too much work on its main thread. 
11-04 15:48:49.358: W/System.err(695): at android.view.ViewGroup.addView(ViewGroup.java:3247) 
11-04 15:48:49.448: W/System.err(695): at android.widget.TableLayout.addView(TableLayout.java:425) 
11-04 15:48:49.579: W/System.err(695): at android.view.ViewGroup.addView(ViewGroup.java:3225) 
11-04 15:48:49.608: W/System.err(695): at android.widget.TableLayout.addView(TableLayout.java:416) 
11-04 15:48:49.758: W/System.err(695): at com.example.annapurna.DisplayStore$LoadContri.doInBackground(DisplayStore.java:228) 
11-04 15:48:49.848: W/System.err(695): at com.example.annapurna.DisplayStore$LoadContri.doInBackground(DisplayStore.java:1) 
11-04 15:48:49.898: I/Choreographer(695): Skipped 34 frames! The application may be doing too much work on its main thread. 
11-04 15:48:50.038: W/System.err(695): at android.os.AsyncTask$2.call(AsyncTask.java:287) 
11-04 15:48:50.098: W/System.err(695): at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305) 
11-04 15:48:50.218: W/System.err(695): at java.util.concurrent.FutureTask.run(FutureTask.java:137) 
11-04 15:48:50.468: W/System.err(695): at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:230) 
11-04 15:48:50.499: W/System.err(695): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1076) 
11-04 15:48:50.660: W/System.err(695): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:569) 
11-04 15:48:50.798: W/System.err(695): at java.lang.Thread.run(Thread.java:856) 
11-04 15:48:50.968: D/in post(695): wats the problem 
11-04 15:48:55.607: I/Choreographer(695): Skipped 36 frames! The application may be doing too much work on its main thread. 
11-04 15:51:22.140: I/Choreographer(695): Skipped 46 frames! The application may be doing too much work on its main thread. 

我試圖獲取數據庫條目從一個MySQL數據庫並顯示在一個表中。 Php文件正常工作,並在logcat中顯示數據。但不會在UI端顯示,並且我無法找到該錯誤,所以請爲此提供幫助。

回答

2

現在來自您的CatLog,您正在重載UI線程。

這是可能的,雖然一切都在「工作」,它加載了太多及時莊園迴應。

你應該做的是使用遊標適配器,因爲需要從數據庫加載信息。

您例如,可以把1000點的觀點在屏幕上,當你只能看到他們的5-20?..它的無用功,CPU時間和內存。

這會導致性能極度降低。

查找到自定義/光標適配器。

p.s.沒有行縮進,很難閱讀你發佈的代碼。

+0

目前,按我們的數據庫中,就是隻檢索條目2-3但隨後也實在是不能夠在用戶界面diaplay他們。 – user29