简体   繁体   English

当第二个活动包含图像按钮时,意图不起作用

[英]intent not working when second activity contained image button

so here's what i need : first page -> linked to second page (with several image button) -> linked to different third page. 所以这就是我需要的:第一页->链接到第二页(带有多个图像按钮)->链接到不同的第三页。

now i have tried the "first page -> linked to second page" stage. 现在我已经尝试了“第一页->链接到第二页”阶段。

activity_main.xml : 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">
<TextView
    android:id="@+id/textView1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_centerHorizontal="true"
    android:layout_centerVertical="true"
    android:textSize="20sp"
    android:text="This is first activity" />
<Button
    android:id="@+id/button1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@+id/textView1"
    android:layout_centerHorizontal="true"
    android:layout_marginTop="14dp"
    android:text="Move to second activity" />
</RelativeLayout>

MainActivity.java : MainActivity.java:

package com.example.acer.myapplication;

import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

public class MainActivity extends Activity {
  Button buttoon;
  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    final Intent move= new Intent(this, ActivityTwo.class);
    buttoon=(Button) findViewById(R.id.button1);
    buttoon.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            startActivity(move);
        }
    });

  }
}

ActivityTwo.java : ActivityTwo.java:

package com.example.acer.myapplication;

import android.os.Bundle;
import android.app.Activity;
public class ActivityTwo extends Activity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_two);
    }
}

it is actually working when the second page DID NOT contained any image button. 当第二页DID不包含任何图像按钮时,它实际上正在工作。

activity_two.xml (WORKING) : activity_two.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">
<TextView
    android:id="@+id/textView1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentTop="true"
    android:layout_centerHorizontal="true"
    android:layout_marginTop="92dp"
    android:text="Second activity"
    android:textSize="20sp" />
</RelativeLayout>

button without any image is also WORKING : 没有任何图像的按钮也正在工作:

<Button
    android:id="@+id/textView1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Second activity" />

but when you added image, it's just not working. 但是当您添加图片时,它就无法正常工作。

using button with background image (NOT WORKING): 使用带有背景图片的按钮(不工作):

<Button
    android:id="@+id/textView1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Second activity"
    android:background="@drawable/image"/>

using image button (NOT WORKING): 使用图像按钮(不工作):

<ImageButton
    android:id="@+id/textView1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Second activity"
    android:src="@drawable/image"/>

and i really need for the second page to have image button. 我真的需要第二页上有图像按钮。 please enlighten me where am i wrong or is there another trick. 请告诉我我在哪里错了或者还有其他窍门。

ps : when i say not working i mean it build smoothly but it just forced closed in the android phone when the intent is called. ps:当我说不工作时,我的意思是它构建平稳,但是当调用intent时,它被强制关闭在android手机中。 so no logcat entries. 因此没有logcat条目。

Your image is probably too large and you are getting an OOME (Out Of Memory Error) 您的图片可能太大,并且收到OOME(内存不足错误)

Force close after first run 第一次运行后强制关闭

Decrease the size of your image. 减小图像的尺寸。 The "default" heap size is usually 24MB ( https://code.google.com/p/android/issues/detail?id=14869 ) which is really easy to burn past. “默认”堆大小通常为24MB( https://code.google.com/p/android/issues/detail?id=14869 ),这真的很容易燃烧。

You probably need to increase the heap size on the device for your app: 您可能需要为应用程序增加设备上的堆大小:

How to increase heap size of an android application? 如何增加Android应用程序的堆大小?

EDIT: 编辑:

I should add that you should make every effort to reduce image sizes before automatically increases the heap size. 我应该补充一点,您应该在自动增加堆大小之前尽一切努力减小图像大小。 Otherwise, you will run out of heap later and then have a huge mess cleaning up all the image files that are too large. 否则,稍后您将用完堆,然后将清理所有太大的图像文件弄得一团糟。

Resize your image, try 100dp x 100dp and put it in your drawable folder. 调整图像大小,尝试100dp x 100dp并将其放入可绘制文件夹中。 Increasing heap size is not recommendable. 不建议增加堆大小。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM