【问题标题】:Android findViewById() returning null for custom viewAndroid findViewById() 为自定义视图返回 null
【发布时间】:2016-02-15 19:11:24
【问题描述】:

因此,我反复检查了有关此主题的所有其他线程,但没有一个产生解决方案。代码如下:

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

}

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

我已经省略了其他普通的构造函数,但是当包括它们时它们并没有什么区别。

编辑:这是堆栈跟踪

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)

 

【问题讨论】:

  • xml activity_main.xml?
  • 发布堆栈跟踪,以及更早的错误(如果有)
  • @Blackbelt 是的

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


【解决方案1】:

我曾在 initalise 内部运行 setID() 作为早期方案的一部分,但没想到在重构期间将其删除。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多