使用&#34时显示错误;此"与班级名称

时间:2014-11-20 09:10:46

标签: android

我试图打开我的数据库并从其他类调用方法。但是在创建数据库类的瞬间时显示错误。在这里,Remind entry =new Remind(AddActivity.this);错误是" The constructor Remind(AddActivity) is undefined"并且在我的其他班级中查看相同的错误正在显示," Remind info = new Remind(this);"

这是我的代码: 主要活动:

import android.support.v7.app.ActionBarActivity;
import android.app.Dialog;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

public class AddActivity extends ActionBarActivity implements OnClickListener {


    EditText name1,details1;
    Button add1,show;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.add);
         name1 =(EditText) findViewById(R.id.editText1);
         details1 =(EditText) findViewById(R.id.editText2);
         add1 = (Button) findViewById(R.id.button1);
         show =(Button) findViewById(R.id.button2);

         add1.setOnClickListener(this);
         show.setOnClickListener(this);
    }


    public void onClick(View v) {
        // TODO Auto-generated method stub
        switch (v.getId()) {
        case R.id.button1:
            boolean success =true;
            try{
            String name =name1.getText().toString();
            String details =details1.getText().toString();

            Remind entry =new Remind(AddActivity.this);
            entry.open();

            entry.createEntry(name,details);
            entry.close();
            name1.setText(" ");
            details1.setText(" ");
            break;
            }catch (Exception e) {
                success=false;
                String error =e.toString();
                Dialog d =new Dialog(this);
                d.setTitle("oohh noo..!!");
                TextView tv =new TextView(this);
                tv.setText(error);
                d.setContentView(tv);
                d.show();
            }finally{
                if(success){
                    Dialog d =new Dialog(this);
                    d.setTitle("oohh yess..!!");
                    TextView tv =new TextView(this);
                    tv.setText("its success");
                    d.setContentView(tv);
                    d.show();
                }
            }
         case R.id.button2:
             Intent i = new Intent("android.intent.action.View");
            startActivity(i);
            break;

        default:
            break;
        }
    }


}

这是查看数据库中数据的类:

import java.util.List;

import com.example.stepping.Remind.Dbhelper;

import android.app.Activity;
import android.database.Cursor;
import android.os.Bundle;
import android.support.v4.widget.SimpleCursorAdapter;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.AdapterView.OnItemLongClickListener;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

public class View extends Activity {

    SimpleCursorAdapter adaptr;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.display);

        ListView lv =(ListView) findViewById(R.id.listview);

        Remind info = new Remind(this);
        try {
            info.open();
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        List<String> data =info.getData();
        lv.setAdapter(new ArrayAdapter<String>(this,
                 android.R.layout.simple_list_item_1, data));
        Helper.getListViewSize(lv);



}
}

这是我的提醒课程:

import java.util.ArrayList;
import java.util.List;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.widget.AdapterView.OnItemLongClickListener;

public class Remind{

    public static final String NAME ="name";
    public static final String DETAIL ="detail";
    public static final String KEY ="key";
    private static final String TABLE_NAME ="remind_tbl";
    private static final String DATABASE_NAME ="remind_db";
    private static final int DATABASE_VERSION =2;

    private Dbhelper ourhelper;
    private Context ourcontext;
    private SQLiteDatabase ourDB;

    private String QUERY_STRING="CREATE TABLE " 
    +TABLE_NAME+ " ( " +KEY+ " INTEGER PRIMARY KEY AUTOINCREMENT , "  
    +NAME+ " TEXT NOT NULL , "
    +DETAIL+ " TEXT )" ;
public class Dbhelper extends SQLiteOpenHelper{                                 

    public Dbhelper(Context context) {
        super(context, DATABASE_NAME, null, DATABASE_VERSION);

    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        // TODO Auto-generated method stub
         db.execSQL(QUERY_STRING);
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        // TODO Auto-generated method stub
        db.execSQL("DROP TABLE IF EXISTS "+TABLE_NAME);
        onCreate(db);   
    }

}


    public Remind open() throws Exception{
        ourhelper = new Dbhelper(ourcontext);
        ourDB = ourhelper.getWritableDatabase();
        return this;
    }

    public void close(){
        ourhelper.close();
    }

    public long createEntry(String name, String details) {
        // TODO Auto-generated method stub
        ContentValues cv =new ContentValues();
        cv.put(NAME, name);
        cv.put(DETAIL,details);
        return ourDB.insert(TABLE_NAME, null, cv);
    }

    public List<String> getData() {
        // TODO Auto-generated method stub
        String[] coloumn = new String[]{KEY,NAME,DETAIL};
        Cursor c=ourDB.query(TABLE_NAME, coloumn, null, null, null, null, null);
        String result =" ";
        List<String> results = new ArrayList<String>();
        int iRow =c.getColumnIndex(KEY);
        int iName =c.getColumnIndex(NAME);
        int iDetails =c.getColumnIndex(DETAIL);
        for(c.moveToFirst(); !c.isAfterLast();c.moveToNext()){
         result = c.getString(iRow)+" "+ c.getString(iName)+" = "+ c.getString(iDetails)+"\n";
        results.add(result);             
    }

        return results;
    }
}

3 个答案:

答案 0 :(得分:1)

您没有定义任何自定义构造函数,因此默认构造函数不允许任何参数:

Remind info = new Remind();

使用Context reference定义自定义构造函数:

public Remind(Context ourcontext){
   this.ourcontext=ourcontext;
}

注意:在您的情况下,您在Remind类中使用上下文引用,因此您必须定义自定义构造函数并初始化ourcontext。

答案 1 :(得分:1)

你的Remind类没有构造函数将Activity作为参数。因此,您无法编译此代码。

尝试创建一个构造函数:

public Remind (Activity activity) { 
    ourContext=activity.getApplicationContext();
}

答案 2 :(得分:0)

指定缺少上下文

public Dbhelper(Context context) {
        super(context, DATABASE_NAME, null, DATABASE_VERSION);
          ourcontext = context;  // This line is missing
    }