Eclipse说Android中的onClickListener变量无法解析,但已创建?

时间:2013-07-13 04:57:39

标签: android eclipse onclicklistener

在我的项目中我有几个onClick监听器,并且所有这些都很好,但是一个,我在代码中找不到和错误,如果我删除代码并重新输入并保存它,它没有错误,如果没有错误,如果我关闭,日食和后来回来,变量不能再次解决。

这是代码中无法解决的问题:

Button webButton = (Button) newStockRow.findViewById(R.id.webButton);
webButton.setOnClickListener(getStockFromWebClickListener);

这就是我创造它的方式:

public OnClickListener getStockFromWebClickListener = new OnClickListener(){

    @Override
    public void onClick(View arg0) {

        TableRow tableR = (TableRow) arg0.getParent();

        TextView stock = (TextView) tableR.findViewById(R.id.stockSymbolTextView);

        String stockSymbol = stock.getText().toString();

        String stockURL = getString(R.string.yahoo_stock_url) + stockSymbol;

        Intent getStockWebPage = new Intent(Intent.ACTION_VIEW, Uri.parse(stockURL));

        startActivity(getStockWebPage);

    }
};

完整文件的代码:

package com.gscore.quotestock;

import java.util.Arrays;
import android.net.Uri;
import android.os.Bundle;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.inputmethod.InputMethodManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TableLayout;
import android.widget.TableRow;
import android.widget.TextView;

public class StockQ extends Activity {

public final static String STOCK_SYMBOL = "com.gscore.quotestock.STOCK";

private SharedPreferences stockSymbolsEntered;

private TableLayout stockTableScrollView;

private EditText stockSymbolET;

Button enterStockSymbolButton;
Button deleteStocksButton;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_stock_q);

    // Get user stock list
    stockSymbolsEntered = getSharedPreferences("stockList", MODE_PRIVATE);


    // Initialize UI components
    stockTableScrollView = (TableLayout) findViewById(R.id.stockTableLayout);

    stockSymbolET = (EditText) findViewById(R.id.stockSymbolEditText);

    enterStockSymbolButton= (Button) findViewById(R.id.enterButton);

    deleteStocksButton= (Button) findViewById(R.id.deleteAllButton);


    // Set ClickListeners 
    enterStockSymbolButton.setOnClickListener(enterButtonClickListener);

    deleteStocksButton.setOnClickListener(deleteButtonClickListener);

    updateSavedStockList(null);

}

private void updateSavedStockList(String newStockSymbol){

    String[] stocks = stockSymbolsEntered.getAll().keySet().toArray(new String[0]);

    Arrays.sort(stocks, String.CASE_INSENSITIVE_ORDER);

    if (newStockSymbol != null){

        insertStockInStockTable(newStockSymbol, Arrays.binarySearch(stocks, newStockSymbol));

    } else {

        for(int i = 0; i < stocks.length; i++){

            insertStockInStockTable(stocks[i], i);

        }

    }

}

private void saveStockSymbol(String newStock){

    String isTheStockNew = stockSymbolsEntered.getString(newStock, null);

    SharedPreferences.Editor preferencesEditor = stockSymbolsEntered.edit();

    preferencesEditor.putString(newStock, newStock);
    preferencesEditor.commit();

    if(isTheStockNew == null){

        updateSavedStockList(newStock);

    }
}

private void insertStockInStockTable(String stock, int arrayIndex){

    LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);

    View newStockRow = inflater.inflate(R.layout.stock_quote_row, null);

    TextView newStockTextView = (TextView) newStockRow.findViewById(R.id.stockSymbolTextView);

    newStockTextView.setText(stock);

    Button stockQuoteButton = (Button) newStockRow.findViewById(R.id.stockQuoteButton);
    stockQuoteButton.setOnClickListener(getStockActivityListener);

    Button webButton = (Button) newStockRow.findViewById(R.id.webButton);
    webButton.setOnClickListener(getStockFromWebClickListener);

    stockTableScrollView.addView(newStockRow, arrayIndex);

}

public OnClickListener enterButtonClickListener= new OnClickListener(){

    @Override
    public void onClick(View v) {

        if(stockSymbolET.getText().length() > 0){

            saveStockSymbol(stockSymbolET.getText().toString());

            stockSymbolET.setText("");

            InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);

            imm.hideSoftInputFromWindow(stockSymbolET.getWindowToken(), 0);

        } else {

            AlertDialog.Builder builder = new AlertDialog.Builder(StockQ.this); 

            builder.setTitle(R.string.invalid_stock_symbol);

            builder.setPositiveButton(R.string.ok, null);

            builder.setMessage(R.string.missing_stock_symbol);

            AlertDialog theAlertDialog = builder.create();

            theAlertDialog.show();
        }

    }


};


private void deleteAllStocks(){

    stockTableScrollView.removeAllViews();


}

public OnClickListener deleteButtonClickListener = new OnClickListener(){

    @Override
    public void onClick(View v) {

        deleteAllStocks();

        SharedPreferences.Editor preferencesEditor = stockSymbolsEntered.edit();

        preferencesEditor.clear();
        preferencesEditor.commit();

    }

};


public OnClickListener getStockActivityListener = new OnClickListener(){

    @Override
    public void onClick(View v) {

        TableRow tableR = (TableRow) v.getParent();

        TextView stock = (TextView) tableR.findViewById(R.id.stockSymbolTextView);

        String stockSymbol = stock.getText().toString();

        Intent intent = new Intent(StockQ.this, StockInfoActivity.class);

        intent.putExtra(STOCK_SYMBOL, stockSymbol);

        startActivity(intent);

    }

};

public OnClickListener getStockFromWebClickListener = new OnClickListener(){

    @Override
    public void onClick(View arg0) {

        TableRow tableR = (TableRow) arg0.getParent();

        TextView stock = (TextView) tableR.findViewById(R.id.stockSymbolTextView);

        String stockSymbol = stock.getText().toString();

        String stockURL = getString(R.string.yahoo_stock_url) + stockSymbol;

        Intent getStockWebPage = new Intent(Intent.ACTION_VIEW, Uri.parse(stockURL));

        startActivity(getStockWebPage);

    }
};




@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    //getMenuInflater().inflate(R.menu.stock_q, menu);
    return true;
}

}

1 个答案:

答案 0 :(得分:1)

在Eclipse上导入内部类(或接口)确实是一个恼人的问题。 你要做的不是:

new OnClickListener()

写:

new View.OnClickListener()

并确保导入android.view.View