Android findViewById()为自定义视图返回null

时间:2016-02-15 19:11:24

标签: java android nullpointerexception android-custom-view custom-view

所以我已经反复检查了这个主题的所有其他线程,但没有一个已经产生了解决方案。这是代码:

XML

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"  
    android:layout_width="match_parent"
    android:layout_height="match_parent" 
    tools:context=".MainActivity">

    <android.support.v7.widget.RecyclerView
        android:id="@+id/recyclerView"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

    <com.turingtechnologies.materialscrollbar.DragScrollBar
        android:layout_width="wrap_content"
        android:id="@+id/dragScrollBar"
        android:layout_height="match_parent"/>

</RelativeLayout>

活动

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

    //Stuff

    recyclerView = (RecylerView)findViewById(R.id.recyclerView); //I've verified that this works properly.
    DragScrollBar dragScrollBar = (DragScrollBar)findViewById(R.id.dragScrollBar);
    if (dragScrollBar == null) { Log.d("d", "fail"); }
    dragScrollBar.bindRecyclerView(recyclerView);
}

DragScrollBar.java

public class DragScrollBar extends MaterialScrollBar<DragScrollBar>{

    public DragScrollBar(Context context, AttributeSet attrs){
        super(context, attrs);
    }
}

MaterialScrollBar.java

abstract class MaterialScrollBar<T> extends RelativeLayout {

    MaterialScrollBar(Context context, AttributeSet attrs){
        super(context, attrs);
        initialise(context);
    }

//Stuff

}

它在预览时正确绘制,但在使用nullPointerException时返回dragScrollBar,并且“失败”将打印到控制台。

我省略了其他正常的构造函数,但是当它们被包含时它们没有什么区别。

编辑:这是堆栈跟踪

fail
Shutting down VM
FATAL EXCEPTION: main
Process: com.turingtechnologies.materialscrollbardemo, PID: 31890                                                                                             java.lang.RuntimeException: Unable to start activity ComponentInfo{com.turingtechnologies.materialscrollbardemo/com.turingtechnologies.materialscrollbardemo.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.Object com.turingtechnologies.materialscrollbar.DragScrollBar.bindRecyclerView(android.support.v7.widget.RecyclerView)' on a null object reference at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2416)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476)
at android.app.ActivityThread.-wrap11(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5417)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)

Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.Object com.turingtechnologies.materialscrollbar.DragScrollBar.bindRecyclerView(android.support.v7.widget.RecyclerView)' on a null object reference
at com.turingtechnologies.materialscrollbardemo.MainActivity.onCreate(MainActivity.java:26)
at android.app.Activity.performCreate(Activity.java:6251)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1107)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2369)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476) 
at android.app.ActivityThread.-wrap11(ActivityThread.java) 
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344) 
at android.os.Handler.dispatchMessage(Handler.java:102) 
at android.os.Looper.loop(Looper.java:148) 
at android.app.ActivityThread.main(ActivityThread.java:5417) 
at java.lang.reflect.Method.invoke(Native Method) 
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)

1 个答案:

答案 0 :(得分:0)

我在initalise中运行了setID()作为早期方案的一部分,并且没有想过在重构期间删除它。