ContextWrapper.getResources 向 TableLayout 添加行时出现空指针
ContextWrapper.getResources nullpointer when adding rows to TableLayout
我已编辑问题以反映答案。每次调用 getArtists()
DatabaseHelper 方法时,我都会实例化一个 new Artists()
。所以我在DatabaseHelper
class中删除了getArtists()
中的artists = new Artists();
。我没有立即一遍又一遍地实例化它,而是学习了如何在构造函数中使用它的上下文。所以我在 DatabaseHelper
构造函数中得到了 Artists()
的引用,就像这样。
public DatabaseController(Context c){
myContext = c;
artists = (Artists) myContext;
}
// 原创 QUESTION/CODE
我在 android.content.ContextWrapper.getResources(ContextWrapper.java:89)
收到 NPE,我不知道发生了什么。许多搜索指出问题是在不是onCreate的地方调用了getResources,但我没有在任何地方调用getResources。
我正在尝试从数据库中获取数据并以编程方式从中填充 TableLayout。
这位艺术家Activity。
public class Artists extends AppCompatActivity
implements NavigationView.OnNavigationItemSelectedListener {
private DatabaseController dbcon;
public TableLayout table;
public static int artistPopulateCatcher = 0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_artists);
table = (TableLayout)findViewById(R.id.table_artists);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_artists);
ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
drawer.setDrawerListener(toggle);
toggle.syncState();
NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
navigationView.setNavigationItemSelectedListener(this);
if(artistPopulateCatcher == 0) {
dbcon = new DatabaseController(this);
dbcon.open();
dbcon.getArtists();
dbcon.close();
}
}
public void AddNewArtistDialog(View view) {
final AlertDialog.Builder builder = new AlertDialog.Builder(this);
final View layout = LayoutInflater.from(this).inflate(R.layout.addartistdialog, null);
builder.setView(layout)
.setPositiveButton("Submit", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
final EditText name = (EditText) layout.findViewById(R.id.edittext_artist_name);
final EditText email = (EditText) layout.findViewById(R.id.edittext_artist_email);
final CheckBox is21 = (CheckBox) layout.findViewById(R.id.checkBox_is21);
String artistIs21;
String artistName;
String artistEmail;
if (is21.isChecked()){
artistIs21 = "Yes";
artistName = name.getText().toString();
artistEmail = email.getText().toString();
dbcon.open();
dbcon.AddArtist(artistName, artistIs21, artistEmail);
dbcon.close();
} else{
artistIs21 = "No";
artistName = name.getText().toString();
artistEmail = email.getText().toString();
dbcon.open();
dbcon.AddArtist(artistName, artistIs21, artistEmail);
dbcon.close();
}
dialog.cancel();
}
})
.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.cancel();
}
});
builder.show();
}
public void PopulateArtist(String artist, int draw, String is21, String email){
Log.d("TAG", artist+", "+draw+", "+is21);
TableRow tr = new TableRow(this);
tr.setLayoutParams(new TableLayout.LayoutParams(TableLayout.LayoutParams.MATCH_PARENT, TableLayout.LayoutParams.WRAP_CONTENT));
TextView artistName = new TextView(this);
TextView artistDraw = new TextView(this);
TextView artistIs21 = new TextView(this);
TextView artistEmail = new TextView(this);
artistName.setText(artist);
artistDraw.setText(String.valueOf(draw));
artistIs21.setText(is21);
artistEmail.setText(email);
tr.addView(artistName);
tr.addView(artistDraw);
tr.addView(artistIs21);
tr.addView(artistEmail);
table.addView(tr, new TableLayout.LayoutParams(TableLayout.LayoutParams.MATCH_PARENT, TableLayout.LayoutParams.MATCH_PARENT));
}
我的数据库控制器:
public class DatabaseController{
private DatabaseHelper dbHelper;
public Context myContext;
private SQLiteDatabase db;
public Artists artists;
public DatabaseController(Context c){
myContext = c;
}
public DatabaseController open() throws SQLException{
dbHelper = new DatabaseHelper(myContext);
db = dbHelper.getWritableDatabase();
return this;
}
public void close(){
dbHelper.close();
}
public void AddArtist(String name, String is21, String email){
ContentValues values = new ContentValues();
values.put(DatabaseHelper.KEY_NAME, name);
values.put(DatabaseHelper.KEY_IS21, is21);
values.put(DatabaseHelper.KEY_EMAIL, email);
values.put(DatabaseHelper.KEY_DRAW, 0);
db.insert(DatabaseHelper.TABLE_ARTISTS, null, values);
}
public Cursor getArtists() {
artists.artistPopulateCatcher = 1;
artists = new Artists();
Cursor cursor = db.rawQuery("SELECT * FROM Artists", null);
cursor.moveToFirst();
while (cursor.isAfterLast() == false){
String name = cursor.getString(cursor.getColumnIndex("Name"));
int draw = cursor.getInt(cursor.getColumnIndex("Draw"));
String is21 = cursor.getString(cursor.getColumnIndex("Is21"));
String email = cursor.getString(cursor.getColumnIndex("Email"));
artists.PopulateArtist(name, draw, is21, email);
cursor.moveToNext();
}
return cursor;
}
public int updateArtist(int _id, String artistName, int draw, String is21, String email){
ContentValues cv = new ContentValues();
cv.put(dbHelper.KEY_ID, _id);
cv.put(dbHelper.KEY_NAME, artistName);
cv.put(dbHelper.KEY_DRAW, draw);
cv.put(dbHelper.KEY_IS21, is21);
cv.put(dbHelper.KEY_EMAIL, email);
int i = db.update(dbHelper.TABLE_ARTISTS, cv, dbHelper.KEY_ID + " = "+_id, null);
return i;
}
}
这是堆栈跟踪:
java.lang.RuntimeException: Unable to start activity ComponentInfo{scarycat.promotertools/scarycat.promotertools.Artists}: java.lang.NullPointerException
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2237)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2286)
at android.app.ActivityThread.access0(ActivityThread.java:144)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1246)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:212)
at android.app.ActivityThread.main(ActivityThread.java:5137)
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:902)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:718)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.NullPointerException
at android.content.ContextWrapper.getResources(ContextWrapper.java:89)
at android.view.ContextThemeWrapper.getResources(ContextThemeWrapper.java:78)
at android.support.v7.app.AppCompatActivity.getResources(AppCompatActivity.java:542)
at android.view.View.<init>(View.java:3569)
at android.view.ViewGroup.<init>(ViewGroup.java:459)
at android.widget.LinearLayout.<init>(LinearLayout.java:168)
at android.widget.TableRow.<init>(TableRow.java:61)
at scarycat.promotertools.Artists.PopulateArtist(Artists.java:112)
at scarycat.promotertools.DatabaseController.getArtists(DatabaseController.java:53)
at scarycat.promotertools.Artists.onCreate(Artists.java:55)
at android.app.Activity.performCreate(Activity.java:5231)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2201)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2286)
at android.app.ActivityThread.access0(ActivityThread.java:144)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1246)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:212)
at android.app.ActivityThread.main(ActivityThread.java:5137)
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:902)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:718)
at dalvik.system.NativeStart.main(Native Method)
我在 DatabaseHelper 的 getArtists() 方法中实例化了一个新的 Artists()。我在构造函数中删除了那一行我把 artists = (Artists) myContext;
我已编辑问题以反映答案。每次调用 getArtists()
DatabaseHelper 方法时,我都会实例化一个 new Artists()
。所以我在DatabaseHelper
class中删除了getArtists()
中的artists = new Artists();
。我没有立即一遍又一遍地实例化它,而是学习了如何在构造函数中使用它的上下文。所以我在 DatabaseHelper
构造函数中得到了 Artists()
的引用,就像这样。
public DatabaseController(Context c){
myContext = c;
artists = (Artists) myContext;
}
// 原创 QUESTION/CODE
我在 android.content.ContextWrapper.getResources(ContextWrapper.java:89)
收到 NPE,我不知道发生了什么。许多搜索指出问题是在不是onCreate的地方调用了getResources,但我没有在任何地方调用getResources。
我正在尝试从数据库中获取数据并以编程方式从中填充 TableLayout。
这位艺术家Activity。
public class Artists extends AppCompatActivity
implements NavigationView.OnNavigationItemSelectedListener {
private DatabaseController dbcon;
public TableLayout table;
public static int artistPopulateCatcher = 0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_artists);
table = (TableLayout)findViewById(R.id.table_artists);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_artists);
ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
drawer.setDrawerListener(toggle);
toggle.syncState();
NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
navigationView.setNavigationItemSelectedListener(this);
if(artistPopulateCatcher == 0) {
dbcon = new DatabaseController(this);
dbcon.open();
dbcon.getArtists();
dbcon.close();
}
}
public void AddNewArtistDialog(View view) {
final AlertDialog.Builder builder = new AlertDialog.Builder(this);
final View layout = LayoutInflater.from(this).inflate(R.layout.addartistdialog, null);
builder.setView(layout)
.setPositiveButton("Submit", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
final EditText name = (EditText) layout.findViewById(R.id.edittext_artist_name);
final EditText email = (EditText) layout.findViewById(R.id.edittext_artist_email);
final CheckBox is21 = (CheckBox) layout.findViewById(R.id.checkBox_is21);
String artistIs21;
String artistName;
String artistEmail;
if (is21.isChecked()){
artistIs21 = "Yes";
artistName = name.getText().toString();
artistEmail = email.getText().toString();
dbcon.open();
dbcon.AddArtist(artistName, artistIs21, artistEmail);
dbcon.close();
} else{
artistIs21 = "No";
artistName = name.getText().toString();
artistEmail = email.getText().toString();
dbcon.open();
dbcon.AddArtist(artistName, artistIs21, artistEmail);
dbcon.close();
}
dialog.cancel();
}
})
.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.cancel();
}
});
builder.show();
}
public void PopulateArtist(String artist, int draw, String is21, String email){
Log.d("TAG", artist+", "+draw+", "+is21);
TableRow tr = new TableRow(this);
tr.setLayoutParams(new TableLayout.LayoutParams(TableLayout.LayoutParams.MATCH_PARENT, TableLayout.LayoutParams.WRAP_CONTENT));
TextView artistName = new TextView(this);
TextView artistDraw = new TextView(this);
TextView artistIs21 = new TextView(this);
TextView artistEmail = new TextView(this);
artistName.setText(artist);
artistDraw.setText(String.valueOf(draw));
artistIs21.setText(is21);
artistEmail.setText(email);
tr.addView(artistName);
tr.addView(artistDraw);
tr.addView(artistIs21);
tr.addView(artistEmail);
table.addView(tr, new TableLayout.LayoutParams(TableLayout.LayoutParams.MATCH_PARENT, TableLayout.LayoutParams.MATCH_PARENT));
}
我的数据库控制器:
public class DatabaseController{
private DatabaseHelper dbHelper;
public Context myContext;
private SQLiteDatabase db;
public Artists artists;
public DatabaseController(Context c){
myContext = c;
}
public DatabaseController open() throws SQLException{
dbHelper = new DatabaseHelper(myContext);
db = dbHelper.getWritableDatabase();
return this;
}
public void close(){
dbHelper.close();
}
public void AddArtist(String name, String is21, String email){
ContentValues values = new ContentValues();
values.put(DatabaseHelper.KEY_NAME, name);
values.put(DatabaseHelper.KEY_IS21, is21);
values.put(DatabaseHelper.KEY_EMAIL, email);
values.put(DatabaseHelper.KEY_DRAW, 0);
db.insert(DatabaseHelper.TABLE_ARTISTS, null, values);
}
public Cursor getArtists() {
artists.artistPopulateCatcher = 1;
artists = new Artists();
Cursor cursor = db.rawQuery("SELECT * FROM Artists", null);
cursor.moveToFirst();
while (cursor.isAfterLast() == false){
String name = cursor.getString(cursor.getColumnIndex("Name"));
int draw = cursor.getInt(cursor.getColumnIndex("Draw"));
String is21 = cursor.getString(cursor.getColumnIndex("Is21"));
String email = cursor.getString(cursor.getColumnIndex("Email"));
artists.PopulateArtist(name, draw, is21, email);
cursor.moveToNext();
}
return cursor;
}
public int updateArtist(int _id, String artistName, int draw, String is21, String email){
ContentValues cv = new ContentValues();
cv.put(dbHelper.KEY_ID, _id);
cv.put(dbHelper.KEY_NAME, artistName);
cv.put(dbHelper.KEY_DRAW, draw);
cv.put(dbHelper.KEY_IS21, is21);
cv.put(dbHelper.KEY_EMAIL, email);
int i = db.update(dbHelper.TABLE_ARTISTS, cv, dbHelper.KEY_ID + " = "+_id, null);
return i;
}
}
这是堆栈跟踪:
java.lang.RuntimeException: Unable to start activity ComponentInfo{scarycat.promotertools/scarycat.promotertools.Artists}: java.lang.NullPointerException
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2237)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2286)
at android.app.ActivityThread.access0(ActivityThread.java:144)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1246)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:212)
at android.app.ActivityThread.main(ActivityThread.java:5137)
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:902)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:718)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.NullPointerException
at android.content.ContextWrapper.getResources(ContextWrapper.java:89)
at android.view.ContextThemeWrapper.getResources(ContextThemeWrapper.java:78)
at android.support.v7.app.AppCompatActivity.getResources(AppCompatActivity.java:542)
at android.view.View.<init>(View.java:3569)
at android.view.ViewGroup.<init>(ViewGroup.java:459)
at android.widget.LinearLayout.<init>(LinearLayout.java:168)
at android.widget.TableRow.<init>(TableRow.java:61)
at scarycat.promotertools.Artists.PopulateArtist(Artists.java:112)
at scarycat.promotertools.DatabaseController.getArtists(DatabaseController.java:53)
at scarycat.promotertools.Artists.onCreate(Artists.java:55)
at android.app.Activity.performCreate(Activity.java:5231)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2201)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2286)
at android.app.ActivityThread.access0(ActivityThread.java:144)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1246)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:212)
at android.app.ActivityThread.main(ActivityThread.java:5137)
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:902)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:718)
at dalvik.system.NativeStart.main(Native Method)
我在 DatabaseHelper 的 getArtists() 方法中实例化了一个新的 Artists()。我在构造函数中删除了那一行我把 artists = (Artists) myContext;