2013-02-11 92 views
0

每次我跑我的应用程序的空指针蒙上在logcat的我怎么能知道哪里出错香港专业教育学院试图调试在日食,但我仍然不能老是搞清楚,错误的logcat

这里是logcat的错误起初,我发现了错误并没有存在这样的列我有固定的,现在是这样的错误:

02-12 00:16:40.837: D/AndroidRuntime(360): Shutting down VM 
02-12 00:16:40.837: W/dalvikvm(360): threadid=1: thread exiting with uncaught exception    (group=0x40015560) 
02-12 00:16:40.868: E/AndroidRuntime(360): FATAL EXCEPTION: main 
02-12 00:16:40.868: E/AndroidRuntime(360): java.lang.RuntimeException: Unable to start  activity ComponentInfo{com.example.database/com.example.database.AndroidSQLite}:  java.lang.NullPointerException 
02-12 00:16:40.868: E/AndroidRuntime(360): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1647) 
02-12 00:16:40.868: E/AndroidRuntime(360): at  android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663) 
02-12 00:16:40.868: E/AndroidRuntime(360): at  android.app.ActivityThread.access$1500(ActivityThread.java:117) 
02-12 00:16:40.868: E/AndroidRuntime(360): at  android.app.ActivityThread$H.handleMessage(ActivityThread.java:931) 
02-12 00:16:40.868: E/AndroidRuntime(360): at android.os.Handler.dispatchMessage(Handler.java:99) 
02-12 00:16:40.868: E/AndroidRuntime(360): at android.os.Looper.loop(Looper.java:123) 
02-12 00:16:40.868: E/AndroidRuntime(360): at android.app.ActivityThread.main(ActivityThread.java:3683) 
02-12 00:16:40.868: E/AndroidRuntime(360): at  java.lang.reflect.Method.invokeNative(Native Method) 
02-12 00:16:40.868: E/AndroidRuntime(360): at  java.lang.reflect.Method.invoke(Method.java:507) 
02-12 00:16:40.868: E/AndroidRuntime(360): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839) 
02-12 00:16:40.868: E/AndroidRuntime(360): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597) 
02-12 00:16:40.868: E/AndroidRuntime(360): at dalvik.system.NativeStart.main(Native  Method) 
02-12 00:16:40.868: E/AndroidRuntime(360): Caused by: java.lang.NullPointerException 
02-12 00:16:40.868: E/AndroidRuntime(360): at  com.example.database.AndroidSQLite.onCreate(AndroidSQLite.java:58) 
02-12 00:16:40.868: E/AndroidRuntime(360): at  android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047) 
02-12 00:16:40.868: E/AndroidRuntime(360): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1611) 
02-12 00:16:40.868: E/AndroidRuntime(360): ... 11 more 
02-12 00:16:42.977: I/Process(360): Sending signal. PID: 360 SIG: 9 

