1

我的这部分代码有问题

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

我可以知道有什么问题吗?

4

5 回答 5

1

改变:

insertButton.setOnClickListener(new OnClickListener();

反而:

insertButton.setOnClickListener(new View.OnClickListener();

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

反而:

 insertButton = (Button) findViewById(R.id.button1);
于 2013-02-14T08:06:26.530 回答
1

尝试

insertButton.setOnClickListener(new View.OnClickListener()
{
        public void onClick(View v)
        {
        }
}
于 2013-02-14T08:07:42.140 回答
1

在您的活动中导入以下行

import android.view.View.OnClickListener;
于 2013-02-14T08:15:20.200 回答
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();
            }
        }
    });

上述语法总是对我有用。

于 2013-02-14T08:27:29.120 回答
0

使用 findViewById方法而不是findByViewId.

insertButton = (Button) findViewById(R.id.button1);  
insertButton.setOnClickListener(new View.OnClickListener() {
    public void onClick(View v)
    {
       //Code for action listener.
    }
 });
于 2013-02-14T08:05:36.500 回答