简体   繁体   English

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

[英]Android findViewById() returning null for custom view

So I've checked all of the other threads on this topic repeatedly but none have yielded the solution. 因此,我反复检查了该主题上的所有其他线程,但没有一个提出解决方案。 Here's the code: 这是代码:

The XML 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>

The Activity 活动

@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 DragScrollBar.java

public class DragScrollBar extends MaterialScrollBar<DragScrollBar>{

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

MaterialScrollBar.java MaterialScrollBar.java

abstract class MaterialScrollBar<T> extends RelativeLayout {

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

//Stuff

}

It draws correctly on the preview but returns a nullPointerException when dragScrollBar is used and 'fail' is printed to the console. 它可以在预览上正确绘制,但是在使用dragScrollBar并将“ fail”打印到控制台时返回nullPointerException

I've omitted the other normal constructors but when included they don't make a difference. 我省略了其他普通的构造函数,但是当包含它们时,它们没有任何区别。

EDIT: Here's the stack trace 编辑:这是堆栈跟踪

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)

作为早期方案的一部分,我已经在initalise中运行了setID(),并且没有考虑在重构期间将其删除。

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

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