2014-02-16 39 views
-1

我的代码适用于模拟器,但不适用于设备。没有这种方法查询时出错。为什么我的代码不能在设备上工作,但在模拟器上工作?

package testing.login; 

import android.content.Context; 
import android.database.sqlite.SQLiteDatabase; 
import android.database.sqlite.SQLiteOpenHelper; 

public class DBHelper extends SQLiteOpenHelper { 

    public DBHelper(Context context) { 
     //Create database 
     super(context, "MyDatabase.db", null, 1); 
     // TODO Auto-generated constructor stub 
    } 

    @Override 
    public void onCreate(SQLiteDatabase db) { 
     //Create tables 
     db.execSQL("CREATE TABLE ANDRO(ID INTEGER,NAME TEXT,ADDRESS TEXT);"); 
     db.execSQL("CREATE TABLE QUESTIONS(ID INTEGER,QUESTION TEXT,OPTION_A      TEXT,OPTION_B TEXT,OPTION_C TEXT,OPTION_D TEXT,ANSWER TEXT);"); 

    } 

    @Override 
    public void onUpgrade(SQLiteDatabase db, int arg1, int arg2) { 
     //Delete tables 
     db.execSQL("drop table if exists ANDRO"); 
     db.execSQL("drop table if exists QUESTION"); 
    } 

} 

package testing.login; 

import android.os.Bundle; 
import android.annotation.SuppressLint; 
import android.app.Activity; 
import android.content.Intent; 
import android.database.Cursor; 
import android.database.sqlite.SQLiteDatabase; 
import android.view.Menu; 
import android.view.View; 
import android.widget.EditText; 
import android.widget.Toast; 

@SuppressLint("NewApi") 
public class MainActivity extends Activity { 

    //Declaring Variables 
    SQLiteDatabase db; 
    EditText name, address; 
    String sname, saddress; 
    boolean namenotequal = true; 
    DBHelper dbHelper; 
    DatabaseBean dbb; 
    boolean upwrong = true; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     dbHelper = new DBHelper(getBaseContext()); //Accesing Database helper class 
     setContentView(R.layout.activity_main); 
    } 

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

    //On click function of sign up button 
    public void signup(View view) { 
     dbHelper.close(); 
     //Intent used to access sign up class 
     Intent intent = new Intent(this, Signup.class); 
     //  intent.putExtra("sachithkn", dbHelper); 
     startActivity(intent); 
    } 

    @Override 
    public void onBackPressed() {} 

    //On click function of sign in button 
    @SuppressLint("ShowToast") 
    public void signin(View view) { 

     //Reading values from text fields 
     name = (EditText) findViewById(R.id.editText1); 
     address = (EditText) findViewById(R.id.editText2); 
     sname = name.getText().toString(); 
     saddress = address.getText().toString(); 

     //Encrypting password using md5 
     saddress = Signup.md5(saddress); 
     String[] values = new String[] { 
      "ID", 
       "NAME", 
       "ADDRESS" 
     }; 

     db = dbHelper.getReadableDatabase(); //Opening database 
     if (sname.equals("") || saddress.equals("")) //Checking whether text fields are empty or not 
      Toast.makeText(getApplicationContext(), "Field cannot be empty", 2000).show(); 
     else { 
      //Reading values from database 
      Cursor cursor = db.query(false, "ANDRO", values, null, null, null, null, null, null, null); 
      if (cursor.moveToFirst()) { 
       do { 
        String dbname = cursor.getString(1); 
        String dbaddress = cursor.getString(2); 

        if (dbname.equals(sname) && dbaddress.equals(saddress)) //Checking whether values are in database or not 
        { 
         upwrong = false; //Variable set to run toast correctly 
         Toast.makeText(getApplicationContext(), "You have successfully logedin", 2000).show(); 
         dbHelper.close(); 
         Intent intent2 = new Intent(this, Questions.class); //Accesing questions activity 
         intent2.putExtra("passname", dbname); 
         startActivity(intent2); 
        } 
       } while (cursor.moveToNext()); 
       if (upwrong) //Variable set to run toast correctly   
       { 
        //Toast set to show when password or user name is wrong 
        Toast.makeText(getApplicationContext(), "Wrong username or password ,Try again", 2000).show(); 
        setContentView(R.layout.activity_main); 
       } 
      } 
     } 
    } 
} 

这里的错误:

02-16 12:34:44.804: W/dalvikvm(26832): VFY: unable to resolve virtual method 255: Landroid/database/sqlite/SQLiteDatabase;.query (ZLjava/lang/String;[Ljava/lang/String;Ljava/lang/String;[Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;Landroid/os/CancellationSignal;)Landroid/database/Cursor; 
02-16 12:34:44.804: D/dalvikvm(26832): VFY: replacing opcode 0x74 at 0x007f 
02-16 12:34:44.804: D/dalvikvm(26832): VFY: dead code 0x0082-00e8 in Ltesting/login/MainActivity;.signin (Landroid/view/View;)V 
02-16 12:34:47.929: W/dalvikvm(26832): VFY: unable to resolve virtual method 255: Landroid/database/sqlite/SQLiteDatabase;.query (ZLjava/lang/String;[Ljava/lang/String;Ljava/lang/String;[Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;Landroid/os/CancellationSignal;)Landroid/database/Cursor; 
02-16 12:34:47.929: D/dalvikvm(26832): VFY: replacing opcode 0x74 at 0x0077 
02-16 12:34:47.929: D/dalvikvm(26832): VFY: dead code 0x007a-00ec in Ltesting/login/Signup;.signup (Landroid/view/View;)V 
02-16 12:34:57.148: D/AndroidRuntime(26832): Shutting down VM 
02-16 12:34:57.148: W/dalvikvm(26832): threadid=1: thread exiting with uncaught exception (group=0x40018578) 
02-16 12:34:57.195: E/AndroidRuntime(26832): FATAL EXCEPTION: main 
02-16 12:34:57.195: E/AndroidRuntime(26832): java.lang.IllegalStateException: Could not execute method of the activity 
02-16 12:34:57.195: E/AndroidRuntime(26832): at android.view.View$1.onClick(View.java:2144) 
02-16 12:34:57.195: E/AndroidRuntime(26832): at android.view.View.performClick(View.java:2485) 
02-16 12:34:57.195: E/AndroidRuntime(26832): at android.view.View$PerformClick.run(View.java:9080) 
02-16 12:34:57.195: E/AndroidRuntime(26832): at android.os.Handler.handleCallback(Handler.java:587) 
02-16 12:34:57.195: E/AndroidRuntime(26832): at android.os.Handler.dispatchMessage(Handler.java:92) 
02-16 12:34:57.195: E/AndroidRuntime(26832): at android.os.Looper.loop(Looper.java:130) 
02-16 12:34:57.195: E/AndroidRuntime(26832): at android.app.ActivityThread.main(ActivityThread.java:3687) 
02-16 12:34:57.195: E/AndroidRuntime(26832): at java.lang.reflect.Method.invokeNative(Native Method) 
02-16 12:34:57.195: E/AndroidRuntime(26832): at java.lang.reflect.Method.invoke(Method.java:507) 
02-16 12:34:57.195: E/AndroidRuntime(26832): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:867) 
02-16 12:34:57.195: E/AndroidRuntime(26832): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:625) 
02-16 12:34:57.195: E/AndroidRuntime(26832): at dalvik.system.NativeStart.main(Native Method) 
02-16 12:34:57.195: E/AndroidRuntime(26832): Caused by: java.lang.reflect.InvocationTargetException 
02-16 12:34:57.195: E/AndroidRuntime(26832): at java.lang.reflect.Method.invokeNative(Native Method) 
02-16 12:34:57.195: E/AndroidRuntime(26832): at java.lang.reflect.Method.invoke(Method.java:507) 
02-16 12:34:57.195: E/AndroidRuntime(26832): at android.view.View$1.onClick(View.java:2139) 
02-16 12:34:57.195: E/AndroidRuntime(26832): ... 11 more 
02-16 12:34:57.195: E/AndroidRuntime(26832): Caused by: java.lang.NoSuchMethodError: android.database.sqlite.SQLiteDatabase.query 
02-16 12:34:57.195: E/AndroidRuntime(26832): at testing.login.MainActivity.signin(MainActivity.java:77) 
02-16 12:34:57.195: E/AndroidRuntime(26832): ... 14 more 
+2

你为什么叫喊? *(编辑:我已经修复了它)* –

+0

下一次,请努力以可读方式格式化您的代码,而不是[indent mess](http://stackoverflow.com/revisions/21808611/1)那是在我为你清理它之前。 –

+0

请问这是db.execSQL(“CREATE TABLE QUESTIONS(ID INTEGER,QUESTION TEXT,OPTION_A(**不完整的sql和打开的字符串**)你的**真实代码** ?!你期望它编译吗? –

回答

0
总之

,你的代码运行时模拟器不支持的API版本。

您的查询supported from API 16,请尝试使用其他查询方法

+0

谢谢我通过改变查询来解决它原始query.This错误显示在android 4.0.3也。我不知道为什么? – sachithkn

相关问题