我是Android开发的新手,所以请帮助我:
我有一个带有7个按钮的xml文件,命名为: btn_1 btn_2,... btn_7
以下是 activity_main.xml
<Button
android:layout_width="103dp"
android:layout_height="82dp"
android:id="@+id/btn_1"
android:layout_below="@+id/heading1"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:background="@mipmap/pic1"
style="@style/Base.Widget.AppCompat.Button.Borderless"
android:clickable="true" />
这是 MainActivity.java -
public class MainActivity extends AppCompatActivity {
private static Button btn1, btn2, btn3, btn4, btn5, btn6, btn7;
@
Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
OnClickButtonListener();
}
public void OnClickButtonListener() {
btn1 = (Button) findViewById(R.id.btn_1);
btn1.setOnClickListener(new View.OnClickListener() {@
Override
public void onClick(View v) {
Intent intent_1 = new Intent(MainActivity.this, MainActivity_1.class);
startActivity(intent_1);
}
});
btn2 = (Button) findViewById(R.id.btn_2);
btn2.setOnClickListener(new View.OnClickListener() {@
Override
public void onClick(View v) {
Intent intent_2 = new Intent(MainActivity.this, MainActivity_2.class);
startActivity(intent_2);
}
});
btn3 = (Button) findViewById(R.id.btn_3);
btn3.setOnClickListener(new View.OnClickListener() {@
Override
public void onClick(View v) {
Intent intent_3 = new Intent(MainActivity.this, MainActivity_3.class);
startActivity(intent_3);
}
});
btn4 = (Button) findViewById(R.id.btn_4);
btn4.setOnClickListener(new View.OnClickListener() {@
Override
public void onClick(View v) {
Intent intent_4 = new Intent(MainActivity.this, MainActivity_4.class);
startActivity(intent_4);
}
});
btn5 = (Button) findViewById(R.id.btn_5);
btn5.setOnClickListener(new View.OnClickListener() {@
Override
public void onClick(View v) {
Intent intent_5 = new Intent(MainActivity.this, MainActivity_5.class);
startActivity(intent_5);
}
});
btn6 = (Button) findViewById(R.id.btn_6);
btn6.setOnClickListener(new View.OnClickListener() {@
Override
public void onClick(View v) {
Intent intent_6 = new Intent(MainActivity.this, MainActivity_6.class);
startActivity(intent_6);
}
});
btn7 = (Button) findViewById(R.id.btn_7);
btn7.setOnClickListener(new View.OnClickListener() {@
Override
public void onClick(View v) {
Intent intent_7 = new Intent(MainActivity.this, MainActivity_7.class);
startActivity(intent_7);
}
});
}
}
当我点击按钮时,应用程序崩溃,但令人惊讶的并非总是如此。此外,在一次尝试中,它在单击btn_6时崩溃,然后在下次尝试时(当我重新打开应用程序时),按下另一个按钮会崩溃,比如现在按钮5!
现在,如果我关闭应用程序,然后重试,它可能会在按下btn_3时崩溃,而在之前的尝试中它会完美地工作。
任何见解?感谢。
这是最新的logCat:
05-25 23:43:17.139 17042-17042 <XX> W/ <YY>: VFY: unable to resolve virtual method 134: Landroid/app/Activity;.onEnterAnimationComplete ()V
05-25 23:43:17.139 17042-17042 <XX> D/ <YY>: VFY: replacing opcode 0x6f at 0x140b
05-25 23:43:17.179 17042-17042 <XX> D/ <YY>: GC_FOR_ALLOC freed 345K, 3% free 54952K/56516K, paused 15ms, total 16ms
05-25 23:43:17.179 17042-17042 <XX> I/ <YY>-heap: Forcing collection of SoftReferences for 11040016-byte allocation
05-25 23:43:17.199 17042-17042 <XX> D/ <YY>: GC_BEFORE_OOM freed 3K, 3% free 54948K/56516K, paused 22ms, total 22ms
05-25 23:43:17.199 17042-17042 <XX> E/ <YY>-heap: Out of memory on a 11040016-byte allocation.
05-25 23:43:17.199 17042-17042 <XX> I/ <YY>: "main" prio=5 tid=1 RUNNABLE
05-25 23:43:17.199 17042-17042 <XX> I/ <YY>: | group="main" sCount=0 dsCount=0 obj=0x41e4f578 self=0x41e3a4e8
05-25 23:43:17.199 17042-17042 <XX> I/ <YY>: | sysTid=17042 nice=-16 sched=0/0 cgrp=apps handle=1074368508
05-25 23:43:17.199 17042-17042 <XX> I/ <YY>: | state=R schedstat=( 3391699451 331158977 2372 ) utm=269 stm=70 core=0
05-25 23:43:17.199 17042-17042 <XX> I/ <YY>: at android.graphics.BitmapFactory.nativeDecodeAsset(Native Method)
05-25 23:43:17.199 17042-17042 <XX> I/ <YY>: at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:596)
05-25 23:43:17.199 17042-17042 <XX> I/ <YY>: at android.graphics.BitmapFactory.decodeResourceStream(BitmapFactory.java:444)
05-25 23:43:17.199 17042-17042 <XX> I/ <YY>: at android.graphics.drawable.Drawable.createFromResourceStream(Drawable.java:832)
05-25 23:43:17.199 17042-17042 <XX> I/ <YY>: at android.content.res.Resources.loadDrawable(Resources.java:2988)
05-25 23:43:17.199 17042-17042 <XX> I/ <YY>: at android.content.res.TypedArray.getDrawable(TypedArray.java:602)
05-25 23:43:17.199 17042-17042 <XX> I/ <YY>: at android.view.View.<init>(View.java:3563)
05-25 23:43:17.199 17042-17042 <XX> I/ <YY>: at android.widget.TextView.<init>(TextView.java:881)
05-25 23:43:17.199 17042-17042 <XX> I/ <YY>: at android.widget.Button.<init>(Button.java:108)
05-25 23:43:17.204 17042-17042 <XX> I/ <YY>: at android.support.v7.widget.AppCompatButton.<init>(AppCompatButton.java:62)
05-25 23:43:17.204 17042-17042 <XX> I/ <YY>: at android.support.v7.widget.AppCompatButton.<init>(AppCompatButton.java:58)
05-25 23:43:17.204 17042-17042 <XX> I/ <YY>: at android.support.v7.app.AppCompatViewInflater.createView(AppCompatViewInflater.java:105)
05-25 23:43:17.204 17042-17042 <XX> I/ <YY>: at android.support.v7.app.AppCompatDelegateImplV7.createView(AppCompatDelegateImplV7.java:972)
05-25 23:43:17.204 17042-17042 <XX> I/ <YY>: at android.support.v7.app.AppCompatDelegateImplV7.onCreateView(AppCompatDelegateImplV7.java:1030)
05-25 23:43:17.204 17042-17042 <XX> I/ <YY>: at android.support.v4.view.LayoutInflaterCompatHC$FactoryWrapperHC.onCreateView(LayoutInflaterCompatHC.java:44)
05-25 23:43:17.204 17042-17042 <XX> I/ <YY>: at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:690)
05-25 23:43:17.204 17042-17042 <XX> I/ <YY>: at android.view.LayoutInflater.rInflate(LayoutInflater.java:761)
05-25 23:43:17.204 17042-17042 <XX> I/ <YY>: at android.view.LayoutInflater.inflate(LayoutInflater.java:498)
05-25 23:43:17.204 17042-17042 <XX> I/ <YY>: at android.view.LayoutInflater.inflate(LayoutInflater.java:398)
05-25 23:43:17.204 17042-17042 <XX> I/ <YY>: at android.view.LayoutInflater.inflate(LayoutInflater.java:354)
05-25 23:43:17.204 17042-17042 <XX> I/ <YY>: at android.support.v7.app.AppCompatDelegateImplV7.setContentView(AppCompatDelegateImplV7.java:276)
05-25 23:43:17.204 17042-17042 <XX> I/ <YY>: at android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:136)
05-25 23:43:17.204 17042-17042 <XX> I/ <YY>: at abcpqr.red.MainActivity_1.onCreate(MainActivity_1.java:21)
05-25 23:43:17.204 17042-17042 <XX> I/ <YY>: at android.app.Activity.performCreate(Activity.java:5372)
05-25 23:43:17.204 17042-17042 <XX> I/ <YY>: at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1104)
05-25 23:43:17.204 17042-17042 <XX> I/ <YY>: at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2257)
05-25 23:43:17.204 17042-17042 <XX> I/ <YY>: at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2349)
05-25 23:43:17.204 17042-17042 <XX> I/ <YY>: at android.app.ActivityThread.access$700(ActivityThread.java:159)
05-25 23:43:17.204 17042-17042 <XX> I/ <YY>: at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1316)
05-25 23:43:17.204 17042-17042 <XX> I/ <YY>: at android.os.Handler.dispatchMessage(Handler.java:99)
05-25 23:43:17.204 17042-17042 <XX> I/ <YY>: at android.os.Looper.loop(Looper.java:176)
05-25 23:43:17.204 17042-17042 <XX> I/ <YY>: at android.app.ActivityThread.main(ActivityThread.java:5419)
05-25 23:43:17.204 17042-17042 <XX> I/ <YY>: at java.lang.reflect.Method.invokeNative(Native Method)
05-25 23:43:17.204 17042-17042 <XX> I/ <YY>: at java.lang.reflect.Method.invoke(Method.java:525)
05-25 23:43:17.204 17042-17042 <XX> I/ <YY>: at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1046)
05-25 23:43:17.204 17042-17042 <XX> I/ <YY>: at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:862)
05-25 23:43:17.209 17042-17042 <XX> I/ <YY>: at dalvik.system.NativeStart.main(Native Method)
05-25 23:43:17.209 17042-17042 <XX> D/skia: --- decoder->decode returned false
05-25 23:43:17.209 17042-17042 <XX> D/AndroidRuntime: Shutting down VM
05-25 23:43:17.209 17042-17042 <XX> W/ <YY>: threadid=1: thread exiting with uncaught exception (group=0x41e4e700)
05-25 23:43:17.209 17042-17042 <XX> E/AndroidRuntime: FATAL EXCEPTION: main
java.lang.OutOfMemoryError
at android.graphics.BitmapFactory.nativeDecodeAsset(Native Method)
at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:596)
at android.graphics.BitmapFactory.decodeResourceStream(BitmapFactory.java:444)
at android.graphics.drawable.Drawable.createFromResourceStream(Drawable.java:832)
at android.content.res.Resources.loadDrawable(Resources.java:2988)
at android.content.res.TypedArray.getDrawable(TypedArray.java:602)
at android.view.View.<init>(View.java:3563)
at android.widget.TextView.<init>(TextView.java:881)
at android.widget.Button.<init>(Button.java:108)
at android.support.v7.widget.AppCompatButton.<init>(AppCompatButton.java:62)
at android.support.v7.widget.AppCompatButton.<init>(AppCompatButton.java:58)
at android.support.v7.app.AppCompatViewInflater.createView(AppCompatViewInflater.java:105)
at android.support.v7.app.AppCompatDelegateImplV7.createView(AppCompatDelegateImplV7.java:972)
at android.support.v7.app.AppCompatDelegateImplV7.onCreateView(AppCompatDelegateImplV7.java:1030)
at android.support.v4.view.LayoutInflaterCompatHC$FactoryWrapperHC.onCreateView(LayoutInflaterCompatHC.java:44)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:690)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:761)
at android.view.LayoutInflater.inflate(LayoutInflater.java:498)
at android.view.LayoutInflater.inflate(LayoutInflater.java:398)
at android.view.LayoutInflater.inflate(LayoutInflater.java:354)
at android.support.v7.app.AppCompatDelegateImplV7.setContentView(AppCompatDelegateImplV7.java:276)
at android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:136)
at abcpqr.red.MainActivity_1.onCreate(MainActivity_1.java:21)
at android.app.Activity.performCreate(Activity.java:5372)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1104)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2257)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2349)
at android.app.ActivityThread.access$700(ActivityThread.java:159)
t android.app.ActivityThread$H.handleMessage(ActivityThread.java:1316)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:176)
at android.app.ActivityThread.main(ActivityThread.java:5419)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:525)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1046)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:862)
at dalvik.system.NativeStart.main(Native Method)
05-25 23:43:25.074 17042-17042 <XX> I/Process: Sending signal. PID: 17042 SIG: 9
这是我的xml的要点:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:ads="http://schemas.android.com/apk/res-auto"
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="xxx.MainActivity">
<Button
android:layout_width="103dp"
android:layout_height="82dp"
android:id="@+id/btn_inside"
android:layout_below="@+id/LL_subheading1"
android:layout_centerHorizontal="true"
android:background="@mipmap/pic_small_inside"
style="@style/Widget.AppCompat.Button.Borderless"
android:clickable="true"/>
<Button
android:layout_width="103dp"
android:layout_height="82dp"
android:id="@+id/btn_on_the_side1"
android:layout_below="@+id/LL_subheading2"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:background="@mipmap/pic_small_on_the_side1"
style="@style/Base.Widget.AppCompat.Button.Borderless"
android:clickable="true"/>
<Button
android:layout_width="103dp"
android:layout_height="82dp"
android:id="@+id/btn_on_the_side3"
android:layout_below="@+id/LL_subheading2"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:background="@mipmap/pic_small_on_the_side3"
style="@style/Base.Widget.AppCompat.Button.Borderless"
android:clickable="true"/>
<Button
android:layout_width="103dp"
android:layout_height="82dp"
android:id="@+id/btn_on_the_side2"
android:layout_alignTop="@+id/btn_on_the_side1"
android:layout_centerHorizontal="true"
android:background="@mipmap/pic_small_on_the_side2"
style="@style/Base.Widget.AppCompat.Button.Borderless"
android:clickable="true"/>
<Button
android:layout_width="103dp"
android:layout_height="82dp"
android:id="@+id/btn_outside1"
android:layout_below="@+id/LL_subheading3"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:background="@mipmap/pic_small_outside1"
style="@style/Base.Widget.AppCompat.Button.Borderless"
android:clickable="true"/>
<Button
android:layout_width="103dp"
android:layout_height="82dp"
android:id="@+id/btn_outside2"
android:layout_below="@+id/LL_subheading3"
android:layout_centerHorizontal="true"
android:background="@mipmap/pic_small_outside2"
style="@style/Base.Widget.AppCompat.Button.Borderless"
android:clickable="true"/>
<Button
android:layout_width="103dp"
android:layout_height="82dp"
android:id="@+id/btn_outside3"
android:layout_below="@+id/LL_subheading3"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:background="@mipmap/pic_small_outside3"
style="@style/Base.Widget.AppCompat.Button.Borderless"
android:clickable="true"/>
</RelativeLayout>
答案 0 :(得分:0)
请使用以下代码并检查
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
private Button btn1, btn2, btn3, btn4, btn5, btn6, btn7;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btn1 = (Button) findViewById(R.id.btn_1);
btn2 = (Button) findViewById(R.id.btn_2);
btn3 = (Button) findViewById(R.id.btn_3);
btn4 = (Button) findViewById(R.id.btn_4);
btn5 = (Button) findViewById(R.id.btn_5);
btn6 = (Button) findViewById(R.id.btn_6);
btn7 = (Button) findViewById(R.id.btn_7);
btn1.setOnClickListener(this);
btn2.setOnClickListener(this);
btn3.setOnClickListener(this);
btn4.setOnClickListener(this);
btn5.setOnClickListener(this);
btn6.setOnClickListener(this);
btn7.setOnClickListener(this);
}
@Override
public void onClick(View v) {
switch(v.getId()) {
case R.id.btn_1:
Intent intent_1 = new Intent(MainActivity.this, MainActivity_1.class);
startActivity(intent_1);
break;
case R.id.btn_2:
Intent intent_2 = new Intent(MainActivity.this, MainActivity_2.class);
startActivity(intent_2);
break;
...
case R.id.btn_7:
Intent intent_7 = new Intent(MainActivity.this, MainActivity_7.class);
startActivity(intent_7);
break;
}
}
}
答案 1 :(得分:0)
首先,我来给你一些建议。重新排列代码,以避免在以下方式上浪费创建屏幕(OnCreate方法)的处理时间:
设计(xml)按钮应具有onclick属性,如:
<Button android:id="@+id/button"
android:onClick="onClickButton"
android:text="Awesome" />
然后为每个按钮创建一个方法:
public void onClickButton(View v){
// Code to run after button click
}
此方法需要返回void并将视图作为参数。
为了帮助您解决错误,我需要安装在android监视器上的logcat。您只需将手机连接到电脑并从开发环境运行应用程序。
答案 2 :(得分:0)
您使用大图像,设备内存不足。请检查您的图片大小。并分享您的Xml布局
答案 3 :(得分:0)
您正在获得OutOfMemoryException
。见到你的
在logcatjava.lang.OutOfMemoryError
android.graphics.BitmapFactory.
nativeDecodeAsset(Native Method)
问题是您正在创建按钮上的新活动点击活动很重,而且您正在从资产文件夹中加载这些活动中的位图。其中包含大量位图的新活动实例导致错误。
解决方案:用片段替换活动,并使用Glide或Picasso加载图片。
注意:从现在开始如果App崩溃尝试在logcat中搜索“Fatal”,该日志将会有错误说明。
答案 4 :(得分:0)
您获得的错误可能是由于
1.使用大尺寸图像
2.在从一个活动转移到另一个活动之后,你应该总是完成以前的活动以释放一些记忆。
3.当您开始在应用中使用更多数量的图片时,您应该在Android Manifest文件中设置android:largeHeap="true"
以便申请