2013-11-14 72 views
0

您好我在SQLite中有一個問題,我跟着travis的教程,但我仍然有錯誤,並且iv'e一直在嘗試幾天,有人可以請提前幫助我。開放的Android SQLite錯誤

這裏是我的代碼

logcat的

10-17 16:21:57.835: D/AndroidRuntime(2238): Shutting down VM 
10-17 16:21:57.835: W/dalvikvm(2238): threadid=1: thread exiting with uncaught exception (group=0x4001d800) 
10-17 16:21:57.865: E/AndroidRuntime(2238): FATAL EXCEPTION: main 
10-17 16:21:57.865: E/AndroidRuntime(2238): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.sample.sqlite/com.sample.sqlite.SQLite}: java.lang.NullPointerException 
10-17 16:21:57.865: E/AndroidRuntime(2238):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663) 
10-17 16:21:57.865: E/AndroidRuntime(2238):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679) 
10-17 16:21:57.865: E/AndroidRuntime(2238):  at android.app.ActivityThread.access$2300(ActivityThread.java:125) 
10-17 16:21:57.865: E/AndroidRuntime(2238):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033) 
10-17 16:21:57.865: E/AndroidRuntime(2238):  at android.os.Handler.dispatchMessage(Handler.java:99) 
10-17 16:21:57.865: E/AndroidRuntime(2238):  at android.os.Looper.loop(Looper.java:123) 
10-17 16:21:57.865: E/AndroidRuntime(2238):  at android.app.ActivityThread.main(ActivityThread.java:4627) 
10-17 16:21:57.865: E/AndroidRuntime(2238):  at java.lang.reflect.Method.invokeNative(Native Method) 
10-17 16:21:57.865: E/AndroidRuntime(2238):  at java.lang.reflect.Method.invoke(Method.java:521) 
10-17 16:21:57.865: E/AndroidRuntime(2238):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868) 
10-17 16:21:57.865: E/AndroidRuntime(2238):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626) 
10-17 16:21:57.865: E/AndroidRuntime(2238):  at dalvik.system.NativeStart.main(Native Method) 
10-17 16:21:57.865: E/AndroidRuntime(2238): Caused by: java.lang.NullPointerException 
10-17 16:21:57.865: E/AndroidRuntime(2238):  at com.sample.sqlite.SQLite.onCreate(SQLite.java:28) 
10-17 16:21:57.865: E/AndroidRuntime(2238):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047) 
10-17 16:21:57.865: E/AndroidRuntime(2238):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627) 
10-17 16:21:57.865: E/AndroidRuntime(2238):  ... 11 more 

JAVA 包com.sample.sqlite;

import android.app.Activity; 
import android.app.Dialog; 
import android.os.Bundle; 
import android.view.View; 
import android.view.View.OnClickListener; 
import android.widget.Button; 
import android.widget.EditText; 
import android.widget.TextView; 

public class SQLite extends Activity implements OnClickListener { 

    Button btnUpdate, btnView; 
    EditText etName, etScore, etCleared; 


    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     // TODO Auto-generated method stub 
     super.onCreate(savedInstanceState); 
     btnUpdate = (Button) findViewById(R.id.btnUpdate); 
     btnView = (Button) findViewById(R.id.btnView); 
     etName = (EditText) findViewById(R.id.etName); 
     etScore = (EditText) findViewById(R.id.etScore); 
     etCleared = (EditText) findViewById(R.id.etCleared); 

     btnUpdate.setOnClickListener(this); 
     btnView.setOnClickListener(this); 
    } 

    @Override 
    public void onClick(View arg0) { 
     // TODO Auto-generated method stub 

     switch (arg0.getId()) { 

     case R.id.btnUpdate: 

      boolean didItWork = true; 
      try{ 
      String name = etName.getText().toString(); 
      String score = etScore.getText().toString(); 
      String cleared = etCleared.getText().toString(); 

      Score entry = new Score(SQLite.this); 
      entry.open(); 
      entry.createEntry(name, score, cleared); 
      entry.close(); 

      }catch (Exception e){ 
       didItWork = false; 
      }finally{ 
       if (didItWork){ 
        Dialog d = new Dialog(this); 
        d.setTitle("Yup"); 
        TextView tv = new TextView(this); 
        tv.setText("Success"); 
        d.setContentView(tv); 
        d.show(); 
       } 
      } 

      break; 

     case R.id.btnView: 

      break; 
     } 
    } 


} 

清單

<?xml version="1.0" encoding="utf-8"?> 
<manifest xmlns:android="http://schemas.android.com/apk/res/android" 
    package="com.sample.sqlite" 
    android:versionCode="1" 
    android:versionName="1.0" > 

    <uses-sdk 
     android:minSdkVersion="8" 
     android:targetSdkVersion="17" /> 

    <application 
     android:allowBackup="true" 
     android:icon="@drawable/ic_launcher" 
     android:label="@string/app_name" 
     android:theme="@style/AppTheme" > 
     <activity 
      android:name=".SQLite" 
      android:label="@string/app_name" > 
      <intent-filter> 
       <action android:name="android.intent.action.SQLITE" /> 

       <category android:name="android.intent.category.LAUNCHER" /> 
      </intent-filter> 
     </activity> 
    </application> 

</manifest> 
+0

'產生的原因:在com.sample顯示java.lang.NullPointerException。 sqlite.SQLite.onCreate(SQLite.java:28)'......也許你沒有在那裏提供上下文? – gunar

回答

2

你不設置內容查看

添加的setContentView()

+0

我在我的SQLite.java上添加了setContentView,但仍然出現錯誤 – John