我正在尝试制作一个包含两个活动的简单应用。第一个被称为主要活动,它有一个按钮。当我点击按钮时,它会进入第二个名为" Leyenda"使用自定义ListView
。
该应用程序编译并运行,但当我点击第一个活动中的按钮转到第二个活动时,它会停止
这是活动和Android Manifest的代码。
这是locat错误
11-30 10:56:24.432: E/art(2835): Throwing OutOfMemoryError "Failed to allocate a 215737356 byte allocation with 1048576 free bytes and 63MB until OOM"
11-30 10:56:24.434: E/AndroidRuntime(2835): FATAL EXCEPTION: main
11-30 10:56:24.434: E/AndroidRuntime(2835): Process: com.example.guille.listview, PID: 2835
11-30 10:56:24.434: E/AndroidRuntime(2835): android.view.InflateException: Binary XML file line #37: Error inflating class android.widget.ImageView
11-30 10:56:24.434: E/AndroidRuntime(2835): at android.view.LayoutInflater.createView(LayoutInflater.java:633)
11-30 10:56:24.434: E/AndroidRuntime(2835): at com.android.internal.policy.impl.PhoneLayoutInflater.onCreateView(PhoneLayoutInflater.java:55)
11-30 10:56:24.434: E/AndroidRuntime(2835): at android.view.LayoutInflater.onCreateView(LayoutInflater.java:682)
11-30 10:56:24.434: E/AndroidRuntime(2835): at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:741)
11-30 10:56:24.434: E/AndroidRuntime(2835): at android.view.LayoutInflater.rInflate(LayoutInflater.java:806)
11-30 10:56:24.434: E/AndroidRuntime(2835): at android.view.LayoutInflater.rInflate(LayoutInflater.java:809)
11-30 10:56:24.434: E/AndroidRuntime(2835): at android.view.LayoutInflater.rInflate(LayoutInflater.java:809)
11-30 10:56:24.434: E/AndroidRuntime(2835): at android.view.LayoutInflater.rInflate(LayoutInflater.java:809)
11-30 10:56:24.434: E/AndroidRuntime(2835): at android.view.LayoutInflater.rInflate(LayoutInflater.java:809)
11-30 10:56:24.434: E/AndroidRuntime(2835): at android.view.LayoutInflater.inflate(LayoutInflater.java:504)
11-30 10:56:24.434: E/AndroidRuntime(2835): at android.view.LayoutInflater.inflate(LayoutInflater.java:414)
11-30 10:56:24.434: E/AndroidRuntime(2835): at com.example.guille.listview.Leyenda$ListViewAdapter.getView(Leyenda.java:81)
11-30 10:56:24.434: E/AndroidRuntime(2835): at android.widget.AbsListView.obtainView(AbsListView.java:2344)
11-30 10:56:24.434: E/AndroidRuntime(2835): at android.widget.ListView.measureHeightOfChildren(ListView.java:1270)
11-30 10:56:24.434: E/AndroidRuntime(2835): at android.widget.ListView.onMeasure(ListView.java:1182)
11-30 10:56:24.434: E/AndroidRuntime(2835): at android.view.View.measure(View.java:17430)
11-30 10:56:24.434: E/AndroidRuntime(2835): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5463)
11-30 10:56:24.434: E/AndroidRuntime(2835): at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1436)
11-30 10:56:24.434: E/AndroidRuntime(2835): at android.widget.LinearLayout.measureVertical(LinearLayout.java:722)
11-30 10:56:24.434: E/AndroidRuntime(2835): at android.widget.LinearLayout.onMeasure(LinearLayout.java:613)
11-30 10:56:24.434: E/AndroidRuntime(2835): at android.view.View.measure(View.java:17430)
11-30 10:56:24.434: E/AndroidRuntime(2835): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5463)
11-30 10:56:24.434: E/AndroidRuntime(2835): at android.widget.FrameLayout.onMeasure(FrameLayout.java:430)
11-30 10:56:24.434: E/AndroidRuntime(2835): at android.view.View.measure(View.java:17430)
11-30 10:56:24.434: E/AndroidRuntime(2835): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5463)
11-30 10:56:24.434: E/AndroidRuntime(2835): at android.support.v7.internal.widget.ActionBarOverlayLayout.onMeasure(ActionBarOverlayLayout.java:453)
11-30 10:56:24.434: E/AndroidRuntime(2835): at android.view.View.measure(View.java:17430)
11-30 10:56:24.434: E/AndroidRuntime(2835): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5463)
11-30 10:56:24.434: E/AndroidRuntime(2835): at android.widget.FrameLayout.onMeasure(FrameLayout.java:430)
11-30 10:56:24.434: E/AndroidRuntime(2835): at android.view.View.measure(View.java:17430)
11-30 10:56:24.434: E/AndroidRuntime(2835): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5463)
11-30 10:56:24.434: E/AndroidRuntime(2835): at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1436)
11-30 10:56:24.434: E/AndroidRuntime(2835): at android.widget.LinearLayout.measureVertical(LinearLayout.java:722)
11-30 10:56:24.434: E/AndroidRuntime(2835): at android.widget.LinearLayout.onMeasure(LinearLayout.java:613)
11-30 10:56:24.434: E/AndroidRuntime(2835): at android.view.View.measure(View.java:17430)
11-30 10:56:24.434: E/AndroidRuntime(2835): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5463)
11-30 10:56:24.434: E/AndroidRuntime(2835): at android.widget.FrameLayout.onMeasure(FrameLayout.java:430)
11-30 10:56:24.434: E/AndroidRuntime(2835): at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2560)
11-30 10:56:24.434: E/AndroidRuntime(2835): at android.view.View.measure(View.java:17430)
11-30 10:56:24.434: E/AndroidRuntime(2835): at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:2001)
11-30 10:56:24.434: E/AndroidRuntime(2835): at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1166)
11-30 10:56:24.434: E/AndroidRuntime(2835): at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1372)
11-30 10:56:24.434: E/AndroidRuntime(2835): at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1054)
11-30 10:56:24.434: E/AndroidRuntime(2835): at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:5779)
11-30 10:56:24.434: E/AndroidRuntime(2835): at android.view.Choreographer$CallbackRecord.run(Choreographer.java:767)
11-30 10:56:24.434: E/AndroidRuntime(2835): at android.view.Choreographer.doCallbacks(Choreographer.java:580)
11-30 10:56:24.434: E/AndroidRuntime(2835): at android.view.Choreographer.doFrame(Choreographer.java:550)
11-30 10:56:24.434: E/AndroidRuntime(2835): at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:753)
11-30 10:56:24.434: E/AndroidRuntime(2835): at android.os.Handler.handleCallback(Handler.java:739)
11-30 10:56:24.434: E/AndroidRuntime(2835): at android.os.Handler.dispatchMessage(Handler.java:95)
11-30 10:56:24.434: E/AndroidRuntime(2835): at android.os.Looper.loop(Looper.java:135)
11-30 10:56:24.434: E/AndroidRuntime(2835): at android.app.ActivityThread.main(ActivityThread.java:5221)
11-30 10:56:24.434: E/AndroidRuntime(2835): at java.lang.reflect.Method.invoke(Native Method)
11-30 10:56:24.434: E/AndroidRuntime(2835): at java.lang.reflect.Method.invoke(Method.java:372)
11-30 10:56:24.434: E/AndroidRuntime(2835): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
11-30 10:56:24.434: E/AndroidRuntime(2835): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)
11-30 10:56:24.434: E/AndroidRuntime(2835): Caused by: java.lang.reflect.InvocationTargetException
11-30 10:56:24.434: E/AndroidRuntime(2835): at java.lang.reflect.Constructor.newInstance(Native Method)
11-30 10:56:24.434: E/AndroidRuntime(2835): at java.lang.reflect.Cons
package com.example.guille.listview;
import android.content.Intent;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
public class MainActivity extends ActionBarActivity {
Intent Leyenda;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button bt = (Button) findViewById(R.id.button);
Leyenda = new Intent(this, Leyenda.class);
bt.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
startActivity(Leyenda);
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
@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);
}
}
package com.example.guille.listview;
import android.content.Context;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
/**
* Created by Guille on 20/11/15.
*/
public class Leyenda extends ActionBarActivity {
ListViewAdapter adapter;
String[] titulo = {"Titulo 1", "Titulo 2", "Titulo 3", "Titulo 4"};
String[] subtitulo = {"subítulo 1", "subítulo 2", "subitulo 3", "subtitulo 4"};
int[] imagenes = {R.drawable.task, R.drawable.mensaje, R.drawable.task, R.drawable.mensaje};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.leyenda);
ListView lista = (ListView) findViewById(R.id.listViewleyenda);
adapter = new ListViewAdapter(this, imagenes, titulo, subtitulo);
lista.setAdapter(adapter);
}
//ListViewAdapter
public class ListViewAdapter extends BaseAdapter {
// Declare Variables
Context context;
int[] imagenes;
String[] titulos;
String[] subtitulo;
String[] contenido;
LayoutInflater inflater;
public ListViewAdapter(Context context, int[] imagenes, String[] titulos, String[] subtitulo) {
this.context = context;
this.imagenes = imagenes;
this.titulos = titulos;
this.subtitulo = subtitulo;
}
@Override
public int getCount() {
return titulos.length;
}
@Override
public Object getItem(int position) {
return null;
}
@Override
public long getItemId(int position) {
return 0;
}
public View getView(int position, View convertView, ViewGroup parent) {
// Declare Variables
ImageView imgImg;
TextView txtTitle;
TextView txtSubtitulos;
TextView txtContenido;
//http://developer.android.com/intl/es/reference/android/view/LayoutInflater.html
inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View itemView = inflater.inflate(R.layout.list_row, parent, false);
// Locate the TextViews in listview_item.xml
imgImg = (ImageView) itemView.findViewById(R.id.imagen_single_post_circuito);
txtTitle = (TextView) itemView.findViewById(R.id.tv_titulo_single_post_circuito);
txtSubtitulos = (TextView) itemView.findViewById(R.id.tv_contenido_single_post_circuito);
// Capture position and set to the TextViews
imgImg.setImageResource(imagenes[position]);
txtTitle.setText(titulos[position]);
txtSubtitulos.setText(subtitulo[position]);
return itemView;
}
}
}
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.guille.listview" >
<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=".Leyenda">
</activity>
</application>
</manifest>
答案 0 :(得分:0)
使用Intent
...
示例:
Intent intent = new Intent(MainActivity.this, Leyenda.class);
startActivity(intent);
或者 在清单
中声明一个Leyenda类答案 1 :(得分:0)
按住主要活动按钮调用以下代码。
Leyenda = new Intent(MainActivity.this, Leyenda.class);
startActivity(Leyenda);
答案 2 :(得分:0)
归功于
OutOfMemoryError
。将largeHeap = true
添加到清单中的应用程序代码中。
但不建议这样做,并确保在设置
时添加try catch(OutOfMemoryError e)
imgImg.setImageResource(imagenes[position]);
和压缩您的图片,然后再将其设置为imgImg