我的应用程序在某些设备上崩溃,导致JAVA.lang.outofmemory
错误,而所有数据都是从在线服务器上的数据库中解析出来的。尽管图像缩小(MAX.SIZE 20 Kb),但它会出现BITMAP Out of Memory Error。我已经为活动附加了崩溃日志和xml。
它崩溃的设备很少包括Samsung Note 3,Xiaomi,Mi4
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.humanfox.klisfer.humanfox/com.humanfox.klisfer.humanfox.caps1}: android.view.InflateException: Binary XML file line #36: Error inflating class <unknown>
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2201)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2250)
at android.app.ActivityThread.access$900(ActivityThread.java:141)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1213)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5072)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:609)
at dalvik.system.NativeStart.main(Native Method)
Caused by: android.view.InflateException: Binary XML file line #36: Error inflating class <unknown>
at android.view.LayoutInflater.createView(LayoutInflater.java:623)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:699)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:758)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:761)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:761)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:761)
at android.view.LayoutInflater.inflate(LayoutInflater.java:495)
at android.view.LayoutInflater.inflate(LayoutInflater.java:400)
at android.view.LayoutInflater.inflate(LayoutInflater.java:353)
at android.support.v7.app.AppCompatDelegateImplV7.setContentView(AppCompatDelegateImplV7.java:256)
at android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:109)
at com.humanfox.klisfer.humanfox.caps1.onCreate(caps1.java:75)
at android.app.Activity.performCreate(Activity.java:5246)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2165)
... 11 more
Caused by: java.lang.reflect.InvocationTargetException
at java.lang.reflect.Constructor.constructNative(Native Method)
at java.lang.reflect.Constructor.newInstance(Constructor.java:423)
at android.view.LayoutInflater.createView(LayoutInflater.java:597)
... 25 more
Caused by: java.lang.OutOfMemoryError
at android.graphics.BitmapFactory.nativeDecodeAsset(Native Method)
at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:601)
at android.graphics.BitmapFactory.decodeResourceStream(BitmapFactory.java:434)
at android.graphics.drawable.Drawable.createFromResourceStream(Drawable.java:840)
at
android.content.res.Resources.createFromResourceStream(Resources.java:2513)
at android.content.res.Resources.loadDrawable(Resources.java:2165)
at android.content.res.MiuiResources.loadDrawable(MiuiResources.java:387)
at android.content.res.Resources.loadDrawable(Resources.java:2072)
at android.content.res.TypedArray.getDrawable(TypedArray.java:602)
at android.widget.ImageView.<init>(ImageView.java:129)
at com.android.volley.toolbox.NetworkImageView.<init>(NetworkImageView.java:61)
at com.android.volley.toolbox.NetworkImageView.<init>(NetworkImageView.java:57)
... 28 more
Activity.java
public class caps1 extends ActionBarActivity {
CollapsingToolbarLayout collapsingToolbarLayout;
ImageView collapse;
public RequestQueue requestQueue,requestQueue1,requestQueue2;
Long pos;
private static String url;
String url2= "http://www.humanfox.com/capsule/recommend.php";
Context context;
Capsule caps;
ProgressDialog pdialog;
ImageView headshot;
NetworkImageView recom3,recom2,recom1,headerimage;
TextView pill1,pill2,pill3,pill4,pill5,pill6;
Snackbar snackbar;
ImageLoader imageloader,imageloader2;
NetworkImageView img1,img2,img3,img4,img5,img6;
TextView icon1,icon2,icon3,icon4,text44,text55;
TextView text444, text555, text666,text6666,text66666,text666666, editorname,editorprof,headernam,headerprof;
TextView future,quest,beauty, game,que1,ans1,que2,ans2,que3,ans3,que4,ans4,que5,ans5,que6,ans6,que7,ans7,que8,ans8,que9,ans9,que10,ans10,que11,ans11,que12,ans12,quotes,write,reader,recommend,quotebys;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_caps1);
setSupportActionBar((Toolbar) findViewById(R.id.toolbar1));
pdialog = new ProgressDialog(this);
pdialog.setMessage("Loading");
pdialog.setCancelable(false);
pos= getIntent().getLongExtra("posi",0);
Log.d("Opened item id", " "+pos);
url ="http://www.humanfox.com/capsule/feed.php?id=";
collapsingToolbarLayout = (CollapsingToolbarLayout) findViewById(R.id.collapsing_toolbar1);
collapsingToolbarLayout.setTitle("Capsule");
collapsingToolbarLayout.setExpandedTitleColor(getResources().getColor(android.R.color.transparent));
//setPalette();
snackbar = Snackbar
.make(collapsingToolbarLayout, "No internet connection!", Snackbar.LENGTH_LONG)
.setAction("RETRY", new View.OnClickListener() {
@Override
public void onClick(View view) {
}
});
img1= (NetworkImageView)findViewById(R.id.image1);
img2= (NetworkImageView)findViewById(R.id.image2);
img3= (NetworkImageView)findViewById(R.id.image3);
img4= (NetworkImageView)findViewById(R.id.image4);
img5= (NetworkImageView)findViewById(R.id.image5);
img6= (NetworkImageView)findViewById(R.id.image6);
headerimage= (NetworkImageView)findViewById(R.id.collapse);
text44=(TextView)findViewById(R.id.text44);
text55=(TextView)findViewById(R.id.text55);
icon1= (TextView)findViewById(R.id.icon1);
icon2= (TextView)findViewById(R.id.icon2);
icon3= (TextView)findViewById(R.id.icon3);
icon4= (TextView)findViewById(R.id.icon4);
quotebys = (TextView) findViewById(R.id.quoteby);
recommend = (TextView) findViewById(R.id.recommend);
headshot = (ImageView) findViewById(R.id.headsh1);
pill1=(TextView)findViewById(R.id.pill1);
pill2=(TextView)findViewById(R.id.pill2);
pill3=(TextView)findViewById(R.id.pill3);
pill4=(TextView)findViewById(R.id.pill4);
pill5=(TextView)findViewById(R.id.pill5);
pill6=(TextView)findViewById(R.id.pill6);
recom1 =(NetworkImageView)findViewById(R.id.recom1);
recom2 =(NetworkImageView)findViewById(R.id.recom2);
recom3 =(NetworkImageView)findViewById(R.id.recom3);
editorname=(TextView)findViewById(R.id.writtenby);
editorprof=(TextView)findViewById(R.id.reader);
headernam=(TextView)findViewById(R.id.headername);
headerprof=(TextView)findViewById(R.id.headerprofession);
text444= (TextView)findViewById(R.id.text444);
text555= (TextView)findViewById(R.id.text555);
text666= (TextView)findViewById(R.id.text666);
text6666= (TextView)findViewById(R.id.text6666);
text66666= (TextView)findViewById(R.id.text66666);
text666666= (TextView)findViewById(R.id.text666666);
quotes = (TextView) findViewById(R.id.quote);
que1 = (TextView) findViewById(R.id.que1);
ans1 = (TextView) findViewById(R.id.ans1);
que2 = (TextView) findViewById(R.id.que2);
ans2 = (TextView) findViewById(R.id.ans2);
que3 = (TextView) findViewById(R.id.que3);
ans3 = (TextView) findViewById(R.id.ans3);
que4 = (TextView) findViewById(R.id.que4);
ans4 = (TextView) findViewById(R.id.ans4);
que5 = (TextView) findViewById(R.id.que5);
ans5 = (TextView) findViewById(R.id.ans5);
que6 = (TextView) findViewById(R.id.que6);
ans6 = (TextView) findViewById(R.id.ans6);
que7 = (TextView) findViewById(R.id.que7);
ans7 = (TextView) findViewById(R.id.ans7);
que8 = (TextView) findViewById(R.id.que8);
ans8 = (TextView) findViewById(R.id.ans8);
que9 = (TextView) findViewById(R.id.que9);
ans9 = (TextView) findViewById(R.id.ans9);
que10 = (TextView) findViewById(R.id.que10);
ans10 = (TextView) findViewById(R.id.ans10);
Typeface icons = Typeface.createFromAsset(getAssets(), "fonts/fontawesome-webfont.ttf");
Typeface face = Typeface.createFromAsset(getAssets(), "fonts/OpenSans-Light.ttf");
Typeface face3 = Typeface.createFromAsset(getAssets(), "fonts/OpenSans-Semibold.ttf");
Typeface face2 = Typeface.createFromAsset(getAssets(), "fonts/OpenSans-Regular.ttf");
Typeface face4 = Typeface.createFromAsset(getAssets(), "fonts/OpenSans-Bold.ttf");
icon1.setTypeface(icons);
icon2.setTypeface(icons);
icon3.setTypeface(icons);
icon4.setTypeface(icons);
quotebys.setTypeface(face);
editorprof.setTypeface(face);
recommend.setTypeface(face3);
editorname.setTypeface(face);
quotes.setTypeface(face);
text444.setTypeface(face3);
text555.setTypeface(face3);
text666.setTypeface(face3);
text6666.setTypeface(face);
text66666.setTypeface(face);
text666666.setTypeface(face);
que1.setTypeface(face2);
ans1.setTypeface(face);
que2.setTypeface(face2);
ans2.setTypeface(face);
que3.setTypeface(face2);
ans3.setTypeface(face);
que4.setTypeface(face2);
ans4.setTypeface(face);
que5.setTypeface(face2);
ans5.setTypeface(face);
que6.setTypeface(face2);
ans6.setTypeface(face);
que7.setTypeface(face2);
ans7.setTypeface(face);
que8.setTypeface(face2);
ans8.setTypeface(face);
que9.setTypeface(face2);
ans9.setTypeface(face);
que10.setTypeface(face2);
ans10.setTypeface(face);
requestQueue = Volley.newRequestQueue(this);
fetchthedata(pos);
requestQueue1 = Volley.newRequestQueue(this);
JsonObjectRequest or = new JsonObjectRequest(Request.Method.GET, url2, null, new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject jsonObject) {
try {
JSONArray jsonArray1 = jsonObject.getJSONArray("result");
for (int i = 0; i < jsonArray1.length(); i++) {
JSONObject recom = jsonArray1.getJSONObject(i);
String pil1 = recom.getString("name1");
String pil2 = recom.getString("profession1");
String re1 = recom.getString("image1");
final long id1 =recom.getLong("id1");
Log.d("1st id is:",""+id1);
imageloader = MySingleton.getInstance(getApplicationContext()).getImageLoader();
re1.replace("\\"," ");
re1.trim();
String pil3 = recom.getString("name2");
String pil4 = recom.getString("profession2");
String re2 = recom.getString("image2");
final long id2 =recom.getLong("id2");
Log.d("2nd id is:",""+id2);
re2.replace("\\"," ");
re2.trim();
String pil5 = recom.getString("name3");
String pil6 = recom.getString("profession3");
String re3 = recom.getString("image3");
final long id3 =recom.getLong("id3");
Log.d("3rd id is:",""+id3);
re3.replace("\\"," ");
re3.trim();
pill1.setText(pil1);
pill2.setText(pil2);
pill3.setText(pil3);
pill4.setText(pil4);
pill5.setText(pil5);
pill6.setText(pil6);
//recom1.setImage(ImageSource.uri(re1));
//Picasso.with(getApplicationContext()).load(re1).into(recom1);
recom1.setImageUrl(re1,imageloader);
//imageloader.get(re1, ImageLoader.getImageListener(img6,R.drawable.ic_launcher,android.R.drawable.ic_dialog_alert));
recom1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent i = new Intent(caps1.this, caps2.class).putExtra("posi", id1);
startActivity(i);
Log.d("clicked item: ", "" + id1);
}
});
//recom2.setImage(ImageSource.uri(re2));
//Picasso.with(getApplicationContext()).load(re2).into(recom2);
recom2.setImageUrl(re2,imageloader);
//imageloader.get(re2, ImageLoader.getImageListener(img6,R.drawable.ic_launcher,android.R.drawable.ic_dialog_alert));
recom2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent i = new Intent(caps1.this,caps2.class).putExtra("posi",id2);
startActivity(i);
Log.d("clicked item: " ,""+id2);
}
});
//recom3.setImage(ImageSource.uri(re3));
//Picasso.with(getApplicationContext()).load(re3).into(recom3);
recom3.setImageUrl(re3,imageloader);
//imageloader.get(re3, ImageLoader.getImageListener(img6,R.drawable.ic_launcher,android.R.drawable.ic_dialog_alert));
recom3.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent i = new Intent(caps1.this,caps2.class).putExtra("posi",id3);
startActivity(i);
Log.d("clicked item: " ,""+id3);
}
});
}
}catch(JSONException e){
e.printStackTrace();
}
}
},
new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError volleyError) {
}
}
);
requestQueue1.add(or);
}
public void fetchthedata(long id){
showpDialog();
JsonObjectRequest jor = new JsonObjectRequest(Request.Method.GET, url+id , null, new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject jsonObject) {
try {
JSONArray jsonArray = jsonObject.getJSONArray("data");
for (int i = 0; i < jsonArray.length(); i++) {
JSONObject capsule = jsonArray.getJSONObject(i);
String m1 = capsule.getString("m1");
imageloader = MySingleton.getInstance(getApplicationContext()).getImageLoader();
m1.replace("\\"," ");
m1.trim();
String caphead = capsule.getString("person_headshot");
caphead.replace("\\"," ");
caphead.trim();
String writerhead = capsule.getString("editor_headshot");
String m2 = capsule.getString("m2");
m2.replace("\\"," ");
m2.trim();
String m3 = capsule.getString("m3");
m3.replace("\\"," ");
m3.trim();
String m4 = capsule.getString("m4");
m4.replace("\\"," ");
m4.trim();
String m5 = capsule.getString("m5");
m5.replace("\\"," ");
m5.trim();
String m6 = capsule.getString("m6");
m6.replace("\\"," ");
m6.trim();
String editorpro = capsule.getString("editor_profession");
String editornam = capsule.getString("editor_name");
String quote = capsule.getString("quote");
String pro = capsule.getString("profession");
String headerimag = capsule.getString("mheader");
headerimag.replace("\\"," ");
headerimag.trim();
String quoteby = capsule.getString("name");
String bday = capsule.getString("birthday");
String plac = capsule.getString("place");
String q1 = capsule.getString("q1");
String a1 = capsule.getString("a1");
String q2 = capsule.getString("q2");
String a2 = capsule.getString("a2");
String q3 = capsule.getString("q3");
String a3 = capsule.getString("a3");
String q4 = capsule.getString("q4");
String a4 = capsule.getString("a4");
String q5 = capsule.getString("q5");
String a5 = capsule.getString("a5");
String q6 = capsule.getString("q6");
String a6 = capsule.getString("a6");
String q7 = capsule.getString("q7");
String a7 = capsule.getString("a7");
String q8 = capsule.getString("q8");
String a8 = capsule.getString("a8");
String q9 = capsule.getString("q9");
String a9 = capsule.getString("a9");
String q10 = capsule.getString("q10");
String a10 = capsule.getString("a10");
String reads= capsule.getString("hits");
String share= capsule.getString("shares");
text44.setText(reads);
text55.setText(share);
text666.setText(quoteby);
text6666.setText(pro);
text66666.setText(bday);
text666666.setText(plac);
que1.setText(q1);
ans1.setText(a1);
que2.setText(q2);
ans2.setText(a2);
que3.setText(q3);
ans3.setText(a3);
que4.setText(q4);
ans4.setText(a4);
que5.setText(q5);
ans5.setText(a5);
que6.setText(q6);
ans6.setText(a6);
que7.setText(q7);
ans7.setText(a7);
que8.setText(q8);
ans8.setText(a8);
que9.setText(q9);
ans9.setText(a9);
que10.setText(q10);
ans10.setText(a10);
quotes.setText(quote);
quotebys.append(quoteby);
editorname.append(editornam);
editorprof.setText(editorpro);
headernam.setText(quoteby);
headerprof.setText(pro);
Log.d("TAG",""+m1);
//imageloader.get(headerimag, ImageLoader.getImageListener(headerimage, R.drawable.ic_launcher, android.R.drawable.ic_dialog_alert));
headerimage.setImageUrl(headerimag,imageloader);
//imageloader.get(m1, ImageLoader.getImageListener(img1, R.drawable.ic_launcher, android.R.drawable.ic_dialog_alert));
img1.setImageUrl(m1,imageloader);
//imageloader.get(m2, ImageLoader.getImageListener(img2,R.drawable.ic_launcher,android.R.drawable.ic_dialog_alert));
img2.setImageUrl(m2,imageloader);
//imageloader.get(m3, ImageLoader.getImageListener(img3,R.drawable.ic_launcher,android.R.drawable.ic_dialog_alert));
img3.setImageUrl(m3,imageloader);
//imageloader.get(m4, ImageLoader.getImageListener(img4,R.drawable.ic_launcher,android.R.drawable.ic_dialog_alert));
img4.setImageUrl(m4,imageloader);
//imageloader.get(m5, ImageLoader.getImageListener(img5,R.drawable.ic_launcher,android.R.drawable.ic_dialog_alert));
img5.setImageUrl(m5,imageloader);
//imageloader.get(m5, ImageLoader.getImageListener(img6,R.drawable.ic_launcher,android.R.drawable.ic_dialog_alert));
img6.setImageUrl(m6,imageloader);
/*Picasso.with(getApplicationContext()).load(m1).into(img1);
Picasso.with(getApplicationContext()).load(m2).into(img2);
Picasso.with(getApplicationContext()).load(m3).into(img3);
Picasso.with(getApplicationContext()).load(m4).into(img4);
Picasso.with(getApplicationContext()).load(m5).into(img5);
Picasso.with(getApplicationContext()).load(m6).into(img6);
Picasso.with(getApplicationContext()).load(headerimag).into(headerimage);*/
/*headerimage.setImage(ImageSource.uri(headerimag));
img1.setImage(ImageSource.uri(m1));
img2.setImage(ImageSource.uri(m2));
img3.setImage(ImageSource.uri(m3));
img4.setImage(ImageSource.uri(m4));
img5.setImage(ImageSource.uri(m5));
img6.setImage(ImageSource.uri(m6));*/
}
} catch (JSONException e) {
e.printStackTrace();
}hidepDialog();
}
},
new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError volleyError) {
pdialog.dismiss();
snackbar.show();
}
}
);
requestQueue.add(jor);
}
public static Bitmap getBitmapFromURL(String src) {
try {
URL url = new URL(src);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setDoInput(true);
connection.connect();
InputStream input = connection.getInputStream();
Bitmap myBitmap = BitmapFactory.decodeStream(input);
return myBitmap;
} catch (IOException e) {
e.printStackTrace();
return null;
}
}
/*private void setPalette() {
Bitmap bitmap = ((BitmapDrawable) collapse.getDrawable()).getBitmap();
Palette.from(bitmap).generate(new Palette.PaletteAsyncListener() {
@Override
public void onGenerated(Palette palette) {
int primaryDark = getResources().getColor(R.color.primary_dark);
int primary = getResources().getColor(R.color.primary);
collapsingToolbarLayout.setContentScrimColor(palette.getMutedColor(primary));
collapsingToolbarLayout.setStatusBarScrimColor(palette.getDarkVibrantColor(primaryDark));
}
});
}*/
public Bitmap getRoundedShape(Bitmap scaleBitmapImage) {
int targetWidth = 50;
int targetHeight = 50;
Bitmap targetBitmap = Bitmap.createBitmap(targetWidth,
targetHeight,Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(targetBitmap);
Path path = new Path();
path.addCircle(((float) targetWidth - 1) / 2,
((float) targetHeight - 1) / 2,
(Math.min(((float) targetWidth),
((float) targetHeight)) / 2),
Path.Direction.CCW);
canvas.clipPath(path);
Bitmap sourceBitmap = scaleBitmapImage;
canvas.drawBitmap(sourceBitmap,
new Rect(0, 0, sourceBitmap.getWidth(),
sourceBitmap.getHeight()),
new Rect(0, 0, targetWidth, targetHeight), null);
return targetBitmap;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
private void showpDialog() {
if (!pdialog.isShowing())
pdialog.show();
}
private void hidepDialog() {
if (pdialog.isShowing())
pdialog.dismiss();
}
}
答案 0 :(得分:0)
你在主Activity线程上做了很多工作,这就是内存异常发生的原因。
在后台线程中执行。
答案 1 :(得分:0)
在主线程(或UIThread)上做大量工作不会导致OOM。
我对我的OOM感到厌倦,时不时地头疼。
我也建议使用像毕加索这样的图书馆,比如穆罕默德。我使用Nostra13 UniversalImageLoader。 Nostra13有一个LRUCache机制来处理内存消耗。没有检查毕加索是如何做到的,我喜欢它。
如果您必须从资源中解码一些高质量的图像,这里有一个很好的起点,这对我有很大帮助。祝你好运。
答案 2 :(得分:0)
检查包含图像的文件夹。当我学习Android时,我看到了类似的错误。将图像移动到drawable-nodpi为我解决了这个问题。