我正在将文本设置为自定义对话框。我得到一个NullPointerException
. 单击a 时会调用该对话框listItem
。编辑,滚动到底部以查看更新的代码。或见这里:
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
HomeItem homeItem = (HomeItem) adapter.getItem(position);
DialogClass dialogClass = new DialogClass(databaseFightCard.this);
dialogClass.setDialog(homeItem.getHomeItemRedName(), homeItem.getHomeItemRedAge(), homeItem.getHomeItemRedRecord(),
homeItem.getHomeItemRedHeight(), homeItem.getHomeItemRedWeight(), homeItem.getHomeItemRedCity(), homeItem.getHomeItemRedExp(),
homeItem.getHomeItemBlueName(), homeItem.getHomeItemBlueAge(), homeItem.getHomeItemBlueRecord(), homeItem.getHomeItemBlueHeight(),
homeItem.getHomeItemBlueWeight(), homeItem.getHomeItemBlueCity(), homeItem.getHomeItemBlueExp());
dialogClass.show();
}
});
对话类
public class DialogClass extends Dialog implements View.OnClickListener {
public Activity c;
public Dialog d;
public Button yes, no;
public TextView rn, ra, rr, rh, rw, rc, re, bn, ba, br, bh, bw, bc, be;
public void setDialog(String redName, String redAge, String redRecord, String redHeight,
String redWeight, String redCity, String redExp,String blueName, String blueAge,
String blueRecord, String blueHeight,
String blueWeight, String blueCity, String blueExp){
rn = (TextView) findViewById(R.id.tvRName);
ra = (TextView) findViewById(R.id.tvRAge);
rr = (TextView) findViewById(R.id.tvRRecord);
rh = (TextView) findViewById(R.id.tvRHeight);
rw = (TextView) findViewById(R.id.tvRWeight);
rc = (TextView) findViewById(R.id.tvRCity);
re = (TextView) findViewById(R.id.tvRExp);
bn = (TextView) findViewById(R.id.tvBName);
ba = (TextView) findViewById(R.id.tvBAge);
br = (TextView) findViewById(R.id.tvBRecord);
bh = (TextView) findViewById(R.id.tvBHeight);
bw = (TextView) findViewById(R.id.tvBWeight);
bc = (TextView) findViewById(R.id.tvBCity);
be = (TextView) findViewById(R.id.tvBExp);
<----------Where the NullPointer is being thrown----------->
rn.setText(redName);
ra.setText(redAge);
rr.setText(redRecord);
rh.setText(redHeight);
rw.setText(redWeight);
rc.setText(redCity);
re.setText(redExp);
bn.setText(blueName);
ba.setText(blueAge);
br.setText(blueRecord);
bh.setText(blueHeight);
bw.setText(blueWeight);
bc.setText(blueCity);
be.setText(blueExp);
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.custom_dialog);
yes = (Button) findViewById(R.id.bPlay);
no = (Button) findViewById(R.id.bDone);
yes.setOnClickListener(this);
no.setOnClickListener(this);
}
public DialogClass(Activity a) {
super(a);
this.c = a;
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.bPlay:
dismiss();
break;
case R.id.bDone:
dismiss();
break;
default:
break;
}
dismiss();
}
}
日志猫
10-05 18:48:01.843 994-994/com.codealchemist.clashmma E/AndroidRuntime﹕ FATAL EXCEPTION: main
java.lang.NullPointerException
at com.codealchemist.clashmma.DialogClass.setDialog(DialogClass.java:43)
at com.codealchemist.clashmma.databaseFightCard$1.onItemClick(databaseFightCard.java:71)
at android.widget.AdapterView.performItemClick(AdapterView.java:298)
at android.widget.AbsListView.performItemClick(AbsListView.java:1100)
at android.widget.AbsListView$PerformClick.run(AbsListView.java:2749)
at android.widget.AbsListView$1.run(AbsListView.java:3423)
at android.os.Handler.handleCallback(Handler.java:725)
at android.os.Handler.dispatchMessage(Handler.java:92)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:5041)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
at dalvik.system.NativeStart.main(Native Method)
LogCat 是指向我DialogClass
的就行了
rn.setText(redName);
我从未将文本设置为自定义构建的对话框,所以请解释我做错了什么。
编辑由于黑带 这就是我尝试做的class member
。如果我的名声不明显,我是初学者,所以请解释一个更好的方法来做到这一点:
在我需要对话框的活动中更改了这一点
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
HomeItem homeItem = (HomeItem) adapter.getItem(position);
DialogClass dialogClass = new DialogClass(homeItem);
dialogClass.show();
}
});
在我的DialogClass
public DialogClass(HomeItem context) {
super(context);
this.hi = context;
}
然后在我onCreate
的我的DialogClass
:
rn.setText(hi.getHomeItemRedName());
bn.setText(hi.getHomeItemBlueName());
因为我必须使用 Context of HomeItem
,或者我所做的任何事情都必须让我的HomeItem
类扩展Context
。我不得不使用@Override
大约 80 种方法:
public class HomeItem extends Context {
private int HomeItemID;
private String HomeItemRedName, HomeItemRedAge, HomeItemRedRecord, HomeItemRedHeight, HomeItemRedWeight,
HomeItemRedCity, HomeItemRedExp;
private String HomeItemBlueName, HomeItemBlueAge, HomeItemBlueRecord, HomeItemBlueHeight, HomeItemBlueWeight,
HomeItemBlueCity, HomeItemBlueExp;
public int getHomeItemID() {
return HomeItemID;
}
public void setHomeItemID(int ID) {
this.HomeItemID = ID;
}
public String getHomeItemRedName() {
return HomeItemRedName;
}
public void setHomeItemRedName(String Name) {
this.HomeItemRedName = Name;
}
public String getHomeItemRedAge(){
return HomeItemRedAge;
}
public void setHomeItemRedAge(String Age){
if (Age == null)
this.HomeItemRedAge = "Unknown";
this.HomeItemRedAge = Age;
}
public String getHomeItemRedRecord(){
return HomeItemRedRecord;
}
public void setHomeItemRedRecord(String Record){
if (Record == null)
this.HomeItemRedRecord = "Unknown";
this.HomeItemRedRecord = Record;
}
public String getHomeItemRedHeight(){
return HomeItemRedHeight;
}
public void setHomeItemRedHeight(String Height){
if (Height == null)
this.HomeItemRedHeight = "Unknown";
this.HomeItemRedHeight = Height;
}
public String getHomeItemRedWeight(){
return HomeItemRedWeight;
}
public void setHomeItemRedWeight(String Weight){
if (Weight == null)
this.HomeItemRedWeight = "Unknown";
this.HomeItemRedWeight = Weight;
}
public String getHomeItemRedCity(){
return HomeItemRedCity;
}
public void setHomeItemRedCity(String City){
if (City == null)
this.HomeItemRedCity = "Unknown";
this.HomeItemRedCity = City;
}
public String getHomeItemRedExp(){
return HomeItemRedExp;
}
public void setHomeItemRedExp(String Exp){
if (Exp == null)
this.HomeItemRedExp = "Unknown";
this.HomeItemRedExp = Exp;
}
//Blue side
public String getHomeItemBlueName(){
return HomeItemBlueName;
}
public void setHomeItemBlueName(String Name){
this.HomeItemBlueName = Name;
}
public String getHomeItemBlueAge(){
return HomeItemBlueAge;
}
public void setHomeItemBlueAge(String Age){
if (Age == null)
this.HomeItemBlueAge = "Unknown";
this.HomeItemBlueAge = Age;
}
public String getHomeItemBlueRecord(){
return HomeItemBlueRecord;
}
public void setHomeItemBlueRecord(String Record){
if (Record == null)
this.HomeItemBlueRecord = "Unknown";
this.HomeItemBlueRecord = Record;
}
public String getHomeItemBlueHeight(){
return HomeItemBlueHeight;
}
public void setHomeItemBlueHeight(String Height){
if (Height == null)
this.HomeItemBlueHeight = "Unknown";
this.HomeItemBlueHeight = Height;
}
public String getHomeItemBlueWeight(){
return HomeItemBlueWeight;
}
public void setHomeItemBlueWeight(String Weight){
if (Weight == null)
this.HomeItemBlueWeight = "Unknown";
this.HomeItemBlueWeight = Weight;
}
public String getHomeItemBlueCity(){
return HomeItemBlueCity;
}
public void setHomeItemBlueCity(String City){
if (City == null)
this.HomeItemBlueCity = "Unknown";
this.HomeItemBlueCity= City;
}
public String getHomeItemBlueExp(){
return HomeItemBlueExp;
}
public void setHomeItemBlueExp(String Exp){
if (Exp == null)
this.HomeItemBlueExp = "Unknown";
this.HomeItemBlueExp = Exp;
}
@Override
public AssetManager getAssets() {
return null;
}
@Override
public Resources getResources() {
return null;
}
@Override
public PackageManager getPackageManager() {
return null;
}
@Override
public ContentResolver getContentResolver() {
return null;
}
@Override
public Looper getMainLooper() {
return null;
}
@Override
public Context getApplicationContext() {
return null;
}
@Override
public void setTheme(int i) {
}
@Override
public Resources.Theme getTheme() {
return null;
}
@Override
public ClassLoader getClassLoader() {
return null;
}
@Override
public String getPackageName() {
return null;
}
@Override
public ApplicationInfo getApplicationInfo() {
return null;
}
@Override
public String getPackageResourcePath() {
return null;
}
@Override
public String getPackageCodePath() {
return null;
}
@Override
public SharedPreferences getSharedPreferences(String s, int i) {
return null;
}
@Override
public FileInputStream openFileInput(String s) throws FileNotFoundException {
return null;
}
@Override
public FileOutputStream openFileOutput(String s, int i) throws FileNotFoundException {
return null;
}
@Override
public boolean deleteFile(String s) {
return false;
}
@Override
public File getFileStreamPath(String s) {
return null;
}
@Override
public File getFilesDir() {
return null;
}
@Override
public File getExternalFilesDir(String s) {
return null;
}
@Override
public File getObbDir() {
return null;
}
@Override
public File getCacheDir() {
return null;
}
@Override
public File getExternalCacheDir() {
return null;
}
@Override
public String[] fileList() {
return new String[0];
}
@Override
public File getDir(String s, int i) {
return null;
}
@Override
public SQLiteDatabase openOrCreateDatabase(String s, int i, SQLiteDatabase.CursorFactory cursorFactory) {
return null;
}
@Override
public SQLiteDatabase openOrCreateDatabase(String s, int i, SQLiteDatabase.CursorFactory cursorFactory, DatabaseErrorHandler databaseErrorHandler) {
return null;
}
@Override
public boolean deleteDatabase(String s) {
return false;
}
@Override
public File getDatabasePath(String s) {
return null;
}
@Override
public String[] databaseList() {
return new String[0];
}
@Override
public Drawable getWallpaper() {
return null;
}
@Override
public Drawable peekWallpaper() {
return null;
}
@Override
public int getWallpaperDesiredMinimumWidth() {
return 0;
}
@Override
public int getWallpaperDesiredMinimumHeight() {
return 0;
}
@Override
public void setWallpaper(Bitmap bitmap) throws IOException {
}
@Override
public void setWallpaper(InputStream inputStream) throws IOException {
}
@Override
public void clearWallpaper() throws IOException {
}
@Override
public void startActivity(Intent intent) {
}
@Override
public void startActivity(Intent intent, Bundle bundle) {
}
@Override
public void startActivities(Intent[] intents) {
}
@Override
public void startActivities(Intent[] intents, Bundle bundle) {
}
@Override
public void startIntentSender(IntentSender intentSender, Intent intent, int i, int i2, int i3) throws IntentSender.SendIntentException {
}
@Override
public void startIntentSender(IntentSender intentSender, Intent intent, int i, int i2, int i3, Bundle bundle) throws IntentSender.SendIntentException {
}
@Override
public void sendBroadcast(Intent intent) {
}
@Override
public void sendBroadcast(Intent intent, String s) {
}
@Override
public void sendOrderedBroadcast(Intent intent, String s) {
}
@Override
public void sendOrderedBroadcast(Intent intent, String s, BroadcastReceiver broadcastReceiver, Handler handler, int i, String s2, Bundle bundle) {
}
@Override
public void sendBroadcastAsUser(Intent intent, UserHandle userHandle) {
}
@Override
public void sendBroadcastAsUser(Intent intent, UserHandle userHandle, String s) {
}
@Override
public void sendOrderedBroadcastAsUser(Intent intent, UserHandle userHandle, String s, BroadcastReceiver broadcastReceiver, Handler handler, int i, String s2, Bundle bundle) {
}
@Override
public void sendStickyBroadcast(Intent intent) {
}
@Override
public void sendStickyOrderedBroadcast(Intent intent, BroadcastReceiver broadcastReceiver, Handler handler, int i, String s, Bundle bundle) {
}
@Override
public void removeStickyBroadcast(Intent intent) {
}
@Override
public void sendStickyBroadcastAsUser(Intent intent, UserHandle userHandle) {
}
@Override
public void sendStickyOrderedBroadcastAsUser(Intent intent, UserHandle userHandle, BroadcastReceiver broadcastReceiver, Handler handler, int i, String s, Bundle bundle) {
}
@Override
public void removeStickyBroadcastAsUser(Intent intent, UserHandle userHandle) {
}
@Override
public Intent registerReceiver(BroadcastReceiver broadcastReceiver, IntentFilter intentFilter) {
return null;
}
@Override
public Intent registerReceiver(BroadcastReceiver broadcastReceiver, IntentFilter intentFilter, String s, Handler handler) {
return null;
}
@Override
public void unregisterReceiver(BroadcastReceiver broadcastReceiver) {
}
@Override
public ComponentName startService(Intent intent) {
return null;
}
@Override
public boolean stopService(Intent intent) {
return false;
}
@Override
public boolean bindService(Intent intent, ServiceConnection serviceConnection, int i) {
return false;
}
@Override
public void unbindService(ServiceConnection serviceConnection) {
}
@Override
public boolean startInstrumentation(ComponentName componentName, String s, Bundle bundle) {
return false;
}
@Override
public Object getSystemService(String s) {
return null;
}
@Override
public int checkPermission(String s, int i, int i2) {
return 0;
}
@Override
public int checkCallingPermission(String s) {
return 0;
}
@Override
public int checkCallingOrSelfPermission(String s) {
return 0;
}
@Override
public void enforcePermission(String s, int i, int i2, String s2) {
}
@Override
public void enforceCallingPermission(String s, String s2) {
}
@Override
public void enforceCallingOrSelfPermission(String s, String s2) {
}
@Override
public void grantUriPermission(String s, Uri uri, int i) {
}
@Override
public void revokeUriPermission(Uri uri, int i) {
}
@Override
public int checkUriPermission(Uri uri, int i, int i2, int i3) {
return 0;
}
@Override
public int checkCallingUriPermission(Uri uri, int i) {
return 0;
}
@Override
public int checkCallingOrSelfUriPermission(Uri uri, int i) {
return 0;
}
@Override
public int checkUriPermission(Uri uri, String s, String s2, int i, int i2, int i3) {
return 0;
}
@Override
public void enforceUriPermission(Uri uri, int i, int i2, int i3, String s) {
}
@Override
public void enforceCallingUriPermission(Uri uri, int i, String s) {
}
@Override
public void enforceCallingOrSelfUriPermission(Uri uri, int i, String s) {
}
@Override
public void enforceUriPermission(Uri uri, String s, String s2, int i, int i2, int i3, String s3) {
}
@Override
public Context createPackageContext(String s, int i) throws PackageManager.NameNotFoundException {
return null;
}
@Override
public Context createConfigurationContext(Configuration configuration) {
return null;
}
@Override
public Context createDisplayContext(Display display) {
return null;
}
编译这个,在我的 LogCat 中,我收到这个错误:
10-06 18:42:59.465 785-785/com.codealchemist.clashmma E/AndroidRuntime﹕ FATAL EXCEPTION: main
java.lang.NullPointerException
at android.app.Dialog.<init>(Dialog.java:154)
at android.app.Dialog.<init>(Dialog.java:131)
at com.codealchemist.clashmma.DialogClass.<init>(DialogClass.java:83)
at com.codealchemist.clashmma.databaseFightCard$1.onItemClick(databaseFightCard.java:70)
at android.widget.AdapterView.performItemClick(AdapterView.java:298)
at android.widget.AbsListView.performItemClick(AbsListView.java:1100)
at android.widget.AbsListView$PerformClick.run(AbsListView.java:2749)
at android.widget.AbsListView$1.run(AbsListView.java:3423)
at android.os.Handler.handleCallback(Handler.java:725)
at android.os.Handler.dispatchMessage(Handler.java:92)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:5041)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
at dalvik.system.NativeStart.main(Native Method)
我也试过这个
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
HomeItem homeItem = (HomeItem) adapter.getItem(position);
final Dialog dialog = new Dialog(databaseFightCard.this, android.R.style.Theme_Black_NoTitleBar_Fullscreen);
WindowManager.LayoutParams lp = (dialog.getWindow().getAttributes());
lp.dimAmount = 0.5f;
dialog.getWindow().addFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);
Window window = dialog.getWindow();
window.setGravity(Gravity.CENTER);
dialog.setContentView(R.layout.custom_dialog);
Button play = (Button) findViewById(R.id.bPlay);
Button done = (Button) findViewById(R.id.bDone);
TextView rn = (TextView) findViewById(R.id.tvRName);
TextView ra = (TextView) findViewById(R.id.tvRAge);
TextView rr = (TextView) findViewById(R.id.tvRRecord);
TextView rh = (TextView) findViewById(R.id.tvRHeight);
TextView rw = (TextView) findViewById(R.id.tvRWeight);
TextView rc = (TextView) findViewById(R.id.tvRCity);
TextView re = (TextView) findViewById(R.id.tvRExp);
TextView bn = (TextView) findViewById(R.id.tvBName);
TextView ba = (TextView) findViewById(R.id.tvBAge);
TextView br = (TextView) findViewById(R.id.tvBRecord);
TextView bh = (TextView) findViewById(R.id.tvBHeight);
TextView bw = (TextView) findViewById(R.id.tvBWeight);
TextView bc = (TextView) findViewById(R.id.tvBCity);
TextView be = (TextView) findViewById(R.id.tvBExp);
rn.setText(homeItem.getHomeItemRedName()+"");
ra.setText(homeItem.getHomeItemRedAge()+"");
rr.setText(homeItem.getHomeItemRedRecord()+"");
rh.setText(homeItem.getHomeItemRedHeight()+"");
rw.setText(homeItem.getHomeItemRedWeight()+"");
rc.setText(homeItem.getHomeItemRedCity()+"");
re.setText(homeItem.getHomeItemRedExp()+"");
bn.setText(homeItem.getHomeItemBlueName()+"");
ba.setText(homeItem.getHomeItemBlueAge()+"");
br.setText(homeItem.getHomeItemBlueRecord()+"");
bh.setText(homeItem.getHomeItemBlueHeight()+"");
bw.setText(homeItem.getHomeItemBlueWeight()+"");
bc.setText(homeItem.getHomeItemBlueCity()+"");
be.setText(homeItem.getHomeItemBlueExp()+"");
play.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
dialog.dismiss();
}
});
done.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
dialog.dismiss();
}
});
dialog.show();
}
});
还在NullPointerException
继续
rn.setText(homeItem.getHomeItemRedName()+"");