简体   繁体   English

NullPointerException:SetOnClickListener(this)-安卓

[英]NullPointerException: SetOnClickListener(this) - Android

When running my application in Android Studio I get a NullPointerException at line 30 which is this: 在Android Studio中运行我的应用程序时,在第30行得到一个NullPointerException ,它是这样的:

llTheory.setOnClickListener(this);

which has always worked for me.. I am implementing View.OnClickListener and have the override method here: 这一直为我工作。.我正在实现View.OnClickListener ,并在此处具有重写方法:

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

My LinearLayout is declared before the setOnClickListener here: 我的LinearLayout在setOnClickListener之前声明:

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

so what is going wrong? 那么怎么了? Here is my LogCat output: 这是我的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)

Fragments work a little bit differently. 片段的工作方式略有不同。

You have to use the concept of rootview when dealing with them. 处理它们时,必须使用rootview的概念。 Otherwise you will find yourself always coming across the dreaded "NULL POINTER" exception. 否则,您将发现自己总是遇到可怕的“ NULL POINTER”异常。

Here is how to go about it. 这是怎么做的。

Define the rootview when you inflate each view for each fragment, then call the onClick functionality. 在为每个片段添加每个视图时定义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.
        }
    });

Another alternative is using getView which always returns the rootview. 另一种选择是使用getView ,它总是返回rootview。 Try this instead 试试这个

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

This link explains a similar problem. 链接说明了类似的问题。

The former way however is the correct way as you isolate your fragments from each other and understand the view hierarchy correctly. 但是,当您将片段彼此隔离并正确理解视图层次结构时,前一种方法是正确的方法。 I would read this . 我会读这个 Trust me the time investment reading that stuff is worthwhile then the hours you'll spend otherwise with half-wit hack job ideas to make stuff work. 相信我,花时间阅读那些东西是值得的,然后用半聪明的黑客工作思路使这些东西起作用,您将花费数小时。

findViewById returns a null value when the requested view cannot be found. 当找不到请求的视图时, findViewById 返回一个空值

  • Check in your corresponding xml that a view is indeed declared with the id @id/llTheory 在相应的xml中检查是否确实使用ID @id/llTheory声明了一个视图
  • A standard Java best practice would be checking a casted item before using it. Java的最佳标准做法是在使用之前先检查已投射的项目。

Do you have 你有

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.layout_with_R_id_llTheory);
...

in your activity? 在你的活动中? Your activity (or fragment hosted by activity) must have layout with R.id.llTheory . 您的活动(或由活动托管的片段)必须具有R.id.llTheory布局。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM