findViewById从Activity返回null

时间:2013-06-06 05:02:03

标签: java android nullpointerexception findviewbyid

这个问题似乎已被问到很多,但我找不到答案。我欢迎任何链接到解决我的问题的副本。

我有两个视图,一个是自定义的,另一个是在xml中定义为

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal"
    android:id="@+id/button_bar_id" >

    <Button
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content"
        android:id="@+id/button_undo"></Button>
</LinearLayout>

在我的主要活动中,我获得了对两个视图的引用,并创建了一个FrameLayout来将它们放入:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    canvasView = new CustomCanvasView(this);

    buttonBar = (LinearLayout) findViewById(R.id.button_bar_id);
    frameLayout = new FrameLayout(this);
    frameLayout.addView(canvasView);
    frameLayout.addView(buttonBar);
    setContentView(frameLayout);
}

就是这样!如果我只使用FrameLayout和我的自定义视图,一切都有效,但如果我尝试添加预定义视图buttonBar,那么我会从初始化它的行中获得nullPointerException。

我查看了this问题解决方案似乎首先调用setContentView(frameLayout),但这对我不起作用,实际上是工作案例(我不添加的地方) buttonBar)自相矛盾地崩溃。

关于这里发生了什么的任何想法?

谢谢!

更新

所以,有些人建议我尝试修改我的xml文件,以便它包含对我的自定义视图的引用:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal"
    android:id="@+id/button_bar_id" >

<com.collin.customcanvasengine.CustomCanvasView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/customCanvasView">
</com.collin.customcanvasengine.CustomCanvasView>
<Button
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content"
    android:id="@+id/button_undo"></Button>

</LinearLayout>

以及修改我的主要活动,以便它使用此xml文件作为其内容视图:

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

    canvasView = (CustomCanvasView) findViewById(R.id.customCanvasView);
    canvasView.setDimensions(10,10);
    canvasView.requestFocus();
}

现在我得到的错误是我的自定义视图无法膨胀:

android.view.InflateException:二进制XML文件行#8:错误膨胀类com.collin.customcanvasengine.CustomCanvasView

有什么想法吗?感谢。

更新

这是logcat输出:

06-06 01:55:30.010: E/Trace(19901): error opening trace file: Permission denied (13)
06-06 01:55:30.010: D/ActivityThread(19901): setTargetHeapUtilization:0.25
06-06 01:55:30.010: D/ActivityThread(19901): setTargetHeapIdealFree:8388608
06-06 01:55:30.010: D/ActivityThread(19901): setTargetHeapConcurrentStart:2097152
06-06 01:55:30.050: D/AndroidRuntime(19901): Shutting down VM
06-06 01:55:30.050: W/dalvikvm(19901): threadid=1: thread exiting with uncaught exception     (group=0x410b5438)
06-06 01:55:30.060: I/Process(19901): Sending signal. PID: 19901 SIG: 9
06-06 01:55:30.060: E/AndroidRuntime(19901): FATAL EXCEPTION: main
06-06 01:55:30.060: E/AndroidRuntime(19901): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.collin.customcanvasengine/com.collin.customcanvasengine.MainActivity}: android.view.InflateException: Binary XML file line #8: Error inflating class com.collin.customcanvasengine.CustomCanvasView
06-06 01:55:30.060: E/AndroidRuntime(19901):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2117)
06-06 01:55:30.060: E/AndroidRuntime(19901):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2155)
06-06 01:55:30.060: E/AndroidRuntime(19901):    at android.app.ActivityThread.access$700(ActivityThread.java:139)
06-06 01:55:30.060: E/AndroidRuntime(19901):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1248)
06-06 01:55:30.060: E/AndroidRuntime(19901):    at android.os.Handler.dispatchMessage(Handler.java:99)
06-06 01:55:30.060: E/AndroidRuntime(19901):    at android.os.Looper.loop(Looper.java:137)
06-06 01:55:30.060: E/AndroidRuntime(19901):    at android.app.ActivityThread.main(ActivityThread.java:5062)
06-06 01:55:30.060: E/AndroidRuntime(19901):    at java.lang.reflect.Method.invokeNative(Native Method)
06-06 01:55:30.060: E/AndroidRuntime(19901):    at java.lang.reflect.Method.invoke(Method.java:511)
06-06 01:55:30.060: E/AndroidRuntime(19901):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:795)
06-06 01:55:30.060: E/AndroidRuntime(19901):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:558)
06-06 01:55:30.060: E/AndroidRuntime(19901):    at dalvik.system.NativeStart.main(Native Method)
06-06 01:55:30.060: E/AndroidRuntime(19901): Caused by: android.view.InflateException: Binary XML file line #8: Error inflating class com.collin.customcanvasengine.CustomCanvasView
06-06 01:55:30.060: E/AndroidRuntime(19901):    at android.view.LayoutInflater.createView(LayoutInflater.java:596)
06-06 01:55:30.060: E/AndroidRuntime(19901):    at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:687)
06-06 01:55:30.060: E/AndroidRuntime(19901):    at android.view.LayoutInflater.rInflate(LayoutInflater.java:746)
06-06 01:55:30.060: E/AndroidRuntime(19901):    at android.view.LayoutInflater.inflate(LayoutInflater.java:489)
06-06 01:55:30.060: E/AndroidRuntime(19901):    at android.view.LayoutInflater.inflate(LayoutInflater.java:396)
06-06 01:55:30.060: E/AndroidRuntime(19901):    at android.view.LayoutInflater.inflate(LayoutInflater.java:352)
06-06 01:55:30.060: E/AndroidRuntime(19901):    at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:256)
06-06 01:55:30.060: E/AndroidRuntime(19901):    at android.app.Activity.setContentView(Activity.java:1893)
06-06 01:55:30.060: E/AndroidRuntime(19901):    at com.collin.customcanvasengine.MainActivity.onCreate(MainActivity.java:23)
06-06 01:55:30.060: E/AndroidRuntime(19901):    at android.app.Activity.performCreate(Activity.java:5058)
06-06 01:55:30.060: E/AndroidRuntime(19901):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1079)
06-06 01:55:30.060: E/AndroidRuntime(19901):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2081)
06-06 01:55:30.060: E/AndroidRuntime(19901):    ... 11 more
06-06 01:55:30.060: E/AndroidRuntime(19901): Caused by: java.lang.NoSuchMethodException: <init> [class android.content.Context, interface android.util.AttributeSet]
06-06 01:55:30.060: E/AndroidRuntime(19901):    at java.lang.Class.getConstructorOrMethod(Class.java:460)
06-06 01:55:30.060: E/AndroidRuntime(19901):    at java.lang.Class.getConstructor(Class.java:431)
06-06 01:55:30.060: E/AndroidRuntime(19901):    at android.view.LayoutInflater.createView(LayoutInflater.java:561)
06-06 01:55:30.060: E/AndroidRuntime(19901):    ... 22 more

