invalid statement in fillwindow() in Android

2020-04-08 11:41发布

I newbie in Android and before ask question here i was googling, but on this question i can't find answer. (also this stackoverflow.com/questions/4195089 don't have answer).

In my test app on emulator i have error ERROR/Cursor(1512): Invalid statement in fillWindow() on LogCat when i press back button on second activity.
11-23 19:06:05.050: DEBUG/TestDB/Debug/MainActivity(1512): onCreate
11-23 19:06:06.820: DEBUG/TestDB/Debug/MainActivity(1512): openDataBase
11-23 19:06:07.240: DEBUG/TestDB/Debug/MainActivity(1512): String[] from
11-23 19:06:07.270: DEBUG/TestDB/Debug/MainActivity(1512): SimpleCursorAdapter
11-23 19:06:07.500: DEBUG/TestDB/Debug/MainActivity(1512): myDbHelper.close
11-23 19:06:07.520: DEBUG/TestDB/Debug/MainActivity(1512): onStart
11-23 19:06:07.551: DEBUG/TestDB/Debug/MainActivity(1512): onResume
11-23 19:06:09.040: INFO/ActivityManager(587): Displayed activity ru.olegi/.MainActivity: 7962 ms
11-23 19:06:11.850: INFO/ActivityManager(587): Starting activity: Intent { comp={ru.olegi/ru.olegi.AnotherActivity} }
11-23 19:06:12.100: DEBUG/TestDB/Debug/AnotherActivity(1512): onCreate
11-23 19:06:12.970: INFO/ActivityManager(587): Displayed activity ru.olegi/.AnotherActivity: 1100 ms
11-23 19:06:14.550: ERROR/Cursor(1512): Invalid statement in fillWindow()
11-23 19:06:14.590: DEBUG/TestDB/Debug/MainActivity(1512): onStart
11-23 19:06:14.600: DEBUG/TestDB/Debug/MainActivity(1512): onResume

All code stored in onCreate. So when i press back, ListView is empty. For fix this i move code to onStart. Here is log for this and again have Invalid statement in fillWindow()
11-23 19:26:06.280: DEBUG/TestDB/Debug/MainActivity(6597): onCreate
11-23 19:26:06.462: DEBUG/TestDB/Debug/MainActivity(6597): onStart
11-23 19:26:06.700: DEBUG/TestDB/Debug/MainActivity(6597): openDataBase
11-23 19:26:06.740: DEBUG/TestDB/Debug/MainActivity(6597): String[] from
11-23 19:26:06.750: DEBUG/TestDB/Debug/MainActivity(6597): SimpleCursorAdapter
11-23 19:26:06.981: DEBUG/TestDB/Debug/MainActivity(6597): myDbHelper.close
11-23 19:26:07.010: DEBUG/TestDB/Debug/MainActivity(6597): onResume
11-23 19:26:08.030: INFO/ActivityManager(587): Displayed activity ru.olegi/.MainActivity: 3876 ms
11-23 19:26:15.419: INFO/ActivityManager(587): Starting activity: Intent { comp={ru.olegi/ru.olegi.AnotherActivity} }
11-23 19:26:15.770: DEBUG/TestDB/Debug/AnotherActivity(6597): onCreate
11-23 19:26:16.440: INFO/ActivityManager(587): Displayed activity ru.olegi/.AnotherActivity: 1001 ms
11-23 19:26:17.860: ERROR/Cursor(6597): Invalid statement in fillWindow()
11-23 19:26:17.870: DEBUG/TestDB/Debug/MainActivity(6597): onStart
11-23 19:26:18.260: DEBUG/TestDB/Debug/MainActivity(6597): openDataBase
11-23 19:26:18.290: DEBUG/TestDB/Debug/MainActivity(6597): String[] from
11-23 19:26:18.310: DEBUG/TestDB/Debug/MainActivity(6597): SimpleCursorAdapter
11-23 19:26:18.670: DEBUG/TestDB/Debug/MainActivity(6597): myDbHelper.close
11-23 19:26:18.700: DEBUG/TestDB/Debug/MainActivity(6597): onResume

This code

package ru.olegi;

import java.io.IOException;

import android.app.Activity;
import android.content.Intent;
import android.database.Cursor;
import android.database.SQLException;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;
import android.widget.SimpleCursorAdapter;

public class MainActivity extends Activity {
    /** Called when the activity is first created. */

    private DatabaseHelper myDbHelper;  
    private ListView lv1;   

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Log.d("TestDB/Debug/MainActivity", "onCreate");          

        setContentView(R.layout.main);        

    }

    @Override
    public void onStart() {
        super.onStart();
        Log.d("TestDB/Debug/MainActivity", "onStart");

        // DATABASE OPEN - START
        DatabaseHelper myDbHelper = new DatabaseHelper(this);
        myDbHelper = new DatabaseHelper(this);

        try {
            myDbHelper.createDataBase();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }          

        try {
            myDbHelper.openDataBase();
        }catch(SQLException sqle){
            throw sqle;
        }        

        Log.d("TestDB/Debug/MainActivity", "openDataBase");        

        // DATABASE OPEN - END

        Cursor c = myDbHelper.fetchAllFoodname();
        startManagingCursor(c);                       

        lv1 = (ListView) findViewById(R.id.ListView01);

        String[] from = new String[] { DatabaseHelper.KEY_NAME };
        int[] to = new int[] { android.R.id.text1};

        Log.d("TestDB/Debug/MainActivity", "String[] from");

        SimpleCursorAdapter notes =
            new SimpleCursorAdapter(this, android.R.layout.simple_list_item_1, c, from, to);

        Log.d("TestDB/Debug/MainActivity", "SimpleCursorAdapter");

        lv1.setAdapter(notes);

        lv1.setOnItemClickListener(new OnItemClickListener() {      
            //@Override     
            public void onItemClick(AdapterView<?> a, View v, int position, long id) {

                Intent intent = new Intent();

                //getApplicationContext()
                intent.setClass(MainActivity.this, AnotherActivity.class);

                //intent.putExtra(AnotherActivity.EXT_ID, id);              

                startActivity(intent);
                //finish(); 
            }   
         });   

        // DATABASE CLOSE - START       

        myDbHelper.close();
        Log.d("TestDB/Debug/MainActivity", "myDbHelper.close");

        // DATABASE CLOSE - END         
    }    

    @Override
    public void onResume(){
        super.onResume();
        Log.d("TestDB/Debug/MainActivity", "onResume"); 
    }

    @Override
    public void onDestroy(){
        super.onDestroy();
        Log.d("TestDB/Debug/MainActivity", "onDestroy");        
    }        
}

Here groups.google.com/group/android-developers/browse_thread/thread/8bec793c626fb405 i read, for fix fillwindow() error need

Short answer; Your database object (db) is in the wrong scope. Longer answer; By declaring db in the method you're saying to the JVM that when the method finishes your database connection (db) is no longer useful. What you should do is declare db at the class level instead of the method level, create it in onCreate, and close it in onDestroy.

Ok, i move sections // DATABASE OPEN - START and // DATABASE CLOSE - START to onCreate and to onDestory. And now i have exception on start
11-23 19:35:39.310: DEBUG/TestDB/Debug/MainActivity(8975): onCreate
11-23 19:35:40.050: DEBUG/TestDB/Debug/MainActivity(8975): openDataBase
11-23 19:35:40.050: DEBUG/TestDB/Debug/MainActivity(8975): onStart
11-23 19:35:40.090: DEBUG/AndroidRuntime(8975): Shutting down VM
11-23 19:35:40.100: WARN/dalvikvm(8975): threadid=3: thread exiting with uncaught exception (group=0x4000fe70)
11-23 19:35:40.120: ERROR/AndroidRuntime(8975): Uncaught handler: thread main exiting due to uncaught exception
11-23 19:35:40.300: ERROR/AndroidRuntime(8975): java.lang.RuntimeException: Unable to start activity ComponentInfo{ru.olegi/ru.olegi.MainActivity}: java.lang.NullPointerException
11-23 19:35:40.300: ERROR/AndroidRuntime(8975): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2268)
11-23 19:35:40.300: ERROR/AndroidRuntime(8975): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2284)
11-23 19:35:40.300: ERROR/AndroidRuntime(8975): at android.app.ActivityThread.access$1800(ActivityThread.java:112)
11-23 19:35:40.300: ERROR/AndroidRuntime(8975): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1692)
11-23 19:35:40.300: ERROR/AndroidRuntime(8975): at android.os.Handler.dispatchMessage(Handler.java:99)
11-23 19:35:40.300: ERROR/AndroidRuntime(8975): at android.os.Looper.loop(Looper.java:123)
11-23 19:35:40.300: ERROR/AndroidRuntime(8975): at android.app.ActivityThread.main(ActivityThread.java:3948)
11-23 19:35:40.300: ERROR/AndroidRuntime(8975): at java.lang.reflect.Method.invokeNative(Native Method)
11-23 19:35:40.300: ERROR/AndroidRuntime(8975): at java.lang.reflect.Method.invoke(Method.java:521)
11-23 19:35:40.300: ERROR/AndroidRuntime(8975): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:782)
11-23 19:35:40.300: ERROR/AndroidRuntime(8975): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:540)
11-23 19:35:40.300: ERROR/AndroidRuntime(8975): at dalvik.system.NativeStart.main(Native Method)
11-23 19:35:40.300: ERROR/AndroidRuntime(8975): Caused by: java.lang.NullPointerException
11-23 19:35:40.300: ERROR/AndroidRuntime(8975): at ru.olegi.MainActivity.onStart(MainActivity.java:59)
11-23 19:35:40.300: ERROR/AndroidRuntime(8975): at android.app.Instrumentation.callActivityOnStart(Instrumentation.java:1205)
11-23 19:35:40.300: ERROR/AndroidRuntime(8975): at android.app.Activity.performStart(Activity.java:3490)
11-23 19:35:40.300: ERROR/AndroidRuntime(8975): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2240)
11-23 19:35:40.300: ERROR/AndroidRuntime(8975): ... 11 more

