我可以创建一个没有图像的新活动,但是当我使用在新活动上放置图像时,当我尝试创建新活动时,应用程序总是崩溃(按下按钮)。
我还在清单文件中添加了第二个活动。
以下是代码:
activity_first.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"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".First" >
<ImageView
android:id="@+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_marginTop="102dp"
android:src="@drawable/desk" />
<Button
android:id="@+id/switchButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_marginTop="43dp"
android:onClick="buttonClicked"
android:text="Switch!" />
First.Java
package com.example.multipleactivity;
public class First extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_first);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.first, menu);
return true;
}
public void buttonClicked(View view){
Intent intent = new Intent(First.this, Second.class);
startActivity(intent);
}
activity_second.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<ImageView
android:id="@+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/house" />
Second.java
public class Second extends Activity{
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_second);
}
请求时的logCat:
09-05 16:54:21.305: D/ActivityThread(19410): setTargetHeapUtilization:0.25
09-05 16:54:21.305: D/ActivityThread(19410): setTargetHeapIdealFree:8388608
09-05 16:54:21.305: D/ActivityThread(19410): setTargetHeapConcurrentStart:2097152
09-05 16:54:21.815: I/Adreno200-EGLSUB(19410): <ConfigWindowMatch:2087>: Format RGBA_8888.
09-05 16:54:21.825: E/(19410): <s3dReadConfigFile:75>: Can't open file for reading
09-05 16:54:21.825: E/(19410): <s3dReadConfigFile:75>: Can't open file for reading
09-05 16:54:25.279: D/dalvikvm(19410): GC_FOR_ALLOC freed 519K, 28% free 15305K/21059K, paused 22ms, total 22ms
09-05 16:54:25.369: D/dalvikvm(19410): GC_FOR_ALLOC freed 3K, 22% free 20926K/26695K, paused 17ms, total 17ms
09-05 16:54:25.399: D/dalvikvm(19410): GC_BEFORE_OOM freed 9K, 22% free 20917K/26695K, paused 25ms, total 26ms
09-05 16:54:25.399: E/dalvikvm-heap(19410): Out of memory on a 40960016-byte allocation.
09-05 16:54:25.399: I/dalvikvm(19410): "main" prio=5 tid=1 RUNNABLE
09-05 16:54:25.399: I/dalvikvm(19410): | group="main" sCount=0 dsCount=0 obj=0x4164e6a0 self=0x4121fb10
09-05 16:54:25.399: I/dalvikvm(19410): | sysTid=19410 nice=0 sched=0/0 cgrp=apps handle=1074870352
09-05 16:54:25.399: I/dalvikvm(19410): | schedstat=( 0 0 0 ) utm=28 stm=16 core=1
09-05 16:54:25.399: I/dalvikvm(19410): at android.graphics.BitmapFactory.nativeDecodeAsset(Native Method)
09-05 16:54:25.399: I/dalvikvm(19410): at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:592)
09-05 16:54:25.399: I/dalvikvm(19410): at android.graphics.BitmapFactory.decodeResourceStream(BitmapFactory.java:445)
09-05 16:54:25.399: I/dalvikvm(19410): at android.graphics.drawable.Drawable.createFromResourceStream(Drawable.java:781)
09-05 16:54:25.399: I/dalvikvm(19410): at android.content.res.Resources.loadDrawable(Resources.java:1963)
09-05 16:54:25.399: I/dalvikvm(19410): at android.content.res.TypedArray.getDrawable(TypedArray.java:601)
09-05 16:54:25.399: I/dalvikvm(19410): at android.widget.ImageView.<init>(ImageView.java:120)
09-05 16:54:25.399: I/dalvikvm(19410): at android.widget.ImageView.<init>(ImageView.java:110)
09-05 16:54:25.399: I/dalvikvm(19410): at java.lang.reflect.Constructor.constructNative(Native Method)
09-05 16:54:25.399: I/dalvikvm(19410): at java.lang.reflect.Constructor.newInstance(Constructor.java:417)
09-05 16:54:25.399: I/dalvikvm(19410): at android.view.LayoutInflater.createView(LayoutInflater.java:587)
09-05 16:54:25.399: I/dalvikvm(19410): at com.android.internal.policy.impl.PhoneLayoutInflater.onCreateView(PhoneLayoutInflater.java:56)
09-05 16:54:25.399: I/dalvikvm(19410): at android.view.LayoutInflater.onCreateView(LayoutInflater.java:660)
09-05 16:54:25.399: I/dalvikvm(19410): at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:685)
09-05 16:54:25.399: I/dalvikvm(19410): at android.view.LayoutInflater.rInflate(LayoutInflater.java:746)
09-05 16:54:25.399: I/dalvikvm(19410): at android.view.LayoutInflater.inflate(LayoutInflater.java:489)
09-05 16:54:25.399: I/dalvikvm(19410): at android.view.LayoutInflater.inflate(LayoutInflater.java:396)
09-05 16:54:25.399: I/dalvikvm(19410): at android.view.LayoutInflater.inflate(LayoutInflater.java:352)
09-05 16:54:25.409: I/dalvikvm(19410): at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:309)
09-05 16:54:25.409: I/dalvikvm(19410): at android.app.Activity.setContentView(Activity.java:1914)
09-05 16:54:25.409: I/dalvikvm(19410): at com.example.multipleactivity.Second.onCreate(Second.java:9)
09-05 16:54:25.409: I/dalvikvm(19410): at android.app.Activity.performCreate(Activity.java:5179)
09-05 16:54:25.409: I/dalvikvm(19410): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1094)
09-05 16:54:25.409: I/dalvikvm(19410): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2074)
09-05 16:54:25.409: I/dalvikvm(19410): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2135)
09-05 16:54:25.409: I/dalvikvm(19410): at android.app.ActivityThread.access$700(ActivityThread.java:143)
09-05 16:54:25.409: I/dalvikvm(19410): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1241)
09-05 16:54:25.409: I/dalvikvm(19410): at android.os.Handler.dispatchMessage(Handler.java:99)
09-05 16:54:25.409: I/dalvikvm(19410): at android.os.Looper.loop(Looper.java:137)
09-05 16:54:25.409: I/dalvikvm(19410): at android.app.ActivityThread.main(ActivityThread.java:4950)
09-05 16:54:25.409: I/dalvikvm(19410): at java.lang.reflect.Method.invokeNative(Native Method)
09-05 16:54:25.409: I/dalvikvm(19410): at java.lang.reflect.Method.invoke(Method.java:511)
09-05 16:54:25.409: I/dalvikvm(19410): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1004)
09-05 16:54:25.409: I/dalvikvm(19410): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:771)
09-05 16:54:25.409: I/dalvikvm(19410): at dalvik.system.NativeStart.main(Native Method)
09-05 16:54:25.409: A/libc(19410): Fatal signal 11 (SIGSEGV) at 0x00000000 (code=1), thread 19410 (ultipleactivity)
任何想法?
答案 0 :(得分:0)
09-05 16:54:25.399: E/dalvikvm-heap(19410): Out of memory on a 40960016-byte allocation.
你的内存不足。你的图像真的很大吗?
尝试通过缩小尺寸来使用较小的图片,或者如果您不需要它们是透明的,请使用JPEG
代替PNG
。
答案 1 :(得分:-1)
请您复制/粘贴整个xml文件吗? 对于每个XML布局,您必须指定主布局(LinearLayout,RelativeLayout等)
具有居中imageView的活动2的布局示例:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<ImageView
android:id="@+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/ic_launcher"
android:layout_gravity="center|center_horizontal" />
</LinearLayout>