【问题标题】:Copying database to SD card (java.lang.IllegalStateException)将数据库复制到 SD 卡 (java.lang.IllegalStateException)
【发布时间】:2015-01-23 21:13:58
【问题描述】:

我创建了一个从我的应用程序的数据/数据文件夹复制数据库的函数,但是当我使用函数 CopyDatabase() 时,我不断强制关闭。

这是我绑定到按钮的代码

public class StudentsFormScreen extends Activity
{
    SQLiteDatabase db;

    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        getActionBar().hide();
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
        setContentView(R.layout.studentsform_layout);
        db=openOrCreateDatabase("MyDB1",MODE_WORLD_WRITEABLE, null);
        db.execSQL("CREATE TABLE IF NOT EXISTS Student(fname VARCHAR,lname VARCHAR,email VARCHAR);");

        String path = db.getPath();

        TextView path1 = (TextView) findViewById(R.id.pather);
        path1.setText(path);
    }


    public void CopyDatabase()
    {
        try
        {
            File sd = Environment.getExternalStorageDirectory();
            File data = Environment.getDataDirectory();

            if (sd.canWrite())
            {
                String currentDBPath = "/data/test.com.classmanagertest/databases/MyDB1.db";
                String backupDBPath = "MyDB1.db";
                File currentDB = new File(data,currentDBPath);
                File backupDB = new File(sd, backupDBPath);

                if (currentDB.exists())
                {
                    FileChannel src = new FileInputStream(currentDB).getChannel();
                    FileChannel dst = new FileOutputStream(backupDB).getChannel();

                    dst.transferFrom(src, 0, src.size());
                    src.close();
                    dst.close();
                    Toast.makeText(getApplicationContext(),"Backup Complete", Toast.LENGTH_SHORT).show();
                }

            }
        }

        catch (Exception e)
        {
            e.printStackTrace();
            displayExceptionMessage(e.getMessage());
        }

    }

    public void displayExceptionMessage(String msg)
    {
        Toast.makeText(this, msg, Toast.LENGTH_SHORT).show();
    }

/*   public void CopyDatabase()
    {

        DataBaseHelper myDbHelper = new DataBaseHelper(this);

        try
        {
            myDbHelper.createDataBase();
        }
        catch (IOException ioe)
        {
            throw new Error("Unable to create database");
        }

        try
        {
            myDbHelper.openDataBase();
        }

        catch (SQLException e)
        {
            e.printStackTrace();
        }

    }

*/
    public void CreateScreen(View view)
    {
        Intent StudentCreateScreenIntent = new Intent(this, StudentsFormCreate.class);

        startActivity(StudentCreateScreenIntent);

        overridePendingTransition(R.anim.transition,R.anim.right2left);
    }

    public void UpdateScreen (View view)
    {
        Intent StudentUpdateScreenIntent = new Intent(this, StudentsFormUpdate.class );

        startActivity(StudentUpdateScreenIntent);

        overridePendingTransition(R.anim.transition,R.anim.right2left);
    }

    public void DeleteScreen (View view)
    {
        Intent StudentDeleteScreenIntent = new Intent (this, StudentsFormDelete.class);

        startActivity(StudentDeleteScreenIntent);

        overridePendingTransition(R.anim.transition,R.anim.right2left);
    }

    public void ShowData(View view)
    {
        Cursor c=db.rawQuery("SELECT * from Student", null);
        int count= c.getCount();
        c.moveToFirst();
        TableLayout tableLayout = new TableLayout(getApplicationContext());
        tableLayout.setVerticalScrollBarEnabled(true);
        TableRow tableRow;
        TextView textView,textView1,textView2,textView3,textView4,textView5;
        tableRow = new TableRow(getApplicationContext());
        textView=new TextView(getApplicationContext());
        textView.setText("Firstname");
        textView.setTextColor(Color.RED);
        textView.setTypeface(null, Typeface.BOLD);
        textView.setPadding(20, 20, 20, 20);
        tableRow.addView(textView);
        textView4=new TextView(getApplicationContext());
        textView4.setText("LastName");
        textView4.setTextColor(Color.RED);
        textView4.setTypeface(null, Typeface.BOLD);
        textView4.setPadding(20, 20, 20, 20);
        tableRow.addView(textView4);
        textView5=new TextView(getApplicationContext());
        textView5.setText("Email");
        textView5.setTextColor(Color.RED);
        textView5.setTypeface(null, Typeface.BOLD);
        textView5.setPadding(20, 20, 20, 20);
        tableRow.addView(textView5);
        tableLayout.addView(tableRow);
        for (Integer j = 0; j < count; j++)
        {
            tableRow = new TableRow(getApplicationContext());
            textView1 = new TextView(getApplicationContext());
            textView1.setText(c.getString(c.getColumnIndex("fname")));
            textView2 = new TextView(getApplicationContext());
            textView2.setText(c.getString(c.getColumnIndex("lname")));
            textView3 = new TextView(getApplicationContext());
            textView3.setText(c.getString(c.getColumnIndex("email")));
            textView1.setPadding(20, 20, 20, 20);
            textView2.setPadding(20, 20, 20, 20);
            textView3.setPadding(20, 20, 20, 20);
            tableRow.addView(textView1);
            tableRow.addView(textView2);
            tableRow.addView(textView3);
            tableLayout.addView(tableRow);
            c.moveToNext() ;
        }
        setContentView(tableLayout);
        db.close();
    }

}

这是我得到的错误:

01-23 20:53:07.539  32699-32699/? E/AndroidRuntime﹕ FATAL EXCEPTION: main
    Process: test.com.classmanagertest, PID: 32699
    java.lang.IllegalStateException: Could not find a method CopyDatabase(View) in the activity class test.com.classmanagertest.StudentsFormScreen for onClick handler on view class android.widget.Button with id 'CopyDatabase'
            at android.view.View$1.onClick(View.java:3994)
            at android.view.View.performClick(View.java:4756)
            at android.view.View$PerformClick.run(View.java:19749)
            at android.os.Handler.handleCallback(Handler.java:739)
            at android.os.Handler.dispatchMessage(Handler.java:95)
            at android.os.Looper.loop(Looper.java:135)
            at android.app.ActivityThread.main(ActivityThread.java:5221)
            at java.lang.reflect.Method.invoke(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:372)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)
     Caused by: java.lang.NoSuchMethodException: CopyDatabase [class android.view.View]
            at java.lang.Class.getMethod(Class.java:664)
            at java.lang.Class.getMethod(Class.java:643)
            at android.view.View$1.onClick(View.java:3987)
            at android.view.View.performClick(View.java:4756)
            at android.view.View$PerformClick.run(View.java:19749)
            at android.os.Handler.handleCallback(Handler.java:739)
            at android.os.Handler.dispatchMessage(Handler.java:95)
            at android.os.Looper.loop(Looper.java:135)
            at android.app.ActivityThread.main(ActivityThread.java:5221)
            at java.lang.reflect.Method.invoke(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:372)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)

我有权限:

        <uses-permission
            android:name = "android.permission.WRITE_EXTERNAL_STORAGE"/>

非常感谢任何帮助!

【问题讨论】:

    标签: java android database illegalstateexception


    【解决方案1】:

    我想你已经在 XML 布局中某个按钮的onClick 中添加了方法调用。在这种情况下,您调用的方法应该有一个View v 参数。

    因此,您的方法定义应如下所示:public void CopyDatabase(View v)

    当您点击按钮时,android 会查找名为 CopyDatabase(View v) 的方法,但无法在 Activity 中找到它,因此会抛出 NoSuchMethodException

    【讨论】:

    • 我尝试了你的建议,程序在 logcat 中没有错误,但它似乎没有复制到我的 SD 卡(外部存储)
    • 查看logcat中是否有异常。 (不是错误,而是警告)。
    • 这是我收到的警告:01-23 21:56:15.118 W/PackageManager(969):为 test.com.classmanagertest 检索资源失败:资源 ID #0x0
    • 我认为,您在数据库路径中提供的包名可能是错误的。你确定这是你的android项目的包名test.com.classmanagertest
    • 是的,我确定是因为我使用文本视图查看数据库路径,它是:/data/data/test.com.classmanagertest/databases/MyDB1
    猜你喜欢
    • 2011-12-06
    • 2011-12-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-01-11
    • 1970-01-01
    • 2013-03-24
    • 2023-03-30
    相关资源
    最近更新 更多