我想在我的Android应用程序中添加工具栏,但是我得到了“致命的异常:主要 java.lang.OutOfMemoryError“当我将Activity更改为AppCompatActivity。这是我的代码,我不知道为什么更改行会导致内存不足错误。
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.Toast;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
public class Notifications extends AppCompatActivity {
//public class Notifications extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_notifications);
//Toolbar myToolbar = (Toolbar) findViewById(R.id.my_toolbar);
//setSupportActionBar(myToolbar);
//String[] notificationsDates = {"12Jan 2016", "14Feb 2016", "22Feb 2016", "18Dec 2015", "2Nov 2015", "20Oct 2015", "15Oct 2015"};
//String[] estimationTimes = {"9:00 am", "10:00 am", "11:00 am", "9:00 am", "10:00 am", "10:00 am", "11:00 am"};
final SingleNotification[] notifications = {
new SingleNotification("12Jan2016","9:00 am",R.drawable.fedex,false),
new SingleNotification("14Feb2016","10:00 am",R.drawable.ups,false),
new SingleNotification("22Feb2016","11:00 am",R.drawable.purolator,true),
new SingleNotification("18Dec2015","9:00 am",R.drawable.dhl,true),
new SingleNotification("2Nov2015","10:00 am",R.drawable.fedex,true),
new SingleNotification("20Oct2015","10:00 am",R.drawable.ups,true),
new SingleNotification("15Oct2015","11:00 am",R.drawable.ups,true)
};
ListAdapter listAdapter = new CustomeAdapter(this, notifications);
ListView notificationListView = (ListView) findViewById(R.id.notificationsList);
notificationListView.setAdapter(listAdapter);
notificationListView.setOnItemClickListener(
new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
String date = String.valueOf(notifications[position].getDeliveryDate());
Toast.makeText(Notifications.this, date, Toast.LENGTH_LONG).show();
}
}
);
}
//String[] notificationsDate = {"Delivery Data: 17Feb2015"};
//String[] notificationsTime = {" Estimated Time: "};
}
这里是CustomAdapter:
import android.content.Context;
import android.graphics.Color;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.content.res.Resources;
import android.app.Activity;
public class CustomeAdapter extends ArrayAdapter<SingleNotification> {
CustomeAdapter(Context context, SingleNotification[] notifications) {
super(context, R.layout.custome_row, notifications);
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater layoutInflater = LayoutInflater.from(getContext());
View customView = layoutInflater.inflate(R.layout.custome_row, parent, false);
SingleNotification notification = getItem(position);
LinearLayout singleNotificationLayout = (LinearLayout) customView.findViewById(R.id.singleNotificationLayout);
TextView deliveryDateText = (TextView) customView.findViewById(R.id.delivaryDataText);
TextView notificationNumberText = (TextView) customView.findViewById(R.id.notificationNumberText);
TextView estimationTimeText = (TextView) customView.findViewById(R.id.estimationTimeText);
ImageView logoImage = (ImageView) customView.findViewById(R.id.logoImage);
deliveryDateText.setText(notification.getDeliveryDate());
notificationNumberText.setText(String.valueOf(position + 1));
estimationTimeText.setText(notification.getEstimationTime());
logoImage.setImageResource(notification.getImageID());
if (notification.getIsDelivered())
singleNotificationLayout.setBackgroundColor(Color.parseColor("#6d6d6d"));
else
singleNotificationLayout.setBackgroundColor(Color.parseColor("#FF40459A"));
return customView;
}
}
这是日志:
java.lang.OutOfMemoryError
at android.graphics.BitmapFactory.nativeDecodeAsset(Native Method)
at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:501)
at android.graphics.BitmapFactory.decodeResourceStream(BitmapFactory.java:354)
at android.graphics.drawable.Drawable.createFromResourceStream(Drawable.java:785)
at android.content.res.Resources.loadDrawable(Resources.java:1970)
at android.content.res.Resources.getDrawable(Resources.java:660)
at android.support.v4.content.ContextCompat.getDrawable(ContextCompat.java:323)
at android.support.v7.widget.TintManager.getDrawable(TintManager.java:175)
at android.support.v7.widget.TintManager.getDrawable(TintManager.java:168)
at android.support.v7.widget.AppCompatImageHelper.setImageResource(AppCompatImageHelper.java:51)
at android.support.v7.widget.AppCompatImageView.setImageResource(AppCompatImageView.java:72)
at android.widget.AbsListView.obtainView(AbsListView.java:2143)
at android.widget.ListView.makeAndAddView(ListView.java:1831)
at android.widget.ListView.fillDown(ListView.java:674)
at android.widget.ListView.fillSpecific(ListView.java:1332)
at android.widget.ListView.layoutChildren(ListView.java:1630)
at android.widget.AbsListView.onLayout(AbsListView.java:1994)
at android.view.View.layout(View.java:14008)
at android.view.ViewGroup.layout(ViewGroup.java:4373)
at android.widget.RelativeLayout.onLayout(RelativeLayout.java:1021)
at android.view.View.layout(View.java:14008)
at android.view.ViewGroup.layout(ViewGroup.java:4373)
at android.support.design.widget.CoordinatorLayout.layoutChild(CoordinatorLayout.java:1037)
at android.support.design.widget.CoordinatorLayout.onLayoutChild(CoordinatorLayout.java:747)
at android.support.design.widget.ViewOffsetBehavior.onLayoutChild(ViewOffsetBehavior.java:42)
at android.support.design.widget.AppBarLayout$ScrollingViewBehavior.onLayoutChild(AppBarLayout.java:1156)
at android.support.design.widget.CoordinatorLayout.onLayout(CoordinatorLayout.java:760)
at android.view.View.layout(View.java:14008)
at android.view.ViewGroup.layout(ViewGroup.java:4373)
at android.widget.FrameLayout.onLayout(FrameLayout.java:448)
at android.view.View.layout(View.java:14008)
at android.view.ViewGroup.layout(ViewGroup.java:4373)
at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1663)
at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1521)
at android.widget.LinearLayout.onLayout(LinearLayout.java:1434)
at android.view.View.layout(View.java:14008)
at android.view.ViewGroup.layout(ViewGroup.java:4373)
at android.widget.FrameLayout.onLayout(FrameLayout.java:448)
at android.view.View.layout(View.java:14008)
at android.view.ViewGroup.layout(ViewGroup.java:4373)
at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1663)
at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1521)
at android.widget.LinearLayout.onLayout(LinearLayout.java:1434)
at android.view.View.layout(View.java:14008)
at android.view.ViewGroup.layout(ViewGroup.java:4373)
at android.widget.FrameLayout.onLayout(FrameLayout.java:448)
at android.view.View.layout(View.java:14008)
at android.view.ViewGroup.layout(ViewGroup.java:4373)
at android.view.ViewRootImpl.performLayout(ViewRootImpl.java:1892)
at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1711)
at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:989)
at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:4351)
at android.view.Choreographer$CallbackRecord.run(Choreographer.java:749)
at android.view.Choreographer.doCallbacks(Choreographer.java:562)
at android.view.Choreographer.doFrame(Choreographer.java:532)
at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:735)
at android.os.Handler.handleCallback(Handler.java:725)
at android.os.Handler.dispatchMessage(Handler.java:92)
at android.os.Looper.loop(Looper.java:137)
答案 0 :(得分:2)
所以您可以看到问题出在适配器(at com...timesavvi.CustomeAdapter.getView(CustomeAdapter.java:36)
)中。
罪魁祸首似乎是logoImage.setImageResource(notification.getImageID());
(我假设是36岁)
这意味着getImageID指向的图像是一个非常大的文件,不适合该设备。您应该使用缩放图像来获得各种密度,而不是使用特别大的尺寸以避免这个问题。
请记住,在adpater中,您编写的任何代码都可以被调用数十次,具体取决于您在回收站/列表视图中有多少行,并且如果您从网络资源解码,它可能会继续在后台为那些甚至不在屏幕上的行执行此操作,因此如果您使用它们,则需要管理此类调用