2017-01-14 45 views
-1

我是定義和使用此刻我正在使用的片段文件內的定製OnClickListener(即MyOnClickListener)。在不同的活動中使用相同的'OnClickListener'

飼養編寫代碼我意識到我所需要的相同的偵聽也在另一個片段即碰巧在另一個活動爲好。

因此,我創建的文件MyOnClickListener.java所有複製我用的第一個片段它前面的代碼,但現在我得到以下錯誤:

無法解析法「getActivity()」

無法解析法「getResources()」

注:我讀了計算器一個解決方案可能是隻寫MainActivity.this代替getActivity(),但在我的情況下我需要在中使用兩個不同的活動。我該怎麼辦?

編輯:下面是MyOnClickListener 代碼它只是顯示的圖標的矩陣:

class MyOnClickListener implements View.OnClickListener { 

    private LabeledButton labeledButton; 

    MyOnClickListener(LabeledButton labeledButton) { 
    super(); 
    this.labeledButton = labeledButton; 
} 

@Override 
public void onClick(View view) { 
    Button iconButton = (Button) view; 
    AlertDialog.Builder builder = new AlertDialog.Builder(getActivity()); // ERROR HERE 
    LayoutInflater layoutInflater = getActivity().getLayoutInflater(); // ERROR HERE 
    final View viewLayout = layoutInflater.inflate(R.layout.dialog_matrix_icons, null); 
    builder.setView(viewLayout); 
    builder.setNegativeButton(android.R.string.cancel, new DialogInterface.OnClickListener() { 
     public void onClick(DialogInterface dialog, int id) { 
      // Do nothing 
     } 
    }); 
    final AlertDialog alertDialog = builder.create(); 

    // Set-up listeners of icon button 
    Button imageButtons[][] = new Button[3][3]; 
    int i; 
    int j; 
    for (i = 0; i < 3; i++) { 
     for (j = 0; j < 3; j++) { 
      Resources res = getResources(); // ERROR HERE 
      TypedArray icons = res.obtainTypedArray(R.array.listIcon); 
      int idDrawable = icons.getResourceId(i + 3 * j, 1); // ERROR HERE 
      icons.recycle(); 
      LinearLayout grid = (LinearLayout) viewLayout; 
      LinearLayout row = (LinearLayout) grid.getChildAt(i); 
      imageButtons[i][j] = (Button) row.getChildAt(j); // Retrieve the right image in the grid 
      imageButtons[i][j].setBackgroundResource(idDrawable); 
      String nameIcon = getResources().getResourceEntryName(idDrawable); // ERROR HERE 
      ImageOnClickListener imageOnClickListener = new ImageOnClickListener(iconButton, alertDialog, idDrawable, nameIcon, labeledButton); 
      imageButtons[i][j].setOnClickListener(imageOnClickListener); 
     } 
    } 
    alertDialog.show(); 
} 

}

+0

顯示您的代碼,請 –

+0

@IntelliJAmiya完成! – Robb1

回答

2

與構造函數替換此

MyOnClickListener(LabeledButton labeledButton,Context context) { 
    super(); 
    this.labeledButton = labeledButton; 
    this.context = context; 
} 

確保您創建MyOnClickListenerClass內上下文變量如果不是你將在構造line.Then可以代替所有的getActivity()context發現錯誤。

當你initiallize您MyOnClickListener確保通過上下文參數

爲了您的吹氣使用此

LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE) 
+0

謝謝,它在任何地方都能正常工作,但在這裏:'LayoutInflater layoutInflater = context.getLayoutInflater();'。我究竟做錯了什麼? – Robb1

+1

查看更新代碼。因爲你的片段通常使用context.getLayourInflater已經擁有了它自己的inflater,所以你不需要初始化你的inflater,但是現在你使用獨立的類來初始化inflater以使其工作。 –

0

我的計算器是一種解決方案可能是隻寫 閱讀MainActivity.this代替getActivity(),但在我的情況下,我需要在兩個不同的活動中使用相同的 。我該怎麼辦?

如果你把OnClickListener在自己編制的統一,你不能whawt 你#2閱讀使用。 MainActivity.this,指的是MainActivity的具體和當前實例(請查看關鍵字this在java中的含義)。如果你需要一個上下文,你可以通過參數onClick callaback,View view來獲取它。更多here

相關問題