NullPointerException:SetOnClickListener(this)-安卓

蒂莫

在Android Studio中运行应用程序时,我会NullPointerException在第30行看到以下内容:

llTheory.setOnClickListener(this);

这一直为我工作。.我正在实现View.OnClickListener,并在此处具有重写方法:

@Override
public void onClick(View v) {
    ....
}

我的LinearLayout在setOnClickListener这里之前声明

    LinearLayout llTheory = (LinearLayout) findViewById(R.id.llTheory);

那么怎么了?这是我的LogCat输出:

10-20 21:09:24.045  14625-14625/com.timmo.applauncher E/AndroidRuntime﹕ FATAL EXCEPTION: main
    Process: com.timmo.applauncher, PID: 14625
    java.lang.RuntimeException: Unable to start activity ComponentInfo{com.timmo.applauncher/com.timmo.timmoapplauncher.Main}: java.lang.NullPointerException
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2224)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2283)
            at android.app.ActivityThread.access$800(ActivityThread.java:144)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1205)
            at android.os.Handler.dispatchMessage(Handler.java:102)
            at android.os.Looper.loop(Looper.java:136)
            at android.app.ActivityThread.main(ActivityThread.java:5153)
            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:796)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:612)
            at de.robv.android.xposed.XposedBridge.main(XposedBridge.java:132)
            at dalvik.system.NativeStart.main(Native Method)
     Caused by: java.lang.NullPointerException
            at com.timmo.timmoapplauncher.Main.Init(Main.java:30)
            at com.timmo.timmoapplauncher.Main.onCreate(Main.java:78)
            at android.app.Activity.performCreate(Activity.java:5312)
            at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2181)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2283)
            at android.app.ActivityThread.access$800(ActivityThread.java:144)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1205)
            at android.os.Handler.dispatchMessage(Handler.java:102)
            at android.os.Looper.loop(Looper.java:136)
            at android.app.ActivityThread.main(ActivityThread.java:5153)
            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:796)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:612)
            at de.robv.android.xposed.XposedBridge.main(XposedBridge.java:132)
            at dalvik.system.NativeStart.main(Native Method)
海鹰Rda最佳

片段的工作方式略有不同。

处理它们时,必须使用rootview的概念。否则,您将发现自己总是遇到可怕的“ NULL POINTER”异常。

这是怎么做的。

在为每个片段充气每个视图时定义rootview,然后调用onClick功能。

public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){
    View rootview = inflater.inflate(R.layout.xml_you_want_to_inflate, container,false);        
    // Now call rootview and the onclick fuction, presumably for a list or button
    rootview.findViewById(R.id.your_button/list_id_here).setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
        // Do stuff.
        }
    });

另一种选择是使用getView,它总是返回rootview。试试这个

LinearLayout llTheory = (LinearLayout)  getView().findViewById(R.id.llTheory);

链接说明了类似的问题。

但是,当您将片段彼此隔离并正确理解视图层次结构时,前一种方法是正确的方法。我会读这个相信我,花时间阅读那些东西是值得的,然后用半聪明的黑客工作思路使这些东西起作用,您将花费数小时。

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章