我创建的Android应用程序Forceclose
所以我有一个XML问题,当我打开它时,它会给我力量。当我删除一些图像视图时,比如说只有7个XML格式的图像视图是有效的。知道为什么吗?我不知道为什么当我的xml上有更多的图像时它不工作 App3.XML我创建的Android应用程序Forceclose,android,android-layout,android-activity,forceclose,Android,Android Layout,Android Activity,Forceclose,所以我有一个XML问题,当我打开它时,它会给我力量。当我删除一些图像视图时,比如说只有7个XML格式的图像视图是有效的。知道为什么吗?我不知道为什么当我的xml上有更多的图像时它不工作 App3.XML <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@color/JCBG"
android:gravity="center"
android:orientation="vertical">
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:gravity="center"
android:text="Products"
android:textColor="@color/JCLogo"
android:textSize="30dp"/>
<ViewFlipper
android:id="@+id/fliping"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<!-- The child Views/Layout to flip -->
<!-- Layout 1 for 1st Screen -->
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="center_vertical|center_horizontal|center"
android:orientation="vertical" >
<ImageView
android:id="@+id/imageView1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="15dp"
android:scaleType="fitXY"
android:src="@drawable/gr" />
</LinearLayout>
<!-- Layout 2 for 2nd Screen -->
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="center"
android:orientation="vertical">
<ImageView
android:id="@+id/imageView2"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="15dp"
android:scaleType="fitXY"
android:src="@drawable/calvitcdetails" />
</LinearLayout>
<!-- Layout 3 for 3rd Screen -->
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="center"
android:orientation="vertical">
<ImageView
android:id="@+id/imageView3"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="15dp"
android:scaleType="fitXY"
android:src="@drawable/kapedetails"/>
</LinearLayout>
<!-- Layout 4 for 4th Screen -->
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="center"
android:orientation="vertical">
<ImageView
android:id="@+id/imageView4"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="15dp"
android:scaleType="fitXY"
android:src="@drawable/glutafitdetails"/>
</LinearLayout>
<!-- Layout 5 for 5th Screen -->
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="center"
android:orientation="vertical">
<ImageView
android:id="@+id/imageView5"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="15dp"
android:scaleType="fitXY"
android:src="@drawable/pgtdetails"/>
</LinearLayout>
<!-- Layout 6 for 6th Screen -->
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="center"
android:orientation="vertical">
<ImageView
android:id="@+id/imageView6"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="15dp"
android:scaleType="fitXY"
android:src="@drawable/hilifedetails"/>
</LinearLayout>
<!-- Layout 7 for 7th Screen -->
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="center"
android:orientation="vertical">
<ImageView
android:id="@+id/imageView7"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="15dp"
android:scaleType="fitXY"
android:src="@drawable/organicbarleyjuicedetails"/>
</LinearLayout>
<!-- Layout 8 for 8th Screen -->
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="center"
android:orientation="vertical">
<ImageView
android:id="@+id/imageView8"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="15dp"
android:scaleType="fitXY"
android:src="@drawable/barleydetails"/>
</LinearLayout>
<!-- Layout 9 for 9th Screen -->
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="center"
android:orientation="vertical">
<ImageView
android:id="@+id/imageView9"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="15dp"
android:scaleType="fitXY"
android:src="@drawable/spirulinadetails"/>
</LinearLayout>
<!-- Layout 10 for 10th Screen -->
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="center"
android:orientation="vertical">
<ImageView
android:id="@+id/imageView10"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="15dp"
android:scaleType="fitXY"
android:src="@drawable/omnisoapdetails"/>
</LinearLayout>
<!-- Layout 11 for 11th Screen -->
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="center"
android:orientation="vertical">
<ImageView
android:id="@+id/imageView11"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="15dp"
android:scaleType="fitXY"
android:src="@drawable/omnipinkishdetails"/>
</LinearLayout>
<!-- Layout 12 for 12th Screen -->
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="center"
android:orientation="vertical">
<ImageView
android:id="@+id/imageView12"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="15dp"
android:scaleType="fitXY"
android:src="@drawable/omnidaydetails"/>
</LinearLayout>
<!-- Layout 13 for 13th Screen -->
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="center"
android:orientation="vertical">
<ImageView
android:id="@+id/imageView13"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="15dp"
android:scaleType="fitXY"
android:src="@drawable/omnitonerdetails"/>
</LinearLayout>
</ViewFlipper>
</LinearLayout>
Activity3.java
package com.example.jcw;
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MotionEvent;
import android.widget.ViewFlipper;
public class activity3 extends Activity
{
private ViewFlipper vf;
private float lastX;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.app3);
vf = (ViewFlipper) findViewById(R.id.fliping);
}
public boolean onTouchEvent(MotionEvent touchevent) {
switch (touchevent.getAction()) {
case MotionEvent.ACTION_DOWN: {
lastX = touchevent.getX();
break;
}
case MotionEvent.ACTION_UP: {
float currentX = touchevent.getX();
if (lastX < currentX) {
if (vf.getDisplayedChild() == 0)
break;
vf.setInAnimation(this, R.anim.in_from_left);
vf.setOutAnimation(this, R.anim.out_to_right);
vf.showNext();
}
if (lastX > currentX) {
if (vf.getDisplayedChild() == 1)
break;
vf.setInAnimation(this, R.anim.in_from_right);
vf.setOutAnimation(this, R.anim.out_to_left);
vf.showPrevious();
}
break;
}
}
return false;
}
}
package com.example.jcw;
导入android.app.Activity;
导入android.os.Bundle;
导入android.view.Menu;
导入android.view.MotionEvent;
导入android.widget.ViewFlipper;
公共类活动3扩展活动
{
私有视图翻转器vf;
私人浮动lastX;
/**在首次创建活动时调用*/
@凌驾
创建时的公共void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.app3);
vf=(ViewFlipper)findViewById(R.id.fliping);
}
公共布尔onTouchEvent(MotionEvent touchevent){
开关(touchevent.getAction()){
case MotionEvent.ACTION\u DOWN:{
lastX=touchevent.getX();
打破
}
case MotionEvent.ACTION\u UP:{
float currentX=touchevent.getX();
if(lastXcurrentX){
if(vf.getDisplayedChild()==1)
打破
vf.设定无生气(这是右起的R.anim.in_);
vf.setOutAnimation(这个,R.anim.out从左到右);
vf.showPrevious();
}
打破
}
}
返回false;
}
}
ImageView
小部件使用占用大量堆空间的位图。因此,如果您试图同时加载多个图像,您将得到OutOfMemoryError
,它可能在您的Logcat中
如果位图的分辨率高于实际显示所需的分辨率,您可以优化加载它们,您将找到谷歌的示例
如果不是这样,那么您必须以不同的方式设计应用程序。在应用程序清单中添加以下内容:
android:largeHeap=“true”您可以更改您的plz-post-your-logcatMy-LOGCAT以处理内存不足。检查一下,谢谢!但很抱歉,我是android开发和java的新手。我知道我需要调整大小,但如何使用提供的代码调用我拥有的图像并自动调整大小。不要为ImageView
设置android:src
。相反,在活动中使用findViewById()
来获取ImageView引用。然后在后台线程中,例如使用AsyncTask
,加载调整大小的位图并将其设置为ImageView
引用的源。加载位图和UI视图设置示例可以在我的答案中的链接文档下找到。
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@color/JCBG"
android:gravity="center"
android:orientation="vertical">
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:gravity="center"
android:text="Products"
android:textColor="@color/JCLogo"
android:textSize="30dp"/>
<AdapterViewFlipper
android:id="@+id/flipper"
android:layout_width="fill_parent"
android:layout_height="match_parent"/>
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="center"
android:orientation="vertical">
<ImageView
android:id="@+id/imageView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="15dp"
android:scaleType="fitXY"/>
</LinearLayout>
adapterViewFlipper.setAdapter(new BAdapter(this,yourImageResourceArray));
public class Badapter extends BaseAdapter{
Context context;
ArrayList<Integer> imageResources;
public Badapter(Context context,ArrayList<Integer> imageResources) {
this.context = context;
this.imageResources = imageResources;
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return imageResources.size();
}
@Override
public Object getItem(int arg0) {
// TODO Auto-generated method stub
return imageResources.get(arg0);
}
@Override
public long getItemId(int arg0) {
// TODO Auto-generated method stub
return 0;
}
@Override
public View getView(int position, View arg1, ViewGroup arg2) {
LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View rowView = inflater
.inflate(R.layout.mLayout, null, true);
ImageView imageView = (ImageView) rowView.findViewById(R.id.imageView);
imageView.setBackgroundResource(imageResources.get(position));
//if you use view Holder you will save more memory
return rowView;
}
}