2016-12-30 53 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

相關問題