0

我有一个带有 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。侦听器调用适配器中的方法,然后调用主类中的 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);
4

3 回答 3

0

java.lang.NullPointerException 04-17 18:25:16.293: E/AndroidRuntime(9236): 在 android.content.ContextWrapper.getResources(ContextWrapper.java:81) 04-17 18:25:16.293: E/AndroidRuntime(9236 ): 在 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);
于 2014-04-17T13:00:51.190 回答
0

如果ReMe类不属于上下文则有问题,因为您必须指定上下文或活动才能Toast在应用程序中显示

于 2014-04-17T13:21:30.690 回答
0

你使用“this”作为Intent构造函数的第一个参数,表示当前类是你的上下文,所以使用相同的“this”作为Toa​​st.makeText的第一个参数

于 2014-04-17T13:13:09.337 回答