setOnClickListener和findViewbyId

时间:2013-02-14 08:02:17

标签: android

我的代码的这部分出现了问题

insertButton = (Button) findByViewId(R.id.button1);  
insertButton.setOnClickListener(new OnClickListener();

它一直说findByViewId的方法未定义,setOnClickListener的方法不适用且OnClickListener无法解析 这是我的完整代码

package edu.nyp.project;

import android.os.Bundle;
import android.app.Activity;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

public class AddData extends Activity {
    Button insertButton = null;
    EditText shopText= null;
EditText dealText= null;
EditText locationText= null;
EditText websiteText= null;
EditText categoryText= null;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.adddata);

    insertButton = (Button) findByViewId(R.id.button1);
    insertButton.setOnClickListener(new OnClickListener()
    {
        public void onClick(View v)
        {
            DBAdapter dbAdapter = new
                    DBAdapter(getApplicationContext());
            try{
                dbAdapter.open();

                String shop = shopText.getText().toString(); 
                String deal = dealText.getText().toString(); 
                String location = locationText.getText().toString(); 
                String website = websiteText.getText().toString(); 
                String category = categoryText.getText().toString(); 
            }
            catch(Exception e){
                Log.d("Add Data ", e.getMessage());
            }
            finally{
                if (dbAdapter != null)
                    dbAdapter.close();
            }
        }
    });
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.adddata, menu);
    return true;
}
}

我可能知道出了什么问题吗?

5 个答案:

答案 0 :(得分:1)

更改:

insertButton.setOnClickListener(new OnClickListener();

代替:

insertButton.setOnClickListener(new View.OnClickListener();

insertButton = (Button) findByViewId(R.id.button1);

代替:

 insertButton = (Button) findViewById(R.id.button1);

答案 1 :(得分:1)

insertButton.setOnClickListener(new View.OnClickListener()
{
        public void onClick(View v)
        {
        }
}

答案 2 :(得分:1)

在活动中导入以下行

import android.view.View.OnClickListener;

答案 3 :(得分:0)

使用findViewById方法代替findByViewId

insertButton = (Button) findViewById(R.id.button1);  
insertButton.setOnClickListener(new View.OnClickListener() {
    public void onClick(View v)
    {
       //Code for action listener.
    }
 });

答案 4 :(得分:0)

只有我能找到与上述代码不同的是:

insertButton = (Button) findByViewId(R.id.button1);

所以用以下代替:

insertButton = (Button) findViewById(R.id.button1);

剩下的代码很好。我的意思是下面的代码非常好:

 insertButton.setOnClickListener(new OnClickListener()
    {
        public void onClick(View v)
        {
            DBAdapter dbAdapter = new
                    DBAdapter(getApplicationContext());
            try{
                dbAdapter.open();

                String shop = shopText.getText().toString(); 
                String deal = dealText.getText().toString(); 
                String location = locationText.getText().toString(); 
                String website = websiteText.getText().toString(); 
                String category = categoryText.getText().toString(); 
            }
            catch(Exception e){
                Log.d("Add Data ", e.getMessage());
            }
            finally{
                if (dbAdapter != null)
                    dbAdapter.close();
            }
        }
    });

上述语法总是适用于我。