Here is code with moved db-sections:

package ru.olegi;

import java.io.IOException;

import android.app.Activity;
import android.content.Intent;
import android.database.Cursor;
import android.database.SQLException;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;
import android.widget.SimpleCursorAdapter;

public class MainActivity extends Activity {
    /** Called when the activity is first created. */

    private DatabaseHelper myDbHelper;  
    private ListView lv1;   

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Log.d("TestDB/Debug/MainActivity", "onCreate");          

        setContentView(R.layout.main);

        // DATABASE OPEN - START
        DatabaseHelper myDbHelper = new DatabaseHelper(this);
        myDbHelper = new DatabaseHelper(this);

        try {
            myDbHelper.createDataBase();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }          

        try {
            myDbHelper.openDataBase();
        }catch(SQLException sqle){
            throw sqle;
        }        

        Log.d("TestDB/Debug/MainActivity", "openDataBase");        

        // DATABASE OPEN - END


    }

    @Override
    public void onStart() {
        super.onStart();
        Log.d("TestDB/Debug/MainActivity", "onStart");

        Cursor c = myDbHelper.fetchAllFoodname();
        startManagingCursor(c);                       

        lv1 = (ListView) findViewById(R.id.ListView01);

        String[] from = new String[] { DatabaseHelper.KEY_NAME };
        int[] to = new int[] { android.R.id.text1};

        Log.d("TestDB/Debug/MainActivity", "String[] from");

        SimpleCursorAdapter notes =
            new SimpleCursorAdapter(this, android.R.layout.simple_list_item_1, c, from, to);

        Log.d("TestDB/Debug/MainActivity", "SimpleCursorAdapter");

        lv1.setAdapter(notes);

        lv1.setOnItemClickListener(new OnItemClickListener() {      
            //@Override     
            public void onItemClick(AdapterView<?> a, View v, int position, long id) {

                Intent intent = new Intent();

                //getApplicationContext()
                intent.setClass(MainActivity.this, AnotherActivity.class);

                //intent.putExtra(AnotherActivity.EXT_ID, id);              

                startActivity(intent);
                //finish(); 
            }   
         });   

    }    

    @Override
    public void onResume(){
        super.onResume();
        Log.d("TestDB/Debug/MainActivity", "onResume"); 
    }

    @Override
    public void onDestroy(){
        super.onDestroy();
        Log.d("TestDB/Debug/MainActivity", "onDestroy");

        // DATABASE CLOSE - START       

        myDbHelper.close();
        Log.d("TestDB/Debug/MainActivity", "myDbHelper.close");

        // DATABASE CLOSE - END     
    }        
}

Please, help fix Invalid statement in fillWindow()

AnotherActivity - is simple with TextView, MainActivity have only listview + onclick listener

1条回答
贪生不怕死
2楼-- · 2020-04-08 12:37
private DatabaseHelper myDbHelper; 
...
public void onStart() {
     DatabaseHelper myDbHelper = new DatabaseHelper(this);

I removed the declaration DatabaseHelper in onStart. That fixed the ERROR/Cursor(1512)

查看更多
登录 后发表回答