当我尝试添加联系人时,为什么我的应用程序崩溃了

时间:2014-01-15 15:26:57

标签: java android database

我真的无法在这里找到问题所在。该应用程序运行,我可以转到add_form,但每当我尝试保存联系人(整个事情是用法语写的,对不起)时,应用程序就会崩溃。我很感激一些帮助。

这是我的主要

package com.example.address;

import android.app.ListActivity;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.view.LayoutInflater;
import android.widget.AdapterView;
import android.widget.CursorAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

public class AddBook extends ListActivity {
    public final static String ID_EXTRA="apt.tutorial._ID";
    Cursor model=null;
    AddAdapter adapter=null;
    AddHelper helper=null;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        helper=new AddHelper(this);
        model=helper.getAll();
        startManagingCursor(model);
        adapter=new AddAdapter(model);
        setListAdapter(adapter);
    }

    @Override
    public void onDestroy() {
        super.onDestroy();

        helper.close();
    }

    @Override
    public void onListItemClick(ListView list, View view,
                                                            int position, long id) {
        Intent i=new Intent(AddBook.this, AddForm.class);

        i.putExtra(ID_EXTRA, String.valueOf(id));
        startActivity(i);
    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        new MenuInflater(this).inflate(R.menu.option, menu);

        return(super.onCreateOptionsMenu(menu));
    }


    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        if (item.getItemId()==R.id.add) {
            startActivity(new Intent(AddBook.this, AddForm.class));

            return(true);
        }

        return(super.onOptionsItemSelected(item));
    }

    class AddAdapter extends CursorAdapter {
        AddAdapter(Cursor c) {
            super(AddBook.this, c);
        }

        @Override
        public void bindView(View row, Context ctxt,
                                                 Cursor c) {
            AddHolder holder=(AddHolder)row.getTag();

            holder.populateFrom(c, helper);
        }

        @Override
        public View newView(Context ctxt, Cursor c,
                                                 ViewGroup parent) {
            LayoutInflater inflater=getLayoutInflater();
            View row=inflater.inflate(R.layout.row, parent, false);
            AddHolder holder=new AddHolder(row);

            row.setTag(holder);

            return(row);
        }
    }

    static class AddHolder {
        private TextView nom=null;
        private TextView prenom=null;
        private ImageView icon=null;

        AddHolder(View row) {
            nom=(TextView)row.findViewById(R.id.nom);
            prenom=(TextView)row.findViewById(R.id.prenom);
        }

        void populateFrom(Cursor c, AddHelper helper) {
            nom.setText(helper.getNom(c));
            prenom.setText(helper.getPrenom(c));

            if (helper.getSexe(c).equals("male")) {
                icon.setImageResource(R.drawable.male_hover);
            }
            else {
                icon.setImageResource(R.drawable.female_hover);
            }
        }
    }
}

以下是表单的代码

    package com.example.address;

import android.app.Activity;
import android.database.Cursor;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RadioGroup;
import android.widget.TextView;

public class AddForm extends Activity {

    EditText nom=null;
    EditText prenom=null;
    EditText rue=null;
    EditText ville=null;
    EditText province=null;
    EditText pays=null;
    EditText tele=null;
    EditText cell=null;
    EditText siteweb=null;
    EditText courriel=null;

    RadioGroup sexes=null;

    AddHelper helper=null;
    String AddId=null;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.add_form);

        helper=new AddHelper(this);

        nom=(EditText)findViewById(R.id.nom);
        rue=(EditText)findViewById(R.id.prenom);
        ville=(EditText)findViewById(R.id.ville);
        province=(EditText)findViewById(R.id.province);
        pays=(EditText)findViewById(R.id.pays);
        tele=(EditText)findViewById(R.id.tele);
        cell=(EditText)findViewById(R.id.cell);
        siteweb=(EditText)findViewById(R.id.siteweb);
        courriel=(EditText)findViewById(R.id.courriel);

        sexes=(RadioGroup)findViewById(R.id.sexe);

        Button save=(Button)findViewById(R.id.save);

        save.setOnClickListener(onSave);

        AddId=getIntent().getStringExtra(AddBook.ID_EXTRA);

        if (AddId!=null) {
            load();
        }
    }

    @Override
    public void onDestroy() {
        super.onDestroy();

        helper.close();
    }

    private void load() {
        Cursor c=helper.getById(AddId);

        c.moveToFirst();        
        nom.setText(helper.getNom(c));
        prenom.setText(helper.getPrenom(c));
        rue.setText(helper.getRue(c));
        ville.setText(helper.getVille(c));
        province.setText(helper.getProvince(c));
        pays.setText(helper.getPays(c));
        tele.setText(helper.getTele(c));
        cell.setText(helper.getCell(c));
        siteweb.setText(helper.getSiteweb(c));
        courriel.setText(helper.getCourriel(c));


        if (helper.getSexe(c).equals("male")) {
            sexes.check(R.id.male);
        }
        else {
            sexes.check(R.id.female);
        }

        c.close();
    }

    private View.OnClickListener onSave=new View.OnClickListener() {
        public void onClick(View v) {
            String sexe=null;

            switch (sexes.getCheckedRadioButtonId()) {
                case R.id.male:
                    sexe="male";
                    break;
                case R.id.female:
                    sexe="female";
                    break;
            }

            if (AddId==null) {
                helper.insert(nom.getText().toString(), prenom.getText().toString(), sexe, rue.getText().toString(),
                                ville.getText().toString(), province.getText().toString(), pays.getText().toString(),
                                tele.getText().toString(), cell.getText().toString(), siteweb.getText().toString(), 
                                courriel.getText().toString());
            }
            else {
                helper.update(AddId, nom.getText().toString(),  prenom.getText().toString(), sexe, rue.getText().toString(),
                        ville.getText().toString(), province.getText().toString(), pays.getText().toString(),
                        tele.getText().toString(), cell.getText().toString(), siteweb.getText().toString(), 
                        courriel.getText().toString());
            }

            finish();
        }
    };
}

