0

我的“findViewById”有问题,它发生在线路之间。

 new AlertDialog.Builder(DetailShareHoldingActivity.this).setTitle("Köp " + Portfolio.getPortfolio().getShareHolding(positionList).getName() +  " Aktier").setView(addView).setPositiveButton("Köp", new DialogInterface.OnClickListener() {

和线

public void onClick(DialogInterface dialog, int whichButton) {

如果我将线放在其他任何可以正常工作的地方,问题是我必须将线放在我拥有的地方,所以我不知道为什么它会抱怨?这是我的 OnclickListener

buyButton = (Button)findViewById(R.id.buyButton);
    buyButton.setOnClickListener(new OnClickListener() {
        public void onClick(View view) {
        EditText tempEditText;    
        final View addView = getLayoutInflater().inflate(R.layout.shop_exsisting_share, null);
        new AlertDialog.Builder(DetailShareHoldingActivity.this).setTitle("Köp " + Portfolio.getPortfolio().getShareHolding(positionList).getName() +  " Aktier").setView(addView).setPositiveButton("Köp", new DialogInterface.OnClickListener() {
            tempEditText = (EditText)findViewById(R.id.nrOfSharesInPortfolioEdit);//Complains on findViewById "Return type for the method is missing" and I have to have the code exactly here so that it is visible when the dialog is created and not when the button is pressed.
            tempEditText= (EditText) findViewById(R.id.nrOfSharesInPortfolioEdit);
            tempEditText.setText(Portfolio.getPortfolio().getShareHolding(positionList).getNrOfSharesInPortfolio());
            System.out.println(Portfolio.getPortfolio().getShareHolding(positionList).getNrOfSharesInPortfolio());
            tempEditText= (EditText) findViewById(R.id.currentCourseEdit);
            tempEditText.setText(Double.toString(Portfolio.getPortfolio().getShareHolding(positionList).getCurrentRate()));

            public void onClick(DialogInterface dialog, int whichButton) {
                System.out.println("Button pressed köp ");
            }
        }).setNegativeButton("Avbryt", null).show();
        }
    });
4

1 回答 1

2

是的,你当然有问题。您正在尝试在类定义中定义已经声明的变量......这是不允许的。

您的评论说:“我必须将代码准确地放在此处,以便在创建对话框时可见,而不是在按下按钮时可见。 ”。这没有任何意义;你需要有你的代码在一个实际的方法。

将您的定义移到类定义之外并改为方法。

tempEditText = (EditText) addView.findViewById(R.id.nrOfSharesInPortfolioEdit);
tempEditText = (EditText) addView.findViewById(R.id.nrOfSharesInPortfolioEdit);
tempEditText.setText(Portfolio.getPortfolio().getShareHolding(positionList).getNrOfSharesInPortfolio());
System.out.println(Portfolio.getPortfolio().getShareHolding(positionList).getNrOfSharesInPortfolio());
tempEditText = (EditText) addView.findViewById(R.id.currentCourseEdit);
tempEditText.setText(Double.toString(Portfolio.getPortfolio().getShareHolding(positionList).getCurrentRate()));

new AlertDialog.Builder(DetailShareHoldingActivity.this)
    .setTitle("Köp " + Portfolio.getPortfolio().getShareHolding(positionList).getName() +  " Aktier")
    .setView(addView)
    .setPositiveButton("Köp", new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int whichButton) {
            System.out.println("Button pressed köp ");
        }
    })
    .setNegativeButton("Avbryt", null)
    .show();
于 2012-12-05T19:35:38.860 回答