我是Android开发的新手。这是我正在使用的代码:
package com.example.databaseread;
import java.util.List;
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final DatabaseHandler db = new DatabaseHandler(this);
Button but1 = (Button) findViewById(R.id.insert);
but1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
// Inserting known Languages
Log.d("Insert: ", "Inserting ..");
db.add(new values("value1"));
db.add(new values("value2"));
db.add(new values("value3"));
db.add(new values("value4"));
Log.d("Insert", "DataBase Successfully Updated");
}
});
Button but2 = (Button) findViewById(R.id.Read);
but2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
// Reading data Already present in the Database
Log.d("Reading: ", "Reading all data..");
List<values> val = db.getAllvalues();
for (values cn : val) {
String log = "Id: " + cn.getId() + " ,values: "
+ cn.getValue();
Log.d("value: ", log);
}
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
错误:
Multiple markers at this line
- container cannot be resolved or is not a field
- implements
android.view.View.OnClickListener.onClick
来public void onClick(View v)
行。
如果有人能提供帮助,我会很高兴。谢谢!
答案 0 :(得分:0)
检查项目的属性并验证Java Compiler - &gt;编译器合规性级别设置为1.6或更高。
答案 1 :(得分:0)
试试这个
public class MainActivity extends Activity implements onclicklistener {
DatabaseHandler db;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
db = new DatabaseHandler(this);
Button but1 = (Button) findViewById(R.id.insert);
but1.setOnClickListener(this);
Button but2 = (Button) findViewById(R.id.Read);
but2.setOnClickListener(this);
}
@Override
public void onClick(View v) {
if(v.getID()==R.id.insert){
// Your logic for insert Button
}else if(v.getID()==R.id.Read){
// Your logic for read Button
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
答案 2 :(得分:0)
您的setOnclick
应该是这样的
your_button1.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
// your logic
}
});
your_button2.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
//your logic
}
});
确保您拥有此导入语句 import android.view.View.OnClickListener;
尝试更改
but1.setOnClickListener(new View.OnClickListener()
和
but2.setOnClickListener(new View.OnClickListener()
到
but1.setOnClickListener(new OnClickListener()
和
but2.setOnClickListener(new OnClickListener()
答案 3 :(得分:0)
检查xml文件上的id声明。它应该如下。
<Button
android:id="@+id/insert"/>
答案 4 :(得分:0)
一旦我清理了项目并重新启动eclipse,错误就消失了。无需进行任何更改。