数据库代码

package com.example.address;

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

class AddHelper extends SQLiteOpenHelper {
    private static final String DATABASE_NAME="addbook.db";
    private static final int SCHEMA_VERSION=1;

    public AddHelper(Context context) {
        super(context, DATABASE_NAME, null, SCHEMA_VERSION);
    }

     @Override
     public void onCreate(SQLiteDatabase db){
         db.execSQL("CREATE TABLE contacts (_id INTEGER PRIMARY KEY AUTOINCREMENT, nom TEXT, prenom TEXT, sexe TEXT, rue TEXT, ville TEXT, province TEXT, pays TEXT, tele TEXT, cell TEXT, siteweb TEXT, courriel TEXT);");
     }
     @Override
     public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion){
         //no-op, since will not be called until 2nd schema
         //version exists
     }

     public Cursor getAll() {
         return(getReadableDatabase()
                 .rawQuery("SELECT _id, nom, prenom, sexe, rue, ville, province, pays, tele, cell, siteweb, courriel FROM contacts ORDER BY nom",
                         null));
     }

     public void insert(String nom, String prenom, String sexe, String rue, String ville, String province, String pays, 
                        String tele, String cell, String siteweb, String courriel){
         ContentValues cv=new ContentValues();

         cv.put("nom", nom);
         cv.put("prenom", prenom);
         cv.put("sexe", sexe);
         cv.put("rue", rue);
         cv.put("ville", ville);
         cv.put("province", province);
         cv.put("pays", pays);
         cv.put("tele", tele);
         cv.put("cell", cell);
         cv.put("siteweb", siteweb);
         cv.put("courriel", courriel);

         getWritableDatabase().insert("contacts", "nom", cv);
     }

     public String getNom(Cursor c) {
         return(c.getString(1));
     }
     public String getPrenom(Cursor c) {
         return(c.getString(2));
     }
     public String getSexe(Cursor c) {
         return(c.getString(3));
     }
     public String getRue(Cursor c) {
         return(c.getString(4));
     }
     public String getVille(Cursor c) {
         return(c.getString(5));
     }
     public String getProvince(Cursor c) {
         return(c.getString(6));
     }
     public String getPays(Cursor c) {
         return(c.getString(7));
     }
     public String getTele(Cursor c) {
         return(c.getString(8));
     }
     public String getCell(Cursor c) {
         return(c.getString(9));
     }
     public String getSiteweb(Cursor c) {
         return(c.getString(10));
     }
     public String getCourriel(Cursor c) {
         return(c.getString(11));
     }

     public Cursor getById(String id) {
            String[] args={id};

            return(getReadableDatabase()
                    .rawQuery("SELECT _id, nom, prenom, sexe, rue, ville, province, pays, tele, cell, siteweb, courriel FROM contacts WHERE _ID=?",
                        args));
     }

     public void update(String id, String nom, String prenom, String sexe, String rue, String ville, String province, String pays, 
                String tele, String cell, String siteweb, String courriel) {
         ContentValues cv=new ContentValues();
         String[] args={id};

         cv.put("nom", nom);
         cv.put("prenom", prenom);
         cv.put("sexe", sexe);
         cv.put("rue", rue);
         cv.put("ville", ville);
         cv.put("province", province);
         cv.put("pays", pays);
         cv.put("tele", tele);
         cv.put("cell", cell);
         cv.put("siteweb", siteweb);
         cv.put("courriel", courriel);

         getWritableDatabase().update("contacts", cv, "_ID=?",
                 args);
     }

}

这是表单

的XML文件
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout android:layout_width="match_parent"
    android:layout_height="wrap_content" xmlns:android="http://schemas.android.com/apk/res/android" android:background="@color/lemon">