这里是我的代码:

 package com.example.database; 

    import android.content.ContentValues; 
    import android.content.Context; 
    import android.database.Cursor; 
    import android.database.sqlite.SQLiteDatabase; 
    import android.database.sqlite.SQLiteOpenHelper; 
    import android.database.sqlite.SQLiteDatabase.CursorFactory; 

    public class SQLiteAdapter { 

     public static final String MYDATABASE_NAME = "SCORING"; 
     public static final String MYDATABASE_TABLE = "SCORING_TABLE"; 
     public static final int MYDATABASE_VERSION = 3; 
     public static final String KEY_ID = "_id"; 
     public static final String KEY_CONTENT1 = "Content1"; 
     public static final String KEY_CONTENT2 = "Content2"; 
     public static final String KEY_CONTENT3 = "Content3"; 

     //create table SCORING (ID integer primary key, Content text not null); 
     private static final String SCRIPT_CREATE_DATABASE = 
     "create table " + MYDATABASE_TABLE + " (" 
     + KEY_ID + " integer primary key autoincrement, " 
     + KEY_CONTENT1 + " text not null, " 
     + KEY_CONTENT2 + " text not null, " 
     + KEY_CONTENT3 + "text not null);"; 

     private SQLiteHelper sqLiteHelper; 
     private SQLiteDatabase sqLiteDatabase; 

     private Context context; 

     public SQLiteAdapter(Context c){ 
     context = c; 
     } 

     public SQLiteAdapter openToRead() throws android.database.SQLException { 
     sqLiteHelper = new SQLiteHelper(context, MYDATABASE_NAME, null,         MYDATABASE_VERSION); 
      sqLiteDatabase = sqLiteHelper.getReadableDatabase(); 
     return this; 
      } 

     public SQLiteAdapter openToWrite() throws android.database.SQLException { 
     sqLiteHelper = new SQLiteHelper(context, MYDATABASE_NAME, null,    MYDATABASE_VERSION); 
     sqLiteDatabase = sqLiteHelper.getWritableDatabase(); 
     return this; 
     } 

     public void close(){ 
     sqLiteHelper.close(); 
     } 

     public long insert(String content1, String content2, String content3){   

     ContentValues contentValues = new ContentValues(); 
     contentValues.put(KEY_CONTENT1, content1); 
     contentValues.put(KEY_CONTENT2, content2); 
     contentValues.put(KEY_CONTENT3, content3); 
     return sqLiteDatabase.insert(MYDATABASE_TABLE, null, contentValues); 
     } 

     public int deleteAll(){ 
     return sqLiteDatabase.delete(MYDATABASE_TABLE, null, null); 
     } 

     public Cursor queueAll(){ 
     String[] columns = new String[]{KEY_ID, KEY_CONTENT1, KEY_CONTENT2, KEY_CONTENT3}; 
     Cursor cursor = sqLiteDatabase.query(MYDATABASE_TABLE, columns, 
      null, null, null, null, null); 

     return cursor; 
     } 

     public class SQLiteHelper extends SQLiteOpenHelper { 

     public SQLiteHelper(Context context, String name, 
      CursorFactory factory, int version) { 
     super(context, name, factory, version); 
     } 

     @Override 
     public void onCreate(SQLiteDatabase db) { 
     // TODO Auto-generated method stub 
     db.execSQL(SCRIPT_CREATE_DATABASE); 
     } 

     @Override 
     public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { 
     // TODO Auto-generated method stub 
     // If you need to add a column 
    if (newVersion > oldVersion) { 
     db.execSQL("ALTER TABLE SCORING_TABLE ADD COLUMN Content4 TEXT"); 
    } 
     } 
     } 
     } 

现在我得到了第二个错误在这一部分

