2016-12-30 51 views
0

我试图切换活动。每个活动只有一个图像视图,所以我没有发现它需要显示xml。这是一个相对简单的程序。根据IF-STATEMENT,程序应切换到适当的视图。所有图像都是72dpi jpegs。失败使用(空白)字节分配(空白)字节分配免费的57MB,直到OOM

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); 
 
     } 
 
    } 
 
}

+0

显然图像过大(分辨率) – Selvin

回答

-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

+0

*您还没有分配的onClick监听器*,这就是为什么他有OOM ...有趣的:-) ...也可以通过布局XML来完成... – Selvin

+0

@Selvin他描述的问题至少有一部分是“基于IF-STATEMENT,程序应该切换到相应的视图“。他竭尽全力说XML文件除了图像外没有任何内容。 –

+0

他没有写过那部分不能正常工作...... – Selvin

相关问题