<TableLayout 
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginLeft="5sp"
    android:layout_marginRight="5sp"
    android:stretchColumns="1"
     >

    <TableRow android:paddingTop="5sp" android:paddingBottom="5sp">
        <TextView android:text="@string/nom" android:textColor="@color/darkbrown" />
        <EditText android:layout_height="40sp" android:id="@+id/nom" android:background="@color/beige"/>
    </TableRow>
    <TableRow android:paddingBottom="5sp">
        <TextView android:text="@string/prenom" android:textColor="@color/darkbrown" />
        <EditText android:layout_height="40sp" android:id="@+id/prenom" android:background="@color/beige" />
    </TableRow>
    <TableRow>
        <TextView android:text="@string/sexe" android:textColor="@color/darkbrown" />
        <RadioGroup android:id="@+id/sexe" android:orientation="horizontal">
            <RadioButton android:id="@+id/male" android:layout_marginLeft="50sp"  /> <!-- style="@style/male" -->
            <RadioButton android:id="@+id/female" android:layout_marginLeft="40sp"  /> <!-- style="@style/female" -->
        </RadioGroup>
    </TableRow>
    <TableRow android:layout_marginTop="2sp">
        <TextView android:text="@string/rue" android:textColor="@color/darkbrown" />
        <EditText android:layout_height="30sp" android:id="@+id/rue" android:textSize="11dp" android:background="@color/beige" />
    </TableRow>
    <TableRow android:layout_marginTop="2sp">
        <TextView android:text="@string/ville"  android:textColor="@color/darkbrown"/>
        <EditText android:layout_height="30sp" android:id="@+id/ville" android:textSize="11dp" android:background="@color/beige" />
    </TableRow>
    <TableRow android:layout_marginTop="2sp">
        <TextView android:text="@string/province" android:textColor="@color/darkbrown" />
        <EditText android:layout_height="30sp" android:id="@+id/province" android:textSize="11dp" android:background="@color/beige" />
    </TableRow>
    <TableRow android:layout_marginTop="2sp">
        <TextView android:text="@string/pays" android:textColor="@color/darkbrown" />
        <EditText android:layout_height="30sp" android:id="@+id/pays" android:textSize="11dp" android:background="@color/beige" />
    </TableRow>
    <TableRow android:layout_marginTop="3sp" android:paddingBottom="3sp">
        <TextView android:text="@string/tele" android:textColor="@color/darkbrown" />
        <EditText android:layout_height="40sp" android:id="@+id/tele" android:background="@color/beige" />
    </TableRow>
    <TableRow android:paddingBottom="3sp">
        <TextView android:text="@string/cell" android:textColor="@color/darkbrown" />
        <EditText android:layout_height="40sp" android:id="@+id/cell" android:background="@color/beige" />
    </TableRow>
    <TableRow android:paddingBottom="3sp">
        <TextView android:text="@string/siteweb" android:textColor="@color/darkbrown"/>
        <EditText android:layout_height="40sp" android:id="@+id/siteweb" android:background="@color/beige" />
    </TableRow>
    <TableRow android:paddingBottom="3sp">
        <TextView android:text="@string/courriel" android:textColor="@color/darkbrown"/>
        <EditText android:layout_height="40sp" android:id="@+id/courriel" android:background="@color/beige" />
    </TableRow>
    <Button android:id="@+id/save"   android:layout_width="fill_parent" 
        android:layout_height="35dp" android:text="@string/add" />
</TableLayout>
</LinearLayout>

这是你们要求的吗?这就是logcat给我的东西

01-16 15:14:58.127: W/dalvikvm(793): threadid=1: thread exiting with uncaught exception (group=0x41465700)
01-16 15:14:58.167: E/AndroidRuntime(793): FATAL EXCEPTION: main
01-16 15:14:58.167: E/AndroidRuntime(793): java.lang.NullPointerException
01-16 15:14:58.167: E/AndroidRuntime(793):  at com.example.address.AddForm$1.onClick(AddForm.java:107)
01-16 15:14:58.167: E/AndroidRuntime(793):  at android.view.View.performClick(View.java:4240)
01-16 15:14:58.167: E/AndroidRuntime(793):  at android.view.View$PerformClick.run(View.java:17721)
01-16 15:14:58.167: E/AndroidRuntime(793):  at android.os.Handler.handleCallback(Handler.java:730)
01-16 15:14:58.167: E/AndroidRuntime(793):  at android.os.Handler.dispatchMessage(Handler.java:92)
01-16 15:14:58.167: E/AndroidRuntime(793):  at android.os.Looper.loop(Looper.java:137)
01-16 15:14:58.167: E/AndroidRuntime(793):  at android.app.ActivityThread.main(ActivityThread.java:5103)
01-16 15:14:58.167: E/AndroidRuntime(793):  at java.lang.reflect.Method.invokeNative(Native Method)
01-16 15:14:58.167: E/AndroidRuntime(793):  at java.lang.reflect.Method.invoke(Method.java:525)
01-16 15:14:58.167: E/AndroidRuntime(793):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
01-16 15:14:58.167: E/AndroidRuntime(793):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
01-16 15:14:58.167: E/AndroidRuntime(793):  at dalvik.system.NativeStart.main(Native Method)
01-16 15:15:00.978: I/Process(793): Sending signal. PID: 793 SIG: 9
01-16 15:15:02.548: D/(836): HostConnection::get() New Host Connection established 0x2a1f78d8, tid 836

1 个答案:

答案 0 :(得分:0)

您需要将这些权限添加到Manifest.xml:

    <uses-permission android:name="android.permission.READ_PHONE_STATE" />       
    <uses-permission android:name="android.permission.WRITE_CONTACTS" />