java.lang.NullPointerException on extends Activity

时间:2014-08-19 14:33:41

标签: java android

我有2节课。第一类是我的自定义吐司类,第二类是活动,其中onCreate void和按钮点击事件。

我认为问题是关于扩展活动的人

THX

第一类:

    class MakeToast extends Activity {

    public void makeToast(String string, Boolean type){

        LayoutInflater inflater = getLayoutInflater();
        int TypeLayout;

        if(type == false){
            TypeLayout = R.layout.toast_layout_error;
        }
        else{
            TypeLayout = R.layout.toast_layout_success;
        }

        View layout = inflater.inflate(TypeLayout,(ViewGroup) findViewById(R.id.toast_layout_root));

        TextView text = (TextView) layout.findViewById(R.id.text);
        text.setText(string);

        Toast toast = new Toast(getApplicationContext());
        toast.setGravity(Gravity.BOTTOM|Gravity.FILL_HORIZONTAL, 0, 0);
        toast.setDuration(Toast.LENGTH_LONG);
        toast.setView(layout);
        toast.show();
    }

}

第二课

当我点击按钮apk崩溃时

public class Gobygo extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    ImageButton button_search = (ImageButton)findViewById(R.id.button_search);

    button_search.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            MakeToast toast_object = new MakeToast();
            toast_object.makeToast("Alert  !!!!", false);

        }
    });
}

}

这里是完整的日志

08-19 10:17:30.333  26797-26797/com.example.tomastrusik.gobygo E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: com.example.tomastrusik.gobygo, PID: 26797
java.lang.NullPointerException
        at android.app.Activity.getLayoutInflater(Activity.java:3338)
        at com.example.tomastrusik.gobygo.Gobygo$MakeToast.makeToast(Gobygo.java:87)
        at com.example.tomastrusik.gobygo.Gobygo$2.onClick(Gobygo.java:72)
        at android.view.View.performClick(View.java:4438)
        at android.view.View$PerformClick.run(View.java:18422)
        at android.os.Handler.handleCallback(Handler.java:733)
        at android.os.Handler.dispatchMessage(Handler.java:95)
        at android.os.Looper.loop(Looper.java:136)
        at android.app.ActivityThread.main(ActivityThread.java:5017)
        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:779)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
        at dalvik.system.NativeStart.main(Native Method)`enter code here`

1 个答案:

答案 0 :(得分:1)

您无需延长Activity。实际上,您不需要扩展任何内容。您实际上是在创建一个静态实用程序函数来显示专门的Toast

public class ToastUtils {

    private ToastUtils() { /*nobody needs to instantiate this ever*/ }

    public static void makeToast(Context context, boolean success, String text) {
        LayoutInflater inflater = LayoutInflater.from(context);
        int layoutRes = success ? R.layout.toast_layout_success : R.layout.toast_layout_error;

        View layout = inflater.inflate(layoutRes, null, false);

        TextView text = (TextView) layout.findViewById(R.id.text);
        text.setText(text);

        Toast toast = new Toast(context);
        toast.setGravity(Gravity.BOTTOM|Gravity.FILL_HORIZONTAL, 0, 0);
        toast.setDuration(Toast.LENGTH_LONG);
        toast.setView(layout);
        toast.show();
    }
}

通过调用ToastUtils.makeToast(...)调用此方法,并从活动中为其提供LayoutInflater。