0

每次我运行我的应用程序时,Null Pointer 都会在 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>
4

1 回答 1

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};
于 2013-02-11T17:08:36.600 回答