简体   繁体   中英

Error in calling of java class in android app development

I am new to android development. This is the code I am using:

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


}

The error:

Multiple markers at this line
    - container cannot be resolved or is not a field
    - implements 
     android.view.View.OnClickListener.onClick

Is coming at public void onClick(View v) line.

I would be glad if anyone could help. Thanks!

检查项目的属性,并验证Java Compiler-> Compiler合规性级别是否设置为1.6或更高。

Try this

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


}

Your setOnclick should look like this

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

Make sure you have this import statement import android.view.View.OnClickListener;

Try changing

but1.setOnClickListener(new View.OnClickListener()    

and

but2.setOnClickListener(new View.OnClickListener()

to

but1.setOnClickListener(new OnClickListener()

and

but2.setOnClickListener(new OnClickListener()

Check your id declaration on xml file. It should be as below.

<Button
    android:id="@+id/insert"/>

The error went away once I cleaned my project and restarted eclipse. No changes were required.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM