简体   繁体   English

Android-未定义类型为new View.OnClickListener(){}的Sqlite数据库方法

[英]Android - Sqlite database method undefined for type new View.OnClickListener(){}

i want to call inserttable method from RechargeActivity.java file, where the definition lies in another TryUIOpenHelper.java , where my databse and tables are created. 我想从RechargeActivity.java文件调用inserttable方法,该文件的定义位于另一个TryUIOpenHelper.java中,在该文件中创建了我的数据库和表。 I wish to know better way to to this.. Please help 我希望知道更好的方法。.请帮助

     public class RechargeActivity extends Activity   {
     protected void onCreate(Bundle savedInstanceState) {


     super.onCreate(savedInstanceState);
     setContentView(R.layout.rechtable);

     RadioButton rb1 = (RadioButton) findViewById(R.id.rtt1); 
     RadioButton rb2 = (RadioButton) findViewById(R.id.rtt2); 
     final TextView amount = (TextView)findViewById(R.id.amount1);
     final TextView talktime = (TextView)findViewById(R.id.talktime1);
     final TextView validity = (TextView)findViewById(R.id.validity1);  
     final Button button = (Button) findViewById(R.id.done);

     final TryUIOpenHelper helper=new TryUIOpenHelper(this);
     final SQLiteDatabase dbDatabase=helper.getWritableDatabase();
     final Date date = new Date(); 
     Calendar now = Calendar.getInstance();
     int  year = now.get(Calendar.YEAR);
     int  month = now.get(Calendar.MONTH);      // 0 to 11
     int day = now.get(Calendar.DAY_OF_MONTH); 

     String val=""+(day)+"/"+(month+1)+"/"+(year+1)+"";
         validity.setText(val);

     try{
     rb1.setOnClickListener( new OnClickListener ()
     {
            public void onClick(View v)
            {   
          //Error here: The method inserttable(SQLiteDatabase) is undefined       for the type new View.OnClickListener(){}
    inserttable(dbDatabase);

           } 
          });
    }
    catch(Exception e)
    {
    System.out.println("Exception:"+e);
    }   

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

      } } );

    button.setOnClickListener(new OnClickListener() {
        @Override
    public void onClick(View v) {
    final Intent intent = 
    new Intent(RechargeActivity.this,       NumForRecharge.class);
            startActivity(intent);
        }
    });

     //db.close();

      }         
      }

在您的TryUIOpenHelper对象上调用它

helper.inserttable(dbDatabase);
 rb1.setOnClickListener( new OnClickListener ()
     {
            public void onClick(View v)
            {   

     TryUIOpenHelper openHelper=new TryUIOpenHelper();
    openHelper.inserttable(dbDatabase);

           } 
          });

Change your onClickListener Like this. 像这样更改您的onClickListener。

Reason: When you try to use a method which lies in some other Class, you need to create a Object for that class and only then you can call a method from that class(Assuming it is public method). 原因:当您尝试使用其他某个类中的方法时,您需要为该类创建一个对象,然后才可以从该类中调用一个方法(假设它是公共方法)。 If it is a private method then you will not be able to access it. 如果它是私有方法,那么您将无法访问它。

Similarly if that method is a static one, you need not create a object for that class. 同样,如果该方法是静态方法,则无需为该类创建对象。 Simply ClassName.MethodName(); 只需ClassName.MethodName(); will do. 会做。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Android对于new View.OnClickListener(){}类型,未定义方法startActivity(Intent) - Android The method startActivity(Intent) is undefined for the type new View.OnClickListener(){} 未为类型imageButton1定义方法setOnClickListener(new View.OnClickListener(){}) - The method setOnClickListener(new View.OnClickListener(){}) is undefined for the type imageButton1 对于new View.OnClickListener(){}类型,未定义方法overridePendingTransition(int,int) - The method overridePendingTransition(int, int) is undefined for the type new View.OnClickListener(){} 方法findViewByID(int)未定义类型new View.OnClickListener(){} - The method findViewByID(int) is undefined for the type new View.OnClickListener(){} 对于新视图类型,未定义方法startactivity(intent)。onclicklistener() - the method startactivity(intent) is undefined for the type new view.onclicklistener() 对于新的View.OnClickListener(){}类型,未定义方法userAccounts() - The method userAccounts() is undefined for the type new View.OnClickListener(){} 未定义类型为new View.OnClickListener(){} - Undefined for the type new View.OnClickListener(){} Android:类型为View.OnClickListener()的getApplication()未定义 - Android: getApplication() is undefined for type View.OnClickListener() android错误:“视图类型中的方法setOnClickListener(View.OnClickListener)不适用于参数(new OnClickListener(){})” - android error: “The method setOnClickListener(View.OnClickListener) in the type View is not applicable for the arguments (new OnClickListener(){})” 如何修复类型为new View.OnClickListener()语法错误的方法startActivity(Intent)未定义 - How to fix The method startActivity(Intent) is undefined for the type new View.OnClickListener() syntax error
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM