"java.lang.NullPointerException" 尝试向 sqlite 插入数据时出错 (Android)

"java.lang.NullPointerException" Error when trying to insert data to sqlite (Android)

我遇到错误:

1566-1566/com.example.rom.romproject E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: com.example.rom.romproject, PID: 1566
java.lang.IllegalStateException: Could not execute method of the activity
        at android.view.View.onClick(View.java:3823)
        at android.view.View.performClick(View.java:4438)
        at android.view.View$PerformClick.run(View.java:18422)
        at android.os.Handler.handleCallback(Handler.java:733)
        at android.os.Handler.dispatchMessage(Handler.java:95)
        at android.os.Looper.loop(Looper.java:136)
        at android.app.ActivityThread.main(ActivityThread.java:5017)
        at java.lang.reflect.Method.invokeNative(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:515)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
        at dalvik.system.NativeStart.main(Native Method)
 Caused by: java.lang.reflect.InvocationTargetException
        at java.lang.reflect.Method.invokeNative(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:515)
        at android.view.View.onClick(View.java:3818)
            at android.view.View.performClick(View.java:4438)
            at android.view.View$PerformClick.run(View.java:18422)
            at android.os.Handler.handleCallback(Handler.java:733)
            at android.os.Handler.dispatchMessage(Handler.java:95)
            at android.os.Looper.loop(Looper.java:136)
            at android.app.ActivityThread.main(ActivityThread.java:5017)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:515)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
            at dalvik.system.NativeStart.main(Native Method)
 Caused by: java.lang.NullPointerException
        at com.example.rom.romproject.ContactView.contactFavorite(ContactView.java:37)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:515)
            at android.view.View.onClick(View.java:3818)
            at android.view.View.performClick(View.java:4438)
            at android.view.View$PerformClick.run(View.java:18422)
            at android.os.Handler.handleCallback(Handler.java:733)
            at android.os.Handler.dispatchMessage(Handler.java:95)
            at android.os.Looper.loop(Looper.java:136)
            at android.app.ActivityThread.main(ActivityThread.java:5017)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:515)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
            at dalvik.system.NativeStart.main(Native Method)

当我单击我创建的按钮时会发生这种情况。 该按钮的目的是将数据插入我的 sql table.

SQL class :

public class sqlDatabaseAdapter
{

    sqlHelper helper;

    public sqlDatabaseAdapter(Context context)
    {
        helper = new sqlHelper(context);
    }

    public long insertData(String name, String phone)
    {
        SQLiteDatabase db = helper.getWritableDatabase();
        ContentValues content = new ContentValues();
        content.put(sqlHelper.NAME, name);
        content.put(sqlHelper.PHONE, phone);


        return db.insert(helper.TABLE_NAME, null, content);
    }




    static class sqlHelper extends SQLiteOpenHelper
    {

        static final String DATABASE_NAME = "ContactDB";
        static final String TABLE_NAME = "Favorites";
        static final int DB_VERSION = 1;
        static final String UID = "_id";
        static final String NAME = "Name";
        static final String PHONE = "Phone";
        static final String CREATE_TABLE = "CREATE TABLE "+ TABLE_NAME +" ("+UID+" INTEGER PRIMARY KEY AUTOINCREMENT, "+NAME+" VARCHAR(255), "+PHONE+" VARCHAR(255));";
        static final String DROP_TABLE = "DROP TABLE IF EXISTS"+TABLE_NAME;
        private Context context;

        public sqlHelper(Context context)
        {
            super(context, DATABASE_NAME, null, DB_VERSION);
            this.context = context;
            Message.Message(context, "Constructor Called");
        }

        @Override
        public void onCreate(SQLiteDatabase db)
        {
            try {
                db.execSQL(CREATE_TABLE);
                Message.Message(context, "onCreate Called");
            } catch( SQLException e)
            {
                Message.Message(context, "" + e);
            }
        }

        @Override
        public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion)
        {
            try {
                db.execSQL(DROP_TABLE);
                onCreate(db);
                Message.Message(context, "OnUpgrade Called");
            } catch(SQLException e)
            {
                Message.Message(context, "" + e);
            }
        }
    }
}

现在,我不确定问题的根源,所以我将 post 我的两个活动。 顺便说一句:我试图插入 SQL 的信息是一个联系人姓名,phone (我从主 activity 列表视图中获取)。

主要 Activity(phone 联系人的列表视图):

public class MainActivity extends ListActivity {

    ListView l;
    Cursor cursor;
    SimpleCursorAdapter listAdapter;
    sqlDatabaseAdapter helper;


    @Override
    public int getSelectedItemPosition()
    {
        return super.getSelectedItemPosition();
    }

    @Override
    public long getSelectedItemId()
    {
        return super.getSelectedItemId();
    }




    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        cursor = getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, null, null, null);
        startManagingCursor(cursor);

        String[] from = {ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME,ContactsContract.CommonDataKinds.Phone.NUMBER, ContactsContract.CommonDataKinds.Phone._ID};
        int[] to = {android.R.id.text1,android.R.id.text2};

        listAdapter = new SimpleCursorAdapter(this,android.R.layout.simple_list_item_2,cursor,from,to);

        setListAdapter(listAdapter);
        l = getListView();
        l.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);

        helper = new sqlDatabaseAdapter(this);
        helper.helper.getWritableDatabase();

    }

    @Override
    protected void onListItemClick(ListView l, View v, int position, long id)
    {
        super.onListItemClick(l, v, position, id);

        TextView _tempName= (TextView)v.findViewById(android.R.id.text1);
        String _temp = _tempName.getText().toString();

        TextView _tempPhone = (TextView)v.findViewById(android.R.id.text2);
        String _temp2 = _tempPhone.getText().toString();

        Intent intent = new Intent(this, ContactView.class);
        intent.putExtra("contactName", _temp);
        intent.putExtra("contactPhone", _temp2);
        startActivity(intent);

    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }
}

第二个activity(按钮所在的位置):

public class ContactView extends ActionBarActivity {

    Button _Call;
    Button _Favorite;
    FavoriteContact contact = new FavoriteContact();
    sqlDatabaseAdapter helper;

    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_contact_view);

        _Call = (Button)findViewById(R.id.bCall);
        _Favorite = (Button)findViewById(R.id.bFavorite);

        contact.setName(getIntent().getExtras().getString("contactName"));
        contact.setPhone(getIntent().getExtras().getString("contactPhone"));

        setTitle(contact.getName());
    }

    public void contactFavorite(View view)
    {
        long id = 0L;
        id = helper.insertData(contact.getName(), contact.getPhone());
        /*
        if( id < 0)
        {
            Message.Message(this, "Unsuccessful");
        }
        else
        {
            Message.Message(this, "Successfully inserted to favorite contacts ");
        }
        */
    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_contact_view, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }
}

我创建的联系人 class 和 FavoriteContact class : (favoritecontact extends Contact)

public class Contact
{
    private String Name = null;
    private String Phone = null;

    public String getPhone() {
        return Phone;
    }

    public void setPhone(String phone) {
        Phone = phone;
    }

    public String getName() {
        return Name;
    }

    public void setName(String name) {
        Name = name;
    }
}

public class FavoriteContact extends Contact
{
    private Boolean isFavorite;

    public Boolean getIsFavorite() {
        return isFavorite;
    }

    public void setIsFavorite(Boolean isFavorite) {
        this.isFavorite = isFavorite;
    }
}

我想我已经给了我需要的一切... 抱歉我的英语不好,这是我第一次post来这里,所以我不知道它是如何工作的:D 感谢您的每一点帮助。

变量助手从未在 ContactView 中初始化。