2014-02-23 18 views
3

所以我有一個12×9的「按鈕」網格稱爲「瓷磚」。 Tile.java擴展了小部件「Button」。「setView必須被調用」 - 自定義類擴展按鈕

我現在有一個問題,雖然我試圖讓按鈕顯示一個召回推按鈕的ID的吐司。

網格動態添加,我希望它保持這種方式。

下面的代碼從GameBoardActivity.java:

這使得「磚」的12x9網格,並增加了一個監聽每個。

public void gridRowButtons(int iterations){ 
    final Tile[] gridSpaces = new Tile[12]; 
    LinearLayout.LayoutParams buttonParams = new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT, 1.0f); 
    for (int i = 0; i < 12; i++) { 
     String idString = String.valueOf(aToI[iterations]) + String.valueOf(i + 1); 
     final int id = getResources().getIdentifier(idString, "id", getPackageName()); 
     gridSpaces[i] = new Tile(getApplicationContext()); 
     gridSpaces[i].setText(""); 
     gridSpaces[i].setHeight(40); 
     gridSpaces[i].setWidth(40); 
     gridSpaces[i].setLayoutParams(buttonParams); 
     gridSpaces[i].setId(id); 
     OnClickListener showID = new OnClickListener(){ 
      public void onClick(View view){     
       TextView text = (TextView) findViewById(R.id.tileIDText); 
       String tileID = getApplicationContext().getResources().getResourceEntryName(id); 
       text.setText(tileID); 
       Tile clickedTile = (Tile) findViewById(id); 
       clickedTile.tileClick(0, tileID); 
      } 
     }; 
     gridSpaces[i].setOnClickListener(showID); 
    } 
    LinearLayout buttonRow = new LinearLayout(getApplicationContext()); 
    buttonRow.setOrientation(LinearLayout.HORIZONTAL); 
    buttonRow.setLayoutParams(buttonParams); 
    LinearLayout boardSpace = (LinearLayout) this.findViewById(R.id.boardLayout); 
    for (int i = 0; i < gridSpaces.length; i++) { 
     buttonRow.addView(gridSpaces[i]); 
    } 
    boardSpace.addView(buttonRow); 
} 

,這裏是上面提到的tileClick方法:

public void tileClick(int action, String tileID) { 
    switch(action) { 
     case 1 : 
      //action 1 
     case 2 : 
      //action 2 
     default : 
      Context context = getContext(); 
      Toast toast = new Toast(context); 
      Toast.makeText(context, tileID, Toast.LENGTH_SHORT); 
      toast.show(); 
    } 
} 

的logcat中顯示以下內容:

02-22 20:45:14.623: E/AndroidRuntime(7868): FATAL EXCEPTION: main 
02-22 20:45:14.623: E/AndroidRuntime(7868): java.lang.RuntimeException: setView must have been called 
02-22 20:45:14.623: E/AndroidRuntime(7868):  at android.widget.Toast.show(Toast.java:103) 
02-22 20:45:14.623: E/AndroidRuntime(7868):  at com.jneal.ecquire.Tile.tileClick(Tile.java:51) 
02-22 20:45:14.623: E/AndroidRuntime(7868):  at com.jneal.ecquire.GameBoardActivity$1.onClick(GameBoardActivity.java:55) 
02-22 20:45:14.623: E/AndroidRuntime(7868):  at android.view.View.performClick(View.java:3627) 
02-22 20:45:14.623: E/AndroidRuntime(7868):  at android.view.View$PerformClick.run(View.java:14329) 
02-22 20:45:14.623: E/AndroidRuntime(7868):  at android.os.Handler.handleCallback(Handler.java:605) 
02-22 20:45:14.623: E/AndroidRuntime(7868):  at android.os.Handler.dispatchMessage(Handler.java:92) 
02-22 20:45:14.623: E/AndroidRuntime(7868):  at android.os.Looper.loop(Looper.java:137) 
02-22 20:45:14.623: E/AndroidRuntime(7868):  at android.app.ActivityThread.main(ActivityThread.java:4511) 
02-22 20:45:14.623: E/AndroidRuntime(7868):  at java.lang.reflect.Method.invokeNative(Native Method) 
02-22 20:45:14.623: E/AndroidRuntime(7868):  at java.lang.reflect.Method.invoke(Method.java:511) 
02-22 20:45:14.623: E/AndroidRuntime(7868):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:976) 
02-22 20:45:14.623: E/AndroidRuntime(7868):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:743) 
02-22 20:45:14.623: E/AndroidRuntime(7868):  at dalvik.system.NativeStart.main(Native Method) 

這是怎麼回事?我有幾乎完全相同的代碼,用於在我的MainActivity.java中顯示敬酒,並且執行沒有問題。 是因爲我擴展了Button,它不知道視圖是什麼? 另外,由於某些原因,Eclipse不會讓我添加setView()。我相信我在這裏有封裝問題,但我對它們是什麼感到困惑。 感謝您的幫助提前。 JRad

回答

10

變化下面的代碼下面的線,給一個人:

 Toast toast = new Toast(context); 
     Toast.makeText(context, tileID, Toast.LENGTH_SHORT); 
     toast.show(); 

改成這樣:

Toast toast = Toast.makeText(context, tileID, Toast.LENGTH_SHORT); 
    toast.show();  

你可以從源代碼中看到,拋出異常僅當mNextView爲空時。 函數「makeText」假設設置它,但它的確如此,但您的原始代碼不會捕獲它構建的Toast的引用。相反,您的原始代碼會創建兩個Toasts,並嘗試「顯示」尚未設置其視圖的代碼。

public void show() { 
    if (mNextView == null) { 
     throw new RuntimeException("setView must have been called"); 
    } 

.... 

public static Toast makeText(Context context, CharSequence text, int duration) { 
    Toast result = new Toast(context); 

    LayoutInflater inflate = (LayoutInflater) 
      context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
    View v = inflate.inflate(com.android.internal.R.layout.transient_notification, null); 
    TextView tv = (TextView)v.findViewById(com.android.internal.R.id.message); 
    tv.setText(text); 

    result.mNextView = v; 
    result.mDuration = duration; 

    return result; 
} 
相關問題