-1

你好我的朋友),我有我的課下一個錯誤......錯誤:無法指非final的變量名在不同的方法定義的內部類中

public class AlertDlg { 
public static void AlertShow(final Activity activity, EditText name, EditText track) 
{ 
    LayoutInflater inflater = activity.getLayoutInflater(); 
    View alertLayout = inflater.inflate(R.layout.layout_custom_dialog, null); 
    name = (EditText) alertLayout.findViewById(R.id.et_name); 
    track = (EditText) alertLayout.findViewById(R.id.et_track); 
    AlertDialog.Builder alert = new AlertDialog.Builder(activity); 
    alert.setTitle("Add track number"); 
    //alert.setIcon(R.drawable.plus); 
    alert.setView(alertLayout); 
    alert.setCancelable(false); 
    alert.setNegativeButton("Cancel", new DialogInterface.OnClickListener() { 

     @Override 
     public void onClick(DialogInterface dialog, int which) { 
      Toast.makeText(activity.getBaseContext(), "Abort", Toast.LENGTH_SHORT).show(); 
     } 
    }); 
    alert.setPositiveButton("Add", new DialogInterface.OnClickListener() { 

     @Override 
     public void onClick(DialogInterface dialog, int which) { 
    ERROR HERE--->String sname = name.getText().toString(); 
      //String track = etPassword.getText().toString(); 
      Toast.makeText(activity.getBaseContext(), "Name: " + sname +" "+ " Track: " , Toast.LENGTH_SHORT).show(); 
     } 
    }); 
    AlertDialog dialog = alert.create(); 
    dialog.show(); 
}} 

錯誤:無法指在不同的方法中定義的內部類內的非最終變量名。我試圖從該數據的EditText。但得到這個錯誤...

調用一個方法:

AlertDlg.AlertShow(MainActivity.this,name,track); 
+0

讓'name'爲''喜歡公共靜態無效AlertShow(最終活動活動,最終的EditText名,最終的EditText軌道)' –

+0

有趣的final'是他alredy爲活動做了 – Blackbelt

+0

然後我在這裏得到一個錯誤:name = (EditText) alertLayout.findViewById(R.id.et_name);錯誤:The final local variable name cannot be assigned. It must be blank and not using a compound assignment Yaroslav

回答

0

取出內部類,使用它就像你的活動中的功能。

public void AlertShow() 
{ 
    Context mContext = this; 
    LayoutInflater inflater = getLayoutInflater(); 
    View alertLayout = inflater.inflate(R.layout.layout_custom_dialog, null); 
    final EditText name = (EditText) alertLayout.findViewById(R.id.et_name); 
    //final EditText track = (EditText) alertLayout.findViewById(R.id.et_track); 
    AlertDialog.Builder alert = new AlertDialog.Builder(this); 
    alert.setTitle("Add track number"); 
    //alert.setIcon(R.drawable.plus); 
    alert.setView(alertLayout); 
    alert.setCancelable(false); 
    alert.setNegativeButton("Cancel", new DialogInterface.OnClickListener() { 

     @Override 
     public void onClick(DialogInterface dialog, int which) { 
      Toast.makeText(mContext, "Abort", Toast.LENGTH_SHORT).show(); 
     } 
    }); 
    alert.setPositiveButton("Add", new DialogInterface.OnClickListener() { 

     @Override 
     public void onClick(DialogInterface dialog, int which) { 
      String sname = name.getText().toString(); 
      //String track = etPassword.getText().toString(); 
      Toast.makeText(mContext, "Name: " + sname +" "+ " Track: " , Toast.LENGTH_SHORT).show(); 
     } 
    }); 
    AlertDialog dialog = alert.create(); 
    dialog.show(); 
} 

然後調用它:

AlertShow(); 
+0

Noo ...我在另一個類中調用這個方法.. – Yaroslav

+0

那麼爲什麼你不在該類的內部複製你稱之爲的方法? – Alqueraf

+0

使我的代碼更易於MainActivity) – Yaroslav

相關問題