2014-10-30 33 views
0

我按照我的first answered post发布。Android Studio,Sql,TextView

经过测试并添加了建议的修改后,我仍然无法让我的代码正常工作。每次我重新开始tuto,然后尝试添加我的代码,然后失败。

如果你能改正我或指出我错过了这一点,那将会很棒。

这工作得很好:

在MyActivity.java有一个可点击按钮

/** Called when the user clicks the Send button */ 
public void goExample(View view) { 
    Intent intent = new Intent(this, Example.class); 
    startActivity(intent); 
} 

这使类实例:

public class Example extends Activity { 

     @Override 
     public void onCreate(Bundle savedInstanceState) { 
      super.onCreate(savedInstanceState); 
      TextView total = (TextView)findViewById(R.id.total); 
      setContentView(R.layout.example); 
     } 

     @Override 
     public boolean onCreateOptionsMenu(Menu menu) { 
      getMenuInflater().inflate(R.menu.menu, menu); 
      return true; 
     }} 

随着其观点的example.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    tools:context=".Example"> 

    <TextView 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:id="@+android:id/total" 
     android:layout_weight="1" /> 

</RelativeLayout> 

没有问题,直到我尝试添加自己的代码(如下):

public class Example extends MyActivity { 

    private TaskDBHelper helper; 

    public int getContactsCount() { 
     helper = new TaskDBHelper(Example.this); 
     SQLiteDatabase db = helper.getReadableDatabase(); 
     String countQuery = "SELECT * FROM " + Playas.TABLE; 
     Cursor cursor = db.rawQuery(countQuery, null); 
     return cursor.getCount(); 
    } 

    public static void main(String[] args) { 
     Example example = new Example(); 
     int a = example.getContactsCount(); 
     System.out.println(a); 
    } 

    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     TextView total = (TextView)findViewById(R.id.total); 
     total.setText(String.valueOf(getContactsCount())); 
     setContentView(R.layout.example); 
    } 

    @Override 
    public boolean onCreateOptionsMenu(Menu menu) { 
     getMenuInflater().inflate(R.menu.menu, menu); 
     return true; 
    } 
} 

然后悲惨的失败,与以下错误:

6715-6715/tab.sqltesting.com.myapplication E/AndroidRuntime﹕ FATAL EXCEPTION: main 
    java.lang.RuntimeException: Unable to start activity ComponentInfo{tab.sqltesting.com.myapplication/tab.sqltesting.com.myapplication.Example}: java.lang.IllegalStateException: attempt to re-open an already-closed object: SQLiteQuery: SELECT * FROM joueurs 
      at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2097) 
      at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2122) 
      at android.app.ActivityThread.access$600(ActivityThread.java:140) 
      at dalvik.system.NativeStart.main(Native Method) 
... 
    Caused by: java.lang.IllegalStateException: attempt to re-open an already-closed object: SQLiteQuery: SELECT * FROM joueurs 
      at android.database.sqlite.SQLiteClosable.acquireReference(SQLiteClosable.java:55) 
      at android.database.sqlite.SQLiteQuery.fillWindow(SQLiteQuery.java:58) 

老实说,我不知道做什么,任何帮助将不胜感激......

感谢

回答

0

在您的类示例中。你应该在setcontentview()之后声明小部件;

public class Example extends Activity { 

     @Override 
     public void onCreate(Bundle savedInstanceState) { 
      super.onCreate(savedInstanceState); 
      setContentView(R.layout.example); 
      TextView total = (TextView)findViewById(R.id.total); 
     } 

     @Override 
     public boolean onCreateOptionsMenu(Menu menu) { 
      getMenuInflater().inflate(R.menu.menu, menu); 
      return true; 
     }} 
0

你为什么不反向游两行代码。

,而不是这样的:

@Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     TextView total = (TextView)findViewById(R.id.total); 
     setContentView(R.layout.example); 
    } 

做这样的:

@Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 

     setContentView(R.layout.example); 
     TextView total = (TextView)findViewById(R.id.total); 
    } 

提示:您应该先设置你的内容视图,然后声明变量。