这次崩溃的模式有点奇怪。有时它是可以的,但过了一段时间我尝试从一个活动导航到另一个活动它会发生(例如:从MainActivity到AnotherActivity),但有时它不会崩溃。有时,当我单击下一页或上一页选项时它也会崩溃,该选项显示数据库中的下一个7或前7个内容。有时甚至更奇怪的是它在我运行程序时崩溃了。 我已经使用过这个功能了:
@Override
public void onProgressUpdate(Integer... progress) {
imageAdapter.notifyDataSetChanged();
}
拜托,请帮助最需要我的F1,我是新手???
非常感谢!
logcat的:
11-20 18:34:01.183: E/AndroidRuntime(1746): FATAL EXCEPTION: main
11-20 18:34:01.183: E/AndroidRuntime(1746): java.lang.IllegalStateException: The content of the adapter has changed but ListView did not receive a notification. Make sure the content of your adapter is not modified from a background thread, but only from the UI thread. [in ListView(2131361798, class android.widget.ListView) with Adapter(class com.mysoft.saddis.MainActivity$ImageAdapter)]
11-20 18:34:01.183: E/AndroidRuntime(1746): at android.widget.ListView.layoutChildren(ListView.java:1545)
11-20 18:34:01.183: E/AndroidRuntime(1746): at android.widget.AbsListView.onLayout(AbsListView.java:2012)
11-20 18:34:01.183: E/AndroidRuntime(1746): at android.view.View.layout(View.java:14289)
11-20 18:34:01.183: E/AndroidRuntime(1746): at android.view.ViewGroup.layout(ViewGroup.java:4562)
11-20 18:34:01.183: E/AndroidRuntime(1746): at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1671)
11-20 18:34:01.183: E/AndroidRuntime(1746): at android.widget.LinearLayout.layoutHorizontal(LinearLayout.java:1660)
11-20 18:34:01.183: E/AndroidRuntime(1746): at android.widget.LinearLayout.onLayout(LinearLayout.java:1436)
11-20 18:34:01.183: E/AndroidRuntime(1746): at android.view.View.layout(View.java:14289)
11-20 18:34:01.183: E/AndroidRuntime(1746): at android.view.ViewGroup.layout(ViewGroup.java:4562)
11-20 18:34:01.183: E/AndroidRuntime(1746): at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1671)
11-20 18:34:01.183: E/AndroidRuntime(1746): at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1525)
11-20 18:34:01.183: E/AndroidRuntime(1746): at android.widget.TableLayout.onLayout(TableLayout.java:448)
11-20 18:34:01.183: E/AndroidRuntime(1746): at android.view.View.layout(View.java:14289)
11-20 18:34:01.183: E/AndroidRuntime(1746): at android.view.ViewGroup.layout(ViewGroup.java:4562)
11-20 18:34:01.183: E/AndroidRuntime(1746): at android.widget.FrameLayout.onLayout(FrameLayout.java:448)
11-20 18:34:01.183: E/AndroidRuntime(1746): at android.view.View.layout(View.java:14289)
11-20 18:34:01.183: E/AndroidRuntime(1746): at android.view.ViewGroup.layout(ViewGroup.java:4562)
11-20 18:34:01.183: E/AndroidRuntime(1746): at com.android.internal.widget.ActionBarOverlayLayout.onLayout(ActionBarOverlayLayout.java:349)
11-20 18:34:01.183: E/AndroidRuntime(1746): at android.view.View.layout(View.java:14289)
11-20 18:34:01.183: E/AndroidRuntime(1746): at android.view.ViewGroup.layout(ViewGroup.java:4562)
11-20 18:34:01.183: E/AndroidRuntime(1746): at android.widget.FrameLayout.onLayout(FrameLayout.java:448)
11-20 18:34:01.183: E/AndroidRuntime(1746): at android.view.View.layout(View.java:14289)
11-20 18:34:01.183: E/AndroidRuntime(1746): at android.view.ViewGroup.layout(ViewGroup.java:4562)
11-20 18:34:01.183: E/AndroidRuntime(1746): at android.view.ViewRootImpl.performLayout(ViewRootImpl.java:1976)
11-20 18:34:01.183: E/AndroidRuntime(1746): at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1730)
11-20 18:34:01.183: E/AndroidRuntime(1746): at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1004)
11-20 18:34:01.183: E/AndroidRuntime(1746): at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:5481)
11-20 18:34:01.183: E/AndroidRuntime(1746): at android.view.Choreographer$CallbackRecord.run(Choreographer.java:749)
11-20 18:34:01.183: E/AndroidRuntime(1746): at android.view.Choreographer.doCallbacks(Choreographer.java:562)
11-20 18:34:01.183: E/AndroidRuntime(1746): at android.view.Choreographer.doFrame(Choreographer.java:532)
11-20 18:34:01.183: E/AndroidRuntime(1746): at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:735)
11-20 18:34:01.183: E/AndroidRuntime(1746): at android.os.Handler.handleCallback(Handler.java:730)
11-20 18:34:01.183: E/AndroidRuntime(1746): at android.os.Handler.dispatchMessage(Handler.java:92)
11-20 18:34:01.183: E/AndroidRuntime(1746): at android.os.Looper.loop(Looper.java:137)
11-20 18:34:01.183: E/AndroidRuntime(1746): at android.app.ActivityThread.main(ActivityThread.java:5103)
11-20 18:34:01.183: E/AndroidRuntime(1746): at java.lang.reflect.Method.invokeNative(Native Method)
11-20 18:34:01.183: E/AndroidRuntime(1746): at java.lang.reflect.Method.invoke(Method.java:525)
11-20 18:34:01.183: E/AndroidRuntime(1746): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
11-20 18:34:01.183: E/AndroidRuntime(1746): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
11-20 18:34:01.183: E/AndroidRuntime(1746): at dalvik.system.NativeStart.main(Native Method)
MainActivity.java
package com.mysoft.saddis;
//all the necessary imports are imported
public class MainActivity extends Activity {
private ListView lstView;
private ImageAdapter imageAdapter;
public int currentPage = 1;
public int TotalPage = 0;
public Button btnNext;
public Button btnPre;
public boolean orientation;
boolean verOk = Splash.version;
public static String urlPageNumHolder;
public ProgressDialog dialog;
ArrayList<HashMap<String, Object>> MyArrList = new ArrayList<HashMap<String, Object>>();
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
getActionBar().setHomeButtonEnabled(true);
// Bets orientation
onConfigurationChanged(null);
// ListView and imageAdapter
lstView = (ListView) findViewById(R.id.listView1);
lstView.setClipToPadding(false);
imageAdapter = new ImageAdapter(getApplicationContext());
lstView.setAdapter(imageAdapter);
// Next
btnNext = (Button) findViewById(R.id.btnNext);
// Perform action on click
btnNext.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
currentPage = currentPage + 1;
ShowData();
}
});
// Previous
btnPre = (Button) findViewById(R.id.btnPre);
// Perform action on click
btnPre.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
currentPage = currentPage - 1;
ShowData();
}
});
// Show first load
ShowData();
// OnClick
lstView.setOnItemClickListener(new OnItemClickListener() {
@SuppressLint("NewApi")
public void onItemClick(AdapterView<?> parent, View v,
int position, long id) {
loadPage(position);
if(verOk==true){
Intent subActivity = new Intent(MainActivity.this,
Webber.class);
Bundle translateBundle = ActivityOptions
.makeCustomAnimation(MainActivity.this,
R.anim.slide_in_left, R.anim.slide_out_left)
.toBundle();
startActivity(subActivity, translateBundle);
} else{
Intent subActivity = new Intent("com.mysoft.saddis.WEBBER");
startActivity(subActivity);
}
}
});
Log.i(TAG, "onCreate");
}
public void ShowData() {
btnNext.setEnabled(false);
btnPre.setEnabled(false);
new LoadContentFromServer().execute();
if (orientation == false)
dialog = ProgressDialog
.show(this, "ዜና", "ትንሽ ይታገሱ...", true, false);
else
dialog = ProgressDialog.show(this, "News", "Loading...", true,
false);
}
public void loadPage(int position) {
// Hold Detail's URL
urlPageNumHolder = MyArrList.get(position).get("id").toString();
}
@Override
public void onConfigurationChanged(Configuration newConfig) {
Configuration c = getResources().getConfiguration();
if (c.orientation == Configuration.ORIENTATION_PORTRAIT) {
orientation = true; // portrait
} else if (c.orientation == Configuration.ORIENTATION_LANDSCAPE) {
orientation = false; // landscape
}
}
class LoadContentFromServer extends AsyncTask<Object, Integer, Object> {
@Override
protected Object doInBackground(Object... params) {
String url = "http://10.0.2.2/android/saddis/mobile/news/getNews.php";
JSONArray data;
try {
data = new JSONArray(getJSONUrl(url));
MyArrList = new ArrayList<HashMap<String, Object>>();
HashMap<String, Object> map;
int displayPerPage = 7; // Per Page
int TotalRows = data.length();
int indexRowStart = ((displayPerPage * currentPage) - displayPerPage);
if (TotalRows <= displayPerPage) {
TotalPage = 1;
} else if ((TotalRows % displayPerPage) == 0) {
TotalPage = (TotalRows / displayPerPage);
} else {
TotalPage = (TotalRows / displayPerPage) + 1;
TotalPage = (int) TotalPage;
}
int indexRowEnd = displayPerPage * currentPage;
if (indexRowEnd > TotalRows) {
indexRowEnd = TotalRows;
}
String imgPath;
for (int i = indexRowStart; i < indexRowEnd; i++) {
JSONObject c = data.getJSONObject(i);
map = new HashMap<String, Object>();
map.put("id", (String) c.getString("id"));
map.put("title_en", (String) c.getString("title_en"));
map.put("description_en", (String) c.getString("description_en"));
map.put("title_am", (String) c.getString("title_am"));
map.put("description_am", (String) c.getString("description_am"));
map.put("posteddate", (String) c.getString("posteddate"));
// Thumbnail Get ImageBitmap To Object
imgPath = "http://10.0.2.2/android/saddis/addis_img_gallery/news_thumb_"+c.getString("id")+".png";
map.put("imagepath", imgPath);
Bitmap newBitmap = loadBitmap(imgPath);
map.put("ImagePathBitmap", newBitmap);
MyArrList.add(map);
publishProgress(i);
}
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}
@Override
public void onProgressUpdate(Integer... progress) {
imageAdapter.notifyDataSetChanged();
}
@Override
protected void onPostExecute(Object result) {
// Disabled Button Next
if (currentPage >= TotalPage) {
btnNext.setEnabled(false);
} else {
btnNext.setEnabled(true);
}
// Disabled Button Previous
if (currentPage <= 1) {
btnPre.setEnabled(false);
} else {
btnPre.setEnabled(true);
}
// dismiss the progress dialog
if (dialog != null)
dialog.dismiss();
}
}
class ImageAdapter extends BaseAdapter {
private Context mContext;
public ImageAdapter(Context context) {
mContext = context;
}
public int getCount() {
return MyArrList.size();
}
public Object getItem(int position) {
return MyArrList.get(position);
}
public long getItemId(int position) {
return position;
}
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
LayoutInflater inflater = (LayoutInflater) mContext
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
if (convertView == null) {
convertView = inflater.inflate(R.layout.activity_column, null);
}
// ImagePath
ImageView imageView = (ImageView) convertView
.findViewById(R.id.ColImagePath);
imageView.getLayoutParams().height = 150;
imageView.getLayoutParams().width = 150;
imageView.setPadding(5, 5, 5, 5);
imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
try {
imageView.setImageBitmap((Bitmap) MyArrList.get(position).get(
"ImagePathBitmap"));
} catch (Exception e) {
imageView
.setImageResource(android.R.drawable.ic_menu_report_image);
}
//String qen = simpleDateFormat.format(MyArrList.get(position).get("posteddate"));
// ID, Title, Desc and DateTime
TextView txtTitle = (TextView) convertView
.findViewById(R.id.title);
txtTitle.setPadding(10, 0, 0, 0);
TextView txtDesc = (TextView) convertView
.findViewById(R.id.description);
txtDesc.setPadding(10, 0, 0, 0);
TextView txtDateTime = (TextView) convertView.findViewById(R.id.dateTime);
//txtDateTime.setText(MyArrList.get(position).get("posteddate").toString());
txtDateTime.setText(MyArrList.get(position).get("posteddate").toString());
if (orientation == false) {
txtTitle.setText(MyArrList.get(position).get("title_am").toString());
txtDesc.setText(MyArrList.get(position).get("description_am")
.toString());
}
else{
txtTitle.setText(MyArrList.get(position).get("title_en").toString());
txtDesc.setText(MyArrList.get(position).get("description_en")
.toString());
}
return convertView;
}
}
/*** Get JSON Code from URL ***/
public String getJSONUrl(String url) {
StringBuilder str = new StringBuilder();
HttpClient client = new DefaultHttpClient();
HttpGet httpGet = new HttpGet(url);
try {
HttpResponse response = client.execute(httpGet);
StatusLine statusLine = response.getStatusLine();
int statusCode = statusLine.getStatusCode();
if (statusCode == 200) { // Download OK
HttpEntity entity = response.getEntity();
InputStream content = entity.getContent();
BufferedReader reader = new BufferedReader(
new InputStreamReader(content));
String line;
while ((line = reader.readLine()) != null) {
str.append(line);
}
} else {
Log.e("Log", "Failed to download file..");
}
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return str.toString();
}
/***** Get Image Resource from URL (Start) *****/
private static final String TAG = "Image";
private static final int IO_BUFFER_SIZE = 4 * 1024;
public static Bitmap loadBitmap(String url) {
Bitmap bitmap = null;
InputStream in = null;
BufferedOutputStream out = null;
try {
in = new BufferedInputStream(new URL(url).openStream(),
IO_BUFFER_SIZE);
final ByteArrayOutputStream dataStream = new ByteArrayOutputStream();
out = new BufferedOutputStream(dataStream, IO_BUFFER_SIZE);
copy(in, out);
out.flush();
final byte[] data = dataStream.toByteArray();
BitmapFactory.Options options = new BitmapFactory.Options();
// options.inSampleSize = 1;
bitmap = BitmapFactory.decodeByteArray(data, 0, data.length,
options);
} catch (IOException e) {
Log.e(TAG, "Could not load Bitmap from: " + url);
} finally {
closeStream(in);
closeStream(out);
}
return bitmap;
}
private static void closeStream(Closeable stream) {
if (stream != null) {
try {
stream.close();
} catch (IOException e) {
android.util.Log.e(TAG, "Could not close stream", e);
}
}
}
private static void copy(InputStream in, OutputStream out)
throws IOException {
byte[] b = new byte[IO_BUFFER_SIZE];
int read;
while ((read = in.read(b)) != -1) {
out.write(b, 0, read);
}
}
/***** Get Image Resource from URL (End) *****/
@Override
public void finish() {
super.finish();
if(verOk==true){
overridePendingTransition(R.anim.slide_in_right, R.anim.slide_out_right);
}
}
@Override
protected void onSaveInstanceState(Bundle outState) {
// TODO Auto-generated method stub
super.onSaveInstanceState(outState);
// webview.saveState(outState);
Log.i(TAG, "onSaveInstanceState");
}
@Override
protected void onRestoreInstanceState(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onRestoreInstanceState(savedInstanceState);
// webview.restoreState(savedInstanceState);
// lstView.restoreState(savedInstanceState);
Log.i(TAG, "onRestoreInstanceState");
}
}