我还没有找到答案。我刚刚开始在android studio中学习编程,现在我遇到了一些麻烦。我无法解决代码中发生的事情。我正在学习几个月前编辑过的在线教程,视频中的人根本没有问题。 Bellow是代码截图。提前致谢!!
MainActivity.java
public class MainActivity extends Activity implements View.OnClickListener{
private ImageView mozartImage;
private ImageView bachImage;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mozartImage = (ImageView) findViewById(R.id.imageViewMozartId);
bachImage = (ImageView) findViewById(R.id.imageViewBachId);
mozartImage.setOnClickListener(this);
bachImage.setOnClickListener(this);
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.imageViewMozartId:
Intent mozartIntent = new Intent(MainActivity.this, DetailsActivity.class);
String mozartBio = "Wolfgang Amadeus Mozart was born in Salzburg and showed prodigious ability" +
"from his earliest childhood. Already competent on keyboard and violin, he composed from the age of five " +
"and performed before European royalty. At 17. he was engaged as a court musician in Salzburg. ";
mozartIntent.putExtra("mozart", mozartBio);
mozartIntent.putExtra("name", "Wolfgang Amadeus Mozart");
startActivity(mozartIntent);
break;
case R.id.imageViewBachId:
Intent bachIntent = new Intent(MainActivity.this, DetailsActivity.class);
String bachBio = "Johann Sebastian Bach was born in Eisenach, Saxe-Eisenach, into a great musical family. " +
"His father, Johann Ambrosius Bach, was the director of the town musicians, " +
"and all of his uncles were proffesional musicians. His father taught him to play the violin. ";
bachIntent.putExtra("bach", bachBio);
bachIntent.putExtra("name", "Johann Sebastian Bach");
startActivity(bachIntent);
break;
}
}
}
DetailsActivity
public class DetailsActivity extends Activity {
private ImageView profileImage;
private TextView bioText;
private Bundle extras;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_details);
profileImage = (ImageView) findViewById(R.id.imageViewBioId);
bioText = (TextView) findViewById(R.id.textViewBioId);
extras = getIntent().getExtras();
if (extras != null) {
String name = extras.getString("name");
showDetails(name);
}
}
public void showDetails(String nName) {
Toast.makeText(this, nName, Toast.LENGTH_LONG).show();
if (nName.equals("mozart")) {
profileImage.setImageDrawable(ContextCompat.getDrawable(this, R.drawable.mozart));
bioText.setText(extras.getString("nName"));
} else if (nName.equals("bach")) {
profileImage.setImageDrawable(ContextCompat.getDrawable(this, R.drawable.bach));
bioText.setText(extras.getString("nName"));
}
}
}
logcat
07-03 18:29:36.741 12648-12648/? I/art﹕ Late-enabling -Xcheck:jni
07-03 18:29:37.383 12648-12648/showmethebio.myandroidhello.com.showmethebio I/art﹕ Alloc partial concurrent mark sweep GC freed 83(16KB) AllocSpace objects, 0(0B) LOS objects, 40% free, 7MB/12MB, paused 350us total 49.224ms
07-03 18:29:37.392 12648-12648/showmethebio.myandroidhello.com.showmethebio I/art﹕ Alloc concurrent mark sweep GC freed 19(12KB) AllocSpace objects, 0(0B) LOS objects, 39% free, 7MB/12MB, paused 388us total 7.055ms
07-03 18:29:37.392 12648-12648/showmethebio.myandroidhello.com.showmethebio I/art﹕ Forcing collection of SoftReferences for 96MB allocation
07-03 18:29:37.397 12648-12648/showmethebio.myandroidhello.com.showmethebio E/art﹕ Throwing OutOfMemoryError "Failed to allocate a 101654796 byte allocation with 5184004 free bytes and 88MB until OOM"
07-03 18:29:37.408 12648-12648/showmethebio.myandroidhello.com.showmethebio I/art﹕ Alloc concurrent mark sweep GC freed 0(0B) AllocSpace objects, 0(0B) LOS objects, 39% free, 7MB/12MB, paused 268us total 6.618ms
07-03 18:29:37.409 12648-12648/showmethebio.myandroidhello.com.showmethebio I/art﹕ Forcing collection of SoftReferences for 96MB allocation
07-03 18:29:37.416 12648-12648/showmethebio.myandroidhello.com.showmethebio I/art﹕ Alloc concurrent mark sweep GC freed 0(0B) AllocSpace objects, 0(0B) LOS objects, 39% free, 7MB/12MB, paused 300us total 6.963ms
07-03 18:29:37.416 12648-12648/showmethebio.myandroidhello.com.showmethebio E/art﹕ Throwing OutOfMemoryError "Failed to allocate a 101654796 byte allocation with 5184004 free bytes and 88MB until OOM"
07-03 18:29:37.416 12648-12648/showmethebio.myandroidhello.com.showmethebio D/skia﹕ --- allocation failed for scaled bitmap
07-03 18:29:37.417 12648-12648/showmethebio.myandroidhello.com.showmethebio D/AndroidRuntime﹕ Shutting down VM
07-03 18:29:37.418 12648-12648/showmethebio.myandroidhello.com.showmethebio E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: showmethebio.myandroidhello.com.showmethebio, PID: 12648
java.lang.RuntimeException: Unable to start activity ComponentInfo{showmethebio.myandroidhello.com.showmethebio/showmethebio.myandroidhello.com.showmethebio.MainActivity}: android.view.InflateException: Binary XML file line #9: Error inflating class android.widget.ImageView
...
Caused by: java.lang.reflect.InvocationTargetException
at java.lang.reflect.Constructor.newInstance(Native Method)
...
Caused by: java.lang.OutOfMemoryError: Failed to allocate a 101654796 byte allocation with 5184004 free bytes and 88MB until OOM
at dalvik.system.VMRuntime.newNonMovableArray(Native Method)
我真的很陌生,每一个帮助都会帮助我度过难关。提前致谢!!
AndroidManfiest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="showmethebio.myandroidhello.com.showmethebio" >
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name=".MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".DetailsActivity"
android:label="@string/title_activity_details" >
</activity>
</application>
</manifest>
Activity_main.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:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity"
android:background="#3bffa729">
<ImageView
android:layout_width="150dp"
android:layout_height="150dp"
android:contentDescription="@string/mozart_name"
android:id="@+id/imageViewMozartId"
android:background="@drawable/mozart"
android:layout_centerHorizontal="true"
android:layout_marginTop="70dp" />
<ImageView
android:layout_width="150dp"
android:layout_height="150dp"
android:contentDescription="@string/bach_name"
android:id="@+id/imageViewBachId"
android:background="@drawable/bach"
android:layout_marginBottom="70dp"
android:layout_centerHorizontal="true"
android:layout_alignParentBottom="true"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="@string/mozart_name"
android:id="@+id/textViewMozartId"
android:layout_below="@+id/imageViewMozartId"
android:layout_centerHorizontal="true"
android:textColor="#ffab0d77" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="@string/bach_name"
android:id="@+id/textViewBachId"
android:textColor="#ffab0d77"
android:layout_alignParentBottom="true"
android:layout_alignRight="@+id/textViewMozartId"
android:layout_alignEnd="@+id/textViewMozartId" />
</RelativeLayout>
Activity_details.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:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin"
tools:context="showmethebio.myandroidhello.com.showmethebio.DetailsActivity"
android:background="#3bffa729">
<ImageView
android:layout_width="180dp"
android:layout_height="180dp"
android:contentDescription="@string/bio_name"
android:id="@+id/imageViewBioId"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="125dp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="15dp"
android:text=""
android:id="@+id/textViewBioId"
android:layout_below="@+id/imageViewBioId"
android:layout_centerHorizontal="true" />
</RelativeLayout>
答案 0 :(得分:2)
可能是ImageView
id
imageViewMozartId ,它太大了(101654796 = 96,95 mebibyte)。
您应该使用合理尺寸和重量的 jpeg 或 png 中的图像。
答案 1 :(得分:1)
导致OutofMemory错误的图像/位图。 android developers页面建议使用文档中定义的这两种方法。仔细阅读它曾经帮我一次。