我有一个带有imageButton的listView。当我点击imageButton时,我试图在主类中调用一个方法来显示一个toast消息。但是,当我点击按钮时,我收到一条错误消息:
04-17 18:25:16.293: E/AndroidRuntime(9236): FATAL EXCEPTION: main
04-17 18:25:16.293: E/AndroidRuntime(9236): java.lang.NullPointerException
04-17 18:25:16.293: E/AndroidRuntime(9236): at android.content.ContextWrapper.getResources(ContextWrapper.java:81)
04-17 18:25:16.293: E/AndroidRuntime(9236): at android.widget.Toast.<init>(Toast.java:94)
04-17 18:25:16.293: E/AndroidRuntime(9236): at android.widget.Toast.makeText(Toast.java:240)
04-17 18:25:16.293: E/AndroidRuntime(9236): at com.flamesavor.reme.ReMe.setAlarm(ReMe.java:69)
04-17 18:25:16.293: E/AndroidRuntime(9236): at com.flamesavor.reme.resources.ListViewAdapter.setAlarm(ListViewAdapter.java:62)
04-17 18:25:16.293: E/AndroidRuntime(9236): at com.flamesavor.reme.resources.ListViewAdapter$1.onClick(ListViewAdapter.java:49)
04-17 18:25:16.293: E/AndroidRuntime(9236): at android.view.View.performClick(View.java:4222)
04-17 18:25:16.293: E/AndroidRuntime(9236): at android.view.View$PerformClick.run(View.java:17343)
04-17 18:25:16.293: E/AndroidRuntime(9236): at android.os.Handler.handleCallback(Handler.java:615)
04-17 18:25:16.293: E/AndroidRuntime(9236): at android.os.Handler.dispatchMessage(Handler.java:92)
04-17 18:25:16.293: E/AndroidRuntime(9236): at android.os.Looper.loop(Looper.java:137)
04-17 18:25:16.293: E/AndroidRuntime(9236): at android.app.ActivityThread.main(ActivityThread.java:4895)
04-17 18:25:16.293: E/AndroidRuntime(9236): at java.lang.reflect.Method.invokeNative(Native Method)
04-17 18:25:16.293: E/AndroidRuntime(9236): at java.lang.reflect.Method.invoke(Method.java:511)
04-17 18:25:16.293: E/AndroidRuntime(9236): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:994)
04-17 18:25:16.293: E/AndroidRuntime(9236): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:761)
04-17 18:25:16.293: E/AndroidRuntime(9236): at dalvik.system.NativeStart.main(Native Method)
下面是我创建了onClickListener的Arrayadapter。侦听器调用适配器中的方法,然后调用Main类中的setAlarm方法(称为“ReMe”)。
public class ListViewAdapter extends android.widget.ArrayAdapter<String> {
private Context context;
private List<String> reminders;
private View reminder;
public ListViewAdapter(Context context, int resource, List<String> objects) {
super(context, resource, objects);
this.context = context;
this.reminders = objects;
}
//Change textcolor to black. Everything else remains same
@Override
public View getView(int position, View convertView, ViewGroup parent){
//Set view to convertView if the there are already rows created
reminder = convertView;
if(reminder == null){
LayoutInflater inflator = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
reminder = inflator.inflate(R.layout.reminder_list_layout, parent, false);
}
//Set imageButton actionListener
ImageButton alarmButton = (ImageButton) reminder.findViewById(R.id.reminder_alarmnavigation);
alarmButton.setTag(reminder);
alarmButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
setAlarm((View) v.getTag());
}
});
//Initialize and assign reminder title
TextView reminder_title = (TextView) reminder.findViewById(R.id.reminder_title);
reminder_title.setText(reminders.get(position));
return reminder;
}
public void setAlarm(View view){
ReMe reminder = new ReMe();
reminder.setAlarm(view);
}
}
以下是ReMe课程中的方法:
public void setAlarm(View view){
Intent intent = new Intent(this, SetReminder.class);
TextView reminder_title = (TextView) view.findViewById(R.id.reminder_title);
Toast.makeText(ReMe.this, "testing", Toast.LENGTH_LONG).show();
//intent.putExtra("com.flamesavor.reme.reminderTitle", reminder_title.getText());
//startActivity(intent);
}
不知道我在哪里弄错了。任何帮助将不胜感激。
下面是修改后的代码,尝试在创建toast时触发活动:
public void setAlarm(View view){
Intent intent = new Intent(view.getContext(), SetReminder.class);
TextView reminder_title = (TextView) view.findViewById(R.id.reminder_title);
intent.putExtra("com.flamesavor.reme.reminderTitle", reminder_title.getText());
startActivity(intent);
}
以下是我得到的错误日志。不知道为什么会这样:
04-18 22:53:06.381: E/AndroidRuntime(8706): FATAL EXCEPTION: main
04-18 22:53:06.381: E/AndroidRuntime(8706): java.lang.NullPointerException
04-18 22:53:06.381: E/AndroidRuntime(8706): at android.app.Activity.startActivityForResult(Activity.java:3423)
04-18 22:53:06.381: E/AndroidRuntime(8706): at android.app.Activity.startActivityForResult(Activity.java:3384)
04-18 22:53:06.381: E/AndroidRuntime(8706): at android.app.Activity.startActivity(Activity.java:3594)
04-18 22:53:06.381: E/AndroidRuntime(8706): at android.app.Activity.startActivity(Activity.java:3562)
04-18 22:53:06.381: E/AndroidRuntime(8706): at com.flamesavor.reme.ReMe.setAlarm(ReMe.java:67)
04-18 22:53:06.381: E/AndroidRuntime(8706): at com.flamesavor.reme.resources.ListViewAdapter.setAlarm(ListViewAdapter.java:62)
04-18 22:53:06.381: E/AndroidRuntime(8706): at com.flamesavor.reme.resources.ListViewAdapter$1.onClick(ListViewAdapter.java:49)
04-18 22:53:06.381: E/AndroidRuntime(8706): at android.view.View.performClick(View.java:4222)
04-18 22:53:06.381: E/AndroidRuntime(8706): at android.view.View$PerformClick.run(View.java:17343)
04-18 22:53:06.381: E/AndroidRuntime(8706): at android.os.Handler.handleCallback(Handler.java:615)
04-18 22:53:06.381: E/AndroidRuntime(8706): at android.os.Handler.dispatchMessage(Handler.java:92)
04-18 22:53:06.381: E/AndroidRuntime(8706): at android.os.Looper.loop(Looper.java:137)
04-18 22:53:06.381: E/AndroidRuntime(8706): at android.app.ActivityThread.main(ActivityThread.java:4895)
04-18 22:53:06.381: E/AndroidRuntime(8706): at java.lang.reflect.Method.invokeNative(Native Method)
04-18 22:53:06.381: E/AndroidRuntime(8706): at java.lang.reflect.Method.invoke(Method.java:511)
04-18 22:53:06.381: E/AndroidRuntime(8706): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:994)
04-18 22:53:06.381: E/AndroidRuntime(8706): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:761)
04-18 22:53:06.381: E/AndroidRuntime(8706): at dalvik.system.NativeStart.main(Native Method)
=============================================== ====================================== 决议(编辑): 设置FLAG_ACTIVITY_NEW_TASK并在视图的上下文中调用新活动。
TextView reminder_title = (TextView) view.findViewById(R.id.reminder_title);
Intent intent = new Intent(view.getContext(), com.flamesavor.reme.SetReminder.class);
intent.putExtra("com.flamesavor.reme.reminderTitle", reminder_title.getText());
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
view.getContext().startActivity(intent);
答案 0 :(得分:0)
java.lang.NullPointerException 04-17 18:25:16.293: E / AndroidRuntime(9236):at android.content.ContextWrapper.getResources(ContextWrapper.java:81) 04-17 18:25:16.293:E / AndroidRuntime(9236):at android.widget.Toast。(Toast.java:94)
ReMe.java
不是Activity类。 ReMe.this
并未引用有效的上下文。
所以改为
Toast.makeText(view.getContext(), "testing", Toast.LENGTH_LONG).show();
http://developer.android.com/reference/android/view/View.html#getContext()
public final Context getContext ()
Added in API level 1
Returns the context the view is running in, through which it can access the current theme, resources, etc.
Returns
The view's Context.
同时更改此
Intent intent = new Intent(this, SetReminder.class);
到
Intent intent = new Intent(view.getContext(), SetReminder.class);
答案 1 :(得分:0)
你使用&#34;这个&#34;作为Intent构造函数的第一个参数,它表明当前类是你的上下文,所以使用相同的&#34;这个&#34;作为Toast.makeText的第一个参数
答案 2 :(得分:0)
如果ReMe
类不属于上下文,那么就会出现问题,因为您必须指定上下文或活动才能在应用中显示Toast