Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ssl/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在Android中获取InvocationTargetException_Android_Nullpointerexception - Fatal编程技术网

在Android中获取InvocationTargetException

在Android中获取InvocationTargetException,android,nullpointerexception,Android,Nullpointerexception,我不知道为什么会出现这个错误。我试图在Log.v中显示它,但我始终获得的ID为0。我正在尝试将一个人添加到我的Sqlite数据库中 package com.medalle; import android.content.ContentValues; import android.content.Context; import android.database.SQLException; import android.data

我不知道为什么会出现这个错误。我试图在Log.v中显示它,但我始终获得的ID为0。我正在尝试将一个人添加到我的Sqlite数据库中

       package com.medalle;

       import android.content.ContentValues;
       import android.content.Context;
       import android.database.SQLException;
       import android.database.sqlite.SQLiteDatabase;
       import android.database.sqlite.SQLiteOpenHelper;
       import android.util.Log;

public class DBAdapter {
public static final String KEY_ROWID = "id";
public static final String KEY_NAME = "name";
public static final String KEY_STATUS = "status";
private static final String TAG = "DBAdapter";
private static final String DATABASE_NAME = "myDB";
private static final String DATABASE_TABLE = "person";
private static final int DATABASE_VERSION = 1;
private static final String DATABASE_CREATE =
        "create table contacts (_id integer primary key autoincrement, "
                + "name text not null, status integer not null);";
private final Context context;
private DatabaseHelper DBHelper;
private SQLiteDatabase db;

public DBAdapter(Context ctx)
{
    this.context = ctx;
    DBHelper = new DatabaseHelper(context);
}

private static class DatabaseHelper extends SQLiteOpenHelper
{
    DatabaseHelper(Context context)
    {
        super(context, DATABASE_NAME, null, DATABASE_VERSION);
    }

    @Override
    public void onCreate(SQLiteDatabase db)
    {
        try {
            db.execSQL(DATABASE_CREATE);
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion)
    {
        Log.w(TAG, "Upgrading database from version " + oldVersion + " to "
                + newVersion + ", which will destroy all old data");
        db.execSQL("DROP TABLE IF EXISTS contacts");
        onCreate(db);
    }
}


//---opens the database---
public DBAdapter open() throws SQLException
{
    db = DBHelper.getWritableDatabase();
    return this;
}


//---closes the database---
public void close()
{
    DBHelper.close();
}

// add new person

public long addNewPerson(int id, String name, int status){
    ContentValues initialValues = new ContentValues();
    initialValues.put(KEY_NAME,name);
    initialValues.put(KEY_STATUS,status);
    return db.insert(DATABASE_TABLE, null, initialValues);
}
 }

    package com.medalle;

    public class Person {
int id;
String name;
int status;

public Person(){}

public Person (int id, String name, int status){
    super();
    this.id = id;
    this.name = name;
    this.status = status;
}

@Override
public String toString() {
    return "Person [id=" + id + ", name=" + name + ", status=" + status
            + "]";
    }
   }

   package com.medalle;

   import android.app.Activity;
   import android.os.Bundle;
   import android.util.Log;
   import android.view.View;
   import android.view.ViewGroup;
   import android.widget.Button;
   import android.widget.EditText;
   import android.widget.RadioButton;
   import android.widget.RadioGroup;
   import android.widget.RadioGroup.OnCheckedChangeListener;
   import android.widget.TextView;
   import android.widget.Toast;

     public class PersonLists extends Activity {
TextView personName, personId, personStatus;
RadioGroup radioStatus;
Button submit;
DBAdapter database;
int pos, pos1;

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

    personId = (TextView)findViewById(R.id.idTV);
    personStatus = (TextView)findViewById(R.id.statusTV);
    personName = (TextView)findViewById(R.id.nameTV);
   // name = (EditText)findViewById(R.id.pname);
   // id = (EditText)findViewById(R.id.pid);
    submit = (Button)findViewById(R.id.btnSubmit);
    radioStatus = (RadioGroup)findViewById(R.id.radioStatusGroup);

    radioStatus.setOnCheckedChangeListener(new OnCheckedChangeListener(){


        public void onCheckedChanged(RadioGroup group, int checkedId){

            pos= radioStatus.indexOfChild(findViewById(checkedId)); 
            pos1=radioStatus.indexOfChild(findViewById(radioStatus.getCheckedRadioButtonId()));

            switch (pos)
            {
            case 0 :
                Toast.makeText(getBaseContext(), "You have Clicked Online",
                Toast.LENGTH_SHORT).show();
                break;
            case 1 :
                Toast.makeText(getBaseContext(), "You have Clicked Offline",
                Toast.LENGTH_SHORT).show();
                break;
            }
        }
    });
}
        public void addPerson(View v){
            EditText ed = (EditText)findViewById(R.id.pname);
            Person p = new Person();
            p.name = ed.getText().toString();
            Log.v("Checking name",p.name);
            Log.v("Checking id", Integer.toString(p.id));
            p.status = radioStatus.getCheckedRadioButtonId();
            database.addNewPerson(p.id, p.name, p.status);
            // p.status
            Toast.makeText(this, "Person Successfully Added",                 Toast.LENGTH_LONG).show();
    }       
         }

添加标记,将错误屏幕与文本分开,并提供任务的详细说明真正的问题是空指针异常。我们看不到你从哪里得到这个NPE,因为它不可见。。。