0

私のプロジェクトでは、いくつかのonClickリスナーがあり、それらはすべて問題ありませんが、コードを削除して再入力して保存すると、コードが見つからずエラーが発生します。 eclipse と後でカムバックすると、変数を再度解決することはできません。

これは、コードで解決できない場所です。

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;
}

}
4

1 に答える 1

1

これは、Eclipse で内部クラス (またはインターフェース) をインポートする際の厄介な問題です。あなたがしなければならないことは、代わりに:

new OnClickListener()

書く:

new View.OnClickListener()

そして、それandroid.view.Viewがインポートされていることを確認してください。

于 2013-07-13T05:36:52.580 に答える