02-12 00:54:03.516: D/AndroidRuntime(359): Shutting down VM 
02-12 00:54:03.516: W/dalvikvm(359): threadid=1: thread exiting with uncaught exception  (group=0x40015560) 
02-12 00:54:03.546: E/AndroidRuntime(359): FATAL EXCEPTION: main 
02-12 00:54:03.546: E/AndroidRuntime(359): java.lang.ArrayIndexOutOfBoundsException 
02-12 00:54:03.546: E/AndroidRuntime(359): at android.widget.SimpleCursorAdapter.bindView(SimpleCursorAdapter.java:130) 
02-12 00:54:03.546: E/AndroidRuntime(359): at android.widget.CursorAdapter.getView(CursorAdapter.java:186) 
02-12 00:54:03.546: E/AndroidRuntime(359): at android.widget.AbsListView.obtainView(AbsListView.java:1430) 
02-12 00:54:03.546: E/AndroidRuntime(359): at android.widget.ListView.makeAndAddView(ListView.java:1745) 
02-12 00:54:03.546: E/AndroidRuntime(359): at android.widget.ListView.fillDown(ListView.java:670) 
02-12 00:54:03.546: E/AndroidRuntime(359): at android.widget.ListView.fillFromTop(ListView.java:727) 
02-12 00:54:03.546: E/AndroidRuntime(359): at android.widget.ListView.layoutChildren(ListView.java:1584) 
02-12 00:54:03.546: E/AndroidRuntime(359): at android.widget.AbsListView.onLayout(AbsListView.java:1260) 
02-12 00:54:03.546: E/AndroidRuntime(359): at android.view.View.layout(View.java:7175) 
02-12 00:54:03.546: E/AndroidRuntime(359): at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1254) 
02-12 00:54:03.546: E/AndroidRuntime(359): at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1130) 
02-12 00:54:03.546: E/AndroidRuntime(359): at android.widget.LinearLayout.onLayout(LinearLayout.java:1047) 
02-12 00:54:03.546: E/AndroidRuntime(359): at android.view.View.layout(View.java:7175) 
02-12 00:54:03.546: E/AndroidRuntime(359): at android.widget.FrameLayout.onLayout(FrameLayout.java:338) 
02-12 00:54:03.546: E/AndroidRuntime(359): at android.view.View.layout(View.java:7175) 
02-12 00:54:03.546: E/AndroidRuntime(359): at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1254) 
02-12 00:54:03.546: E/AndroidRuntime(359): at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1130) 
02-12 00:54:03.546: E/AndroidRuntime(359): at android.widget.LinearLayout.onLayout(LinearLayout.java:1047) 
02-12 00:54:03.546: E/AndroidRuntime(359): at android.view.View.layout(View.java:7175) 
02-12 00:54:03.546: E/AndroidRuntime(359): at android.widget.FrameLayout.onLayout(FrameLayout.java:338) 
02-12 00:54:03.546: E/AndroidRuntime(359): at android.view.View.layout(View.java:7175) 
02-12 00:54:03.546: E/AndroidRuntime(359): at android.view.ViewRoot.performTraversals(ViewRoot.java:1140) 
02-12 00:54:03.546: E/AndroidRuntime(359): at android.view.ViewRoot.handleMessage(ViewRoot.java:1859) 
02-12 00:54:03.546: E/AndroidRuntime(359): at android.os.Handler.dispatchMessage(Handler.java:99) 
02-12 00:54:03.546: E/AndroidRuntime(359): at android.os.Looper.loop(Looper.java:123) 
02-12 00:54:03.546: E/AndroidRuntime(359): at android.app.ActivityThread.main(ActivityThread.java:3683) 
02-12 00:54:03.546: E/AndroidRuntime(359): at java.lang.reflect.Method.invokeNative(Native Method) 
02-12 00:54:03.546: E/AndroidRuntime(359): at  java.lang.reflect.Method.invoke(Method.java:507) 
02-12 00:54:03.546: E/AndroidRuntime(359): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839) 
02-12 00:54:03.546: E/AndroidRuntime(359): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597) 
02-12 00:54:03.546: E/AndroidRuntime(359): at dalvik.system.NativeStart.main(Native Method) 
02-12 00:54:05.646: I/Process(359): Sending signal. PID: 359 SIG: 9 

和它的类:

 package com.example.database; 



    import com.example.database.R; 
    import android.app.Activity; 
    import android.content.Intent; 
    import android.database.Cursor; 
    import android.os.Bundle; 
    import android.view.View; 
    import android.widget.Button; 
    import android.widget.EditText; 
    import android.widget.ListView; 
    import android.widget.SimpleCursorAdapter; 
    import android.widget.TextView; 
    import android.widget.Toast; 

    public class AndroidSQLite extends Activity { 

    EditText inputContent2; 
    TextView textView1, textView2; 
    Button buttonAdd, buttonDeleteAll; 

    private SQLiteAdapter mySQLiteAdapter; 
    ListView listContent; 

    SimpleCursorAdapter cursorAdapter; 
    Cursor cursor; 

     /** Called when the activity is first created. */ 
     @Override 
     public void onCreate(Bundle savedInstanceState) { 
      super.onCreate(savedInstanceState); 
      setContentView(R.layout.main); 


      textView1 = (TextView)findViewById(R.id.textView1); 
      textView2 = (TextView)findViewById(R.id.textView2); 
      inputContent2 = (EditText)findViewById(R.id.content2); 
      buttonAdd = (Button)findViewById(R.id.add); 
      buttonDeleteAll = (Button)findViewById(R.id.deleteall); 
      listContent = (ListView)findViewById(R.id.contentlist); 

      mySQLiteAdapter = new SQLiteAdapter(this); 
      mySQLiteAdapter.openToWrite(); 

      cursor = mySQLiteAdapter.queueAll(); 
      String[] from = new String[]{SQLiteAdapter.KEY_ID, SQLiteAdapter.KEY_CONTENT1,   SQLiteAdapter.KEY_CONTENT2}; 
      int[] to = new int[]{R.id.id, R.id.text1, R.id.text2, R.id.text3}; 
      cursorAdapter = 
      new SimpleCursorAdapter(this, R.layout.row, cursor, from, to); 
      listContent.setAdapter(cursorAdapter); 

      buttonAdd.setOnClickListener(buttonAddOnClickListener); 
      buttonDeleteAll.setOnClickListener(buttonDeleteAllOnClickListener); 



     } 

     Button.OnClickListener buttonAddOnClickListener 
     = new Button.OnClickListener(){ 

     @Override 
     public void onClick(View arg0) { 
     // TODO Auto-generated method stub 
      int a=Integer.parseInt(textView1.getText().toString()); 
      int b=a+2; 
      String s1 = String.valueOf(b); 
      textView1.setText(s1); 
     Toast.makeText(getApplicationContext(), "Wrong", 
        Toast.LENGTH_SHORT).show(); 

     String data1 = textView1.getText().toString(); 
     String data2 = inputContent2.getText().toString(); 
     String data3 = textView2.getText().toString(); 
     mySQLiteAdapter.insert(data1, data2, data3); 
     updateList(); 



     } 

     }; 


     Button.OnClickListener buttonDeleteAllOnClickListener 
     = new Button.OnClickListener(){ 

     @Override 
     public void onClick(View arg0) { 
     // TODO Auto-generated method stub 
     mySQLiteAdapter.deleteAll(); 
     updateList(); 
     } 

     }; 

    @Override 
    protected void onDestroy() { 
     // TODO Auto-generated method stub 
     super.onDestroy(); 
     mySQLiteAdapter.close(); 
    }   



    private void updateList(){ 
     cursor.requery(); 
     } 

    } 

作为XML布局这里是:

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:orientation="vertical" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    > 


<TextView 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:text="hello" 
    /> 
<TextView 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:text="Enter content of column 1" 
    /> 

<TextView 
    android:id="@+id/textView1" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:text="0" 
    android:textAppearance="?android:attr/textAppearanceLarge" /> 

<TextView 
     android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:text="Enter content of column 2" 
    /> 
<EditText 
    android:id="@+id/content2" 
    android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
    /> 

<TextView 
    android:id="@+id/textView2" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:text="Large Text" 
    android:textAppearance="?android:attr/textAppearanceLarge" /> 

<Button 
    android:id="@+id/add" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:text="Add" 
    /> 

<Button 
    android:id="@+id/deleteall" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:text="Delete All" 
    /> 
<ListView 
android:id="@+id/contentlist" 
android:layout_width="fill_parent" 
android:layout_height="fill_parent"/> 
</LinearLayout> 
+3

发布您的LogCat错误,它们会准确告诉您发生错误的位置。请花点时间正确格式化并缩进代码,然后在代码块(Ctrl + K)中将其发布到此处。 – Sam 2013-02-11 16:38:17

+0

看看你的日志。你会选择“引起”,它会给你一个行号。有时它不是确切的,但它告诉你在哪里看。 – Rarw 2013-02-11 16:39:17

+0

,你认为它可能与实际发布堆栈跟踪有关吗? – njzk2 2013-02-11 16:46:07

回答

3

很简单,你有三个值在from阵列中,但在to中有四个。这两个阵列必须匹配(使用一对一关系):

String[] from = new String[]{SQLiteAdapter.KEY_ID, SQLiteAdapter.KEY_CONTENT1, SQLiteAdapter.KEY_CONTENT2}; 
int[] to = new int[]{R.id.id, R.id.text1, R.id.text2, R.id.text3}; 
+0

非常感谢你Sam现在我可以搬家了 – androidnoob 2013-02-11 17:16:09

+0

Progress is always一件好事。 :)请点击复选标记接受此答案。 – Sam 2013-02-11 17:20:48