【问题标题】:Failure To allocate (blank) byte allocation with (blank) bytes free of 57mb until OOM未能分配(空白)字节分配(空白)字节,直到 OOM
【发布时间】:2016-12-30 20:51:27
【问题描述】:

我正在尝试切换活动。每个活动只有一个图像视图,所以我发现没有必要显示 xml。这是一个相对简单的程序。根据 IF-STATEMENT,程序应切换到其适当的视图。所有图片均为 72dpi jpegs。

package app.com.example.android.chancetheoracle;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Toast;

import java.util.Random;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        Toast.makeText(this, "Think Of Your Question, Then Tap Anywhere.",Toast.LENGTH_LONG).show();
    }

    public void onClick(View view){
        Random answer = new Random();

        int ask = answer.nextInt(6) + 1;

        if (ask == 1){
            Intent intent = new Intent(this, redThree.class);
            startActivity(intent);
            Toast.makeText(this, "2/5! Click Anywhere To Ask Again.",Toast.LENGTH_SHORT).show();
        }
        else if (ask == 2){
            Intent intent = new Intent(this, greenThree.class);
            startActivity(intent);
            Toast.makeText(this, "3/5! Click Anywhere To Ask Again.",Toast.LENGTH_SHORT).show();
        }
        else if (ask == 3){
            Intent intent = new Intent(this, greenFour.class);
            startActivity(intent);
            Toast.makeText(this, "4/5! Click Anywhere To Ask Again.",Toast.LENGTH_SHORT).show();
        }
        else if (ask == 4){
            Intent intent = new Intent(this, redFour.class);
            startActivity(intent);
            Toast.makeText(this, "1/5! Click Anywhere To Ask Again.",Toast.LENGTH_SHORT).show();
        }
        else if (ask == 5){
            Intent intent = new Intent(this, redFive.class);
            startActivity(intent);
            Toast.makeText(this, "0/5! Click Anywhere To Ask Again.",Toast.LENGTH_SHORT).show();
        }
        else if (ask == 6){
            Intent intent = new Intent(this, greenFive.class);
            startActivity(intent);
            Toast.makeText(this, "5/5! Click Anywhere To Ask Again.",Toast.LENGTH_SHORT).show();
        }
        else {
            Toast.makeText(this, "Think Of Your Question, Then Tap Anywhere.",Toast.LENGTH_LONG).show();
        }
    }

    public static class greenThree extends AppCompatActivity {

        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_green_three);
        }
    }

    public static class redThree extends AppCompatActivity {

        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_red_three);
        }
    }

    public static class greenFour extends AppCompatActivity {

        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_green_four);
        }
    }

    public static class redFour extends AppCompatActivity {

        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_red_four);
        }
    }

    public static class greenFive extends AppCompatActivity {

        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_green_five);
        }
    }

    public static class redFive extends AppCompatActivity {

        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_red_five);
        }
    }
}

【问题讨论】:

  • 图片明显太大(分辨率)

标签: java android android-bitmap developer-tools


【解决方案1】:

您尚未将 onClick 侦听器分配给任何内容。它不会自己开火。

final Button button = (Button) findViewById(R.id.button_id);
button.setOnClickListener(new View.OnClickListener() {
    public void onClick(View v) {
        // Perform action on click
    }
});

关于内存问题,参考这个帖子: Android:java.lang.OutOfMemoryError: Failed to allocate a 23970828 byte allocation with 2097152 free bytes and 2MB until OOM

https://developer.android.com/reference/android/widget/Button.html

【讨论】:

  • 你还没有分配 onClick 监听器 这就是为什么他有 OOM...有趣:-) ...也可以通过布局 XML 完成...
  • @Selvin 他描述的至少部分问题是“基于 IF 语句,程序应该切换到其适当的视图。”他特意说 XML 文件中除了图像之外什么都没有。
猜你喜欢
  • 1970-01-01
  • 2015-11-21
  • 2018-10-02
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多