2017-06-01 62 views
0

我一直在開發這款Android遊戲,並且想知道爲什麼每當我添加此TextView時,我的應用程序開始變得無響應。就像,我的一些按鈕不會按,而其他按鈕,等等。然後,當我從線性佈局中刪除TextView時,一切都恢復正常。請幫忙!當添加TextView時,Android應用程序變慢或變爲無響應

MainActivity

public class MainActivity extends AppCompatActivity { 

boolean started = false; 
boolean place; 
boolean[] tiles = new boolean[60]; 
int prevId = 0; 
int times = 0; 
ArrayList<Integer> IDs = new ArrayList<>(); 

Typeface face; 
TextView num; 

@Override 
protected void onCreate(Bundle savedInstanceState) 
{ 
    super.onCreate(savedInstanceState); 

    requestWindowFeature(Window.FEATURE_NO_TITLE); 
    getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, 
      WindowManager.LayoutParams.FLAG_FULLSCREEN); 

    setContentView(R.layout.activity_main); 

    setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT); 

    num = (TextView) findViewById(R.id.count); 

    face = Typeface.createFromAsset(getAssets(),"fonts/ZonaPro-Bold.otf"); 

    num.setTypeface(face); 
} 

public void press(View view) 
{ 
    place = false; 
    int id = view.getId() % 2131427400; 
    String test = "" + id; 

    ImageView img = (ImageView) view; 

    Log.d("TEST", test); 

    *****HERE, I HAVE LOTS OF LOGIC, AND IF-STATEMENTS INCLUDING SOME UNIMPORTANT VARIABLES***** 

    int abs = Math.abs(Math.abs(id - prevId)); 

    if(place && (abs == 1 || abs == 8) && tiles[id] == false && !(img.getTag().toString().equals("bound"))) 
    { 
     img.setImageResource(R.drawable.blue_sphere); 
     tiles[id] = true; 
     prevId = id; 
     times++; 
     IDs.add(id); 
     num.setText("" + times); 
    } 
} 

public void start(View view) 
{ 
    ImageView v = (ImageView) view; 

    if(!started) 
    { 
     started = true; 
     v.setImageResource(R.drawable.blue_sphere); 
     tiles[22] = true; 
     prevId = 22; 
     IDs.add(22); 
     num = (TextView) findViewById(R.id.count); 
     num.setText("0"); 
    } 
} 

public void undo(View view) 
{ 

    for(int ID : IDs) 
    { 
     ImageView re = (ImageView) findViewById(2131427400 + ID); 
     Log.d("TAG", re.getTag().toString()); 

     if(re.getTag().toString().equals("tile")) 
      re.setImageResource(R.drawable.gray_block); 
     else if(re.getTag().toString().equals("start")) 
     { 
      re.setImageResource(R.drawable.start_tile); 
      started = false; 
     } 
     else if(re.getTag().toString().equals("bound")) 
     { 
      re.setImageResource(R.drawable.newer_x); 
     } 
     else if(re.getTag().toString().equals("goal")) 
     { 
      re.setImageResource(R.drawable.new_goal); 
     } 
    } 

    Arrays.fill(tiles, false); 
    times = 0; 
} 
} 

activity_main.xml中(所有這些佈局都較大,水平線性佈局內)

<!-- 1 --> 
<LinearLayout 
    android:layout_width="0dp" 
    android:layout_height="fill_parent" 
    android:layout_weight="1" 
    android:orientation="vertical" 
    android:weightSum="9"> 

    <ImageView 
     android:layout_width="fill_parent" 
     android:layout_height="0dp" 
     android:id="@+id/_11" 
     app:srcCompat="@drawable/gray_block" 
     android:layout_weight="1" 
     android:onClick="press" 
     android:tag="tile" /> 

    <ImageView 
     android:layout_width="fill_parent" 
     android:layout_height="0dp" 
     android:id="@+id/_12" 
     app:srcCompat="@drawable/gray_block" 
     android:layout_weight="1" 
     android:onClick="press" 
     android:tag="tile"/> 

    <ImageView 
     android:layout_width="fill_parent" 
     android:layout_height="0dp" 
     android:id="@+id/_13" 
     app:srcCompat="@drawable/gray_block" 
     android:layout_weight="1" 
     android:onClick="press" 
     android:tag="tile"/> 

    <ImageView 
     android:layout_width="fill_parent" 
     android:layout_height="0dp" 
     android:id="@+id/_14" 
     app:srcCompat="@drawable/newer_x" 
     android:layout_weight="1" 
     android:onClick="press" 
     android:tag="bound"/> 

    <ImageView 
     android:layout_width="fill_parent" 
     android:layout_height="0dp" 
     android:id="@+id/_15" 
     app:srcCompat="@drawable/gray_block" 
     android:layout_weight="1" 
     android:onClick="press" 
     android:tag="tile"/> 

    <ImageView 
     android:layout_width="fill_parent" 
     android:layout_height="0dp" 
     android:id="@+id/_16" 
     app:srcCompat="@drawable/gray_block" 
     android:layout_weight="1" 
     android:onClick="press" 
     android:tag="tile"/> 

    <ImageView 
     android:layout_width="fill_parent" 
     android:layout_height="0dp" 
     android:id="@+id/_17" 
     app:srcCompat="@drawable/gray_block" 
     android:layout_weight="1" 
     android:onClick="press" 
     android:tag="tile"/> 

    <ImageView 
     android:layout_width="fill_parent" 
     android:layout_height="0dp" 
     android:id="@+id/_18" 
     app:srcCompat="@drawable/start_tile" 
     android:layout_weight="1" 
     android:onClick="start" 
     android:tag="start" /> 

****this is the text view I'm having problems with**** 

    <TextView 
     android:layout_width="fill_parent" 
     android:layout_height="0dp" 
     android:id="@+id/count" 
     android:layout_weight="1" 
     android:textColor="@android:color/black" 
     android:gravity="center" 
     android:textSize="30sp" /> 

****************************************************** 

</LinearLayout> 

<!-- 2 --> 
<LinearLayout 
    android:layout_width="0dp" 
    android:layout_height="fill_parent" 
    android:layout_weight="1" 
    android:orientation="vertical" 
    android:weightSum="9"> 

    <ImageView 
     android:layout_width="fill_parent" 
     android:layout_height="0dp" 
     android:id="@+id/_21" 
     app:srcCompat="@drawable/gray_block" 
     android:layout_weight="1" 
     android:onClick="press" 
     android:tag="tile" /> 


    <ImageView 
     android:layout_width="fill_parent" 
     android:layout_height="0dp" 
     android:id="@+id/_22" 
     app:srcCompat="@drawable/gray_block" 
     android:layout_weight="1" 
     android:onClick="press" 
     android:tag="tile"/> 

    <ImageView 
     android:layout_width="fill_parent" 
     android:layout_height="0dp" 
     android:id="@+id/_23" 
     app:srcCompat="@drawable/gray_block" 
     android:layout_weight="1" 
     android:onClick="press" 
     android:tag="tile"/> 

    <ImageView 
     android:layout_width="fill_parent" 
     android:layout_height="0dp" 
     android:id="@+id/_24" 
     app:srcCompat="@drawable/gray_block" 
     android:layout_weight="1" 
     android:onClick="press" 
     android:tag="tile"/> 

    <ImageView 
     android:layout_width="fill_parent" 
     android:layout_height="0dp" 
     android:id="@+id/_25" 
     app:srcCompat="@drawable/gray_block" 
     android:layout_weight="1" 
     android:onClick="press" 
     android:tag="tile"/> 

    <ImageView 
     android:layout_width="fill_parent" 
     android:layout_height="0dp" 
     android:id="@+id/_26" 
     app:srcCompat="@drawable/gray_block" 
     android:layout_weight="1" 
     android:onClick="press" 
     android:tag="tile"/> 

    <ImageView 
     android:layout_width="fill_parent" 
     android:layout_height="0dp" 
     android:id="@+id/_27" 
     app:srcCompat="@drawable/gray_block" 
     android:layout_weight="1" 
     android:onClick="press" 
     android:tag="tile"/> 

    <ImageView 
     android:layout_width="fill_parent" 
     android:layout_height="0dp" 
     android:id="@+id/_28" 
     app:srcCompat="@drawable/gray_block" 
     android:layout_weight="1" 
     android:onClick="press" 
     android:tag="tile"/> 

</LinearLayout> 

<!-- 3 --> 
<LinearLayout 
    android:layout_width="0dp" 
    android:layout_height="fill_parent" 
    android:layout_weight="1" 
    android:orientation="vertical" 
    android:weightSum="9"> 


    <ImageView 
     android:layout_width="fill_parent" 
     android:layout_height="0dp" 
     android:id="@+id/_31" 
     app:srcCompat="@drawable/newer_x" 
     android:layout_weight="1" 
     android:onClick="press" 
     android:tag="bound" /> 

    <ImageView 
     android:layout_width="fill_parent" 
     android:layout_height="0dp" 
     android:id="@+id/_32" 
     app:srcCompat="@drawable/newer_x" 
     android:layout_weight="1" 
     android:onClick="press" 
     android:tag="bound" /> 

    <ImageView 
     android:layout_width="fill_parent" 
     android:layout_height="0dp" 
     android:id="@+id/_33" 
     app:srcCompat="@drawable/newer_x" 
     android:layout_weight="1" 
     android:onClick="press" 
     android:tag="bound" /> 

    <ImageView 
     android:layout_width="fill_parent" 
     android:layout_height="0dp" 
     android:id="@+id/_34" 
     app:srcCompat="@drawable/gray_block" 
     android:layout_weight="1" 
     android:onClick="press" 
     android:tag="tile"/> 

    <ImageView 
     android:layout_width="fill_parent" 
     android:layout_height="0dp" 
     android:id="@+id/_35" 
     app:srcCompat="@drawable/gray_block" 
     android:layout_weight="1" 
     android:onClick="press" 
     android:tag="tile"/> 

    <ImageView 
     android:layout_width="fill_parent" 
     android:layout_height="0dp" 
     android:id="@+id/_36" 
     app:srcCompat="@drawable/gray_block" 
     android:layout_weight="1" 
     android:onClick="press" 
     android:tag="tile"/> 

    <ImageView 
     android:layout_width="fill_parent" 
     android:layout_height="0dp" 
     android:id="@+id/_37" 
     app:srcCompat="@drawable/newer_x" 
     android:layout_weight="1" 
     android:onClick="press" 
     android:tag="bound"/> 

    <ImageView 
     android:layout_width="fill_parent" 
     android:layout_height="0dp" 
     android:id="@+id/_38" 
     app:srcCompat="@drawable/gray_block" 
     android:layout_weight="1" 
     android:onClick="press" 
     android:tag="tile"/> 

</LinearLayout> 

<!-- 4 --> 
<LinearLayout 
    android:layout_width="0dp" 
    android:layout_height="fill_parent" 
    android:layout_weight="1" 
    android:orientation="vertical" 
    android:weightSum="9"> 


    <ImageView 
     android:layout_width="fill_parent" 
     android:layout_height="0dp" 
     android:id="@+id/_41" 
     app:srcCompat="@drawable/gray_block" 
     android:layout_weight="1" 
     android:onClick="press" 
     android:tag="tile" /> 

    <ImageView 
     android:layout_width="fill_parent" 
     android:layout_height="0dp" 
     android:id="@+id/_42" 
     app:srcCompat="@drawable/gray_block" 
     android:layout_weight="1" 
     android:onClick="press" 
     android:tag="tile"/> 

    <ImageView 
     android:layout_width="fill_parent" 
     android:layout_height="0dp" 
     android:id="@+id/_43" 
     app:srcCompat="@drawable/gray_block" 
     android:layout_weight="1" 
     android:onClick="press" 
     android:tag="tile"/> 

    <ImageView 
     android:layout_width="fill_parent" 
     android:layout_height="0dp" 
     android:id="@+id/_44" 
     app:srcCompat="@drawable/gray_block" 
     android:layout_weight="1" 
     android:onClick="press" 
     android:tag="tile"/> 

    <ImageView 
     android:layout_width="fill_parent" 
     android:layout_height="0dp" 
     android:id="@+id/_45" 
     app:srcCompat="@drawable/gray_block" 
     android:layout_weight="1" 
     android:onClick="press" 
     android:tag="tile"/> 

    <ImageView 
     android:layout_width="fill_parent" 
     android:layout_height="0dp" 
     android:id="@+id/_46" 
     app:srcCompat="@drawable/newer_x" 
     android:layout_weight="1" 
     android:onClick="press" 
     android:tag="bound"/> 

    <ImageView 
     android:layout_width="fill_parent" 
     android:layout_height="0dp" 
     android:id="@+id/_47" 
     app:srcCompat="@drawable/gray_block" 
     android:layout_weight="1" 
     android:onClick="press" 
     android:tag="tile"/> 

    <ImageView 
     android:layout_width="fill_parent" 
     android:layout_height="0dp" 
     android:id="@+id/_48" 
     app:srcCompat="@drawable/gray_block" 
     android:layout_weight="1" 
     android:onClick="press" 
     android:tag="tile"/> 

</LinearLayout> 

<!-- 5 --> 
<LinearLayout 
    android:layout_width="0dp" 
    android:layout_height="fill_parent" 
    android:layout_weight="1" 
    android:orientation="vertical" 
    android:weightSum="9"> 


    <ImageView 
     android:layout_width="fill_parent" 
     android:layout_height="0dp" 
     android:id="@+id/_51" 
     app:srcCompat="@drawable/new_goal" 
     android:layout_weight="1" 
     android:onClick="press" 
     android:tag="goal" /> 

    <ImageView 
     android:layout_width="fill_parent" 
     android:layout_height="0dp" 
     android:id="@+id/_52" 
     app:srcCompat="@drawable/gray_block" 
     android:layout_weight="1" 
     android:onClick="press" 
     android:tag="tile"/> 

    <ImageView 
     android:layout_width="fill_parent" 
     android:layout_height="0dp" 
     android:id="@+id/_53" 
     app:srcCompat="@drawable/gray_block" 
     android:layout_weight="1" 
     android:onClick="press" 
     android:tag="tile"/> 

    <ImageView 
     android:layout_width="fill_parent" 
     android:layout_height="0dp" 
     android:id="@+id/_54" 
     app:srcCompat="@drawable/gray_block" 
     android:layout_weight="1" 
     android:onClick="press" 
     android:tag="tile"/> 

    <ImageView 
     android:layout_width="fill_parent" 
     android:layout_height="0dp" 
     android:id="@+id/_55" 
     app:srcCompat="@drawable/gray_block" 
     android:layout_weight="1" 
     android:onClick="press" 
     android:tag="tile"/> 

    <ImageView 
     android:layout_width="fill_parent" 
     android:layout_height="0dp" 
     android:id="@+id/_56" 
     app:srcCompat="@drawable/gray_block" 
     android:layout_weight="1" 
     android:onClick="press" 
     android:tag="tile"/> 

    <ImageView 
     android:layout_width="fill_parent" 
     android:layout_height="0dp" 
     android:id="@+id/_57" 
     app:srcCompat="@drawable/gray_block" 
     android:layout_weight="1" 
     android:onClick="press" 
     android:tag="tile"/> 

    <ImageView 
     android:layout_width="fill_parent" 
     android:layout_height="0dp" 
     android:id="@+id/_58" 
     app:srcCompat="@drawable/gray_block" 
     android:layout_weight="1" 
     android:onClick="press" 
     android:tag="tile"/> 

    <ImageView 
     android:layout_width="match_parent" 
     android:layout_height="0dp" 
     android:id="@+id/undo" 
     app:srcCompat="@drawable/newer_undo" 
     android:layout_weight="1" 
     android:onClick="undo" 
     android:tag="undo"/> 

</LinearLayout> 

我張貼了這麼多的代碼很抱歉,但我有不知道爲什麼會發生這種情況,我希望問題在這些文件中的某處。

回答

0

問題出在您的start(View view) onClick方法中,您正在引用已在onCreate(Bundle obj)中實例化的TextView,請嘗試刪除它。並將fill_parent替換爲match_parent。

0

可能低於線

//TextView num; 

首先取消註釋這個問題。

然後不要再聲明如下行(已的onCreate申報)

num = (TextView) findViewById(R.id.count); 

從在onStart()刪除它。

+0

不幸的是,問題仍在發生。從字面上看,這個TextView存在的地方是這個問題,但我不知道該怎麼做。 – TheFiveHundredYears

相關問題