此外,CustomCanvasView的构造函数的格式为

public CustomCanvasView(Context context)

最终更新

所以Christopher Perry的回答对我来说最有效。正如所建议的那样,有必要使用public CustomCanvasView(Context context, AttributeSet attr)形式的构造函数,更重要的是,在此构造函数中将属性集传递给super的调用中的父元素。目前我还没有使用构造函数中的属性集,但我可能决定将来实现自定义属性。无论哪种方式都可以。

再次感谢大家,感谢Christopher Perry的帮助。

5 个答案:

答案 0 :(得分:2)

尝试使用LayoutInflater.inflate

canvasView = new CustomCanvasView(this);
LayoutInflater inflater= (LayoutInflater)this.getSystemService(
                                               Context.LAYOUT_INFLATER_SERVICE);
View buttonBar=inflater.inflate(R.layout.your_layout_name,null);
frameLayout = new FrameLayout(this);
frameLayout.addView(canvasView);
frameLayout.addView(buttonBar);
setContentView(frameLayout);

答案 1 :(得分:1)

在将Activity放入Activity的视图层次结构之前,您要求Activity查找您的视图。您需要使用布局资源ID调用setContentView,然后才能在Activity中找到视图。

你应该做的是将你的xml更改为这样的东西(例如你的xml文件在本例中称为some_layout.xml,因为我不知道你的文件名):

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <Button
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content"
        android:id="@+id/button_undo" />

    <com.mypackage.CustomCanvasView
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" />

</LinearLayout>

然后你的onCreate就是:

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

由于您要在Activity视图层次结构中放置自定义View,因此只需在layout xml文件中声明它即可。没有理由用Android已经为你做的事情弄乱你的活动代码。

修改

确保在CustomCanvasView类中定义一个带有AttributeSet的constructor,否则您将获得InflateException。像这样:

public CustomCanvasView(Context context, AttributeSet attributes) {
  super(context, attributes);
  // code goes here
}

答案 2 :(得分:1)

您需要先将布局内容设置为活动

  setContentView(R.layout.mylayout);

然后你可以findviewbyid(初始化视图);

   buttonBar = (LinearLayout) findViewById(R.id.button_bar_id); 

您可以找到设置为活动的当前视图层次结构的ViewById。如果未初始化视图,则会获得NullPointerException

答案 3 :(得分:0)

使用buttonBar = (LinearLayout)canvasView. findViewById(R.id.button_bar_id);

答案 4 :(得分:0)

您必须先扩充布局,然后将其添加到frameLayout