尝试访问父布局时出现空指针异常

发布于 2024-11-17 23:11:04 字数 4015 浏览 3 评论 0原文

我有一个包含三个按钮的相对布局,我试图动态更改相对布局的背景。这是 xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
            android:orientation="horizontal"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:background="@drawable/mbi2"
            android:id="@+id/rlMain"
            android:layout_centerHorizontal="true">
    <Button android:text="Compose" 
            android:id="@+id/btnCompose" 
            android:layout_height="wrap_content"  
            android:layout_width="wrap_content"/>
    <Button android:text="Messages" 
            android:id="@+id/btnMessages" 
            android:layout_height="wrap_content" 
            android:layout_width="wrap_content" 
            android:layout_toRightOf="@id/btnCompose"/>
    <Button android:text="More &gt;&gt;" 
            android:id="@+id/btnMore" 
            android:layout_height="wrap_content" 
            android:layout_width="wrap_content" 
            android:layout_toRightOf="@id/btnMessages"/>                    
            </RelativeLayout>

这是给我一个错误的 java 代码

            RelativeLayout llMain=null;
    try {
        llMain = (RelativeLayout) findViewById(R.id.rlMain);
        Resources res = getResources();
        Drawable drawMbi = res.getDrawable(drawBgId);
        llMain.setBackgroundDrawable(drawMbi);          
    } catch (Exception e) {
        e.printStackTrace();
    }

错误是

06-29 23:56:41.099: WARN/System.err(8487): java.lang.NullPointerException
06-29 23:56:41.099: WARN/System.err(8487):     at com.me2youmob.cwrap.ChickenWrapActivity.loadMBIIntoView(ChickenWrapActivity.java:65)
06-29 23:56:41.099: WARN/System.err(8487):     at com.me2youmob.cwrap.ChickenWrapActivity.onCreate(ChickenWrapActivity.java:23)
06-29 23:56:41.099: WARN/System.err(8487):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1066)
06-29 23:56:41.099: WARN/System.err(8487):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2797)
06-29 23:56:41.108: WARN/System.err(8487):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2854)
06-29 23:56:41.108: WARN/System.err(8487):     at android.app.ActivityThread.access$2300(ActivityThread.java:136)
06-29 23:56:41.118: WARN/System.err(8487):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2179)
06-29 23:56:41.118: WARN/System.err(8487):     at android.os.Handler.dispatchMessage(Handler.java:99)
06-29 23:56:41.118: WARN/System.err(8487):     at android.os.Looper.loop(Looper.java:143)
06-29 23:56:41.118: WARN/System.err(8487):     at android.app.ActivityThread.main(ActivityThread.java:5068)
06-29 23:56:41.118: WARN/System.err(8487):     at java.lang.reflect.Method.invokeNative(Native Method)
06-29 23:56:41.118: WARN/System.err(8487):     at java.lang.reflect.Method.invoke(Method.java:521)
06-29 23:56:41.118: WARN/System.err(8487):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:858)
06-29 23:56:41.118: WARN/System.err(8487):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
06-29 23:56:41.118: WARN/System.err(8487):     at dalvik.system.NativeStart.main(Native Method)

这个问题中的另一件事是,早些时候我在线性布局中放置了按钮,然后在相对布局中。我认为它无法达到相对布局并删除线性布局部分。它仍然给我同样的错误。

知道这是怎么回事吗?

新更新:

    @Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    RelativeLayout rlM = (RelativeLayout) findViewById(R.id.rlMain);
    if (rlM == null)
    {
        Log.d("NULL CHECK","Layout is null");
    }
    else
    {
        Log.d("NULL CHECK","Layout is not null");
    }
    //spPreferences = getSharedPreferences(PREFS_NAME, 0);
    //loadMBIIntoView();
    //handleButtonClicks();
}

I have a relative layout with three buttons in it and I am trying to change the background of the relative layout dynamically. Here's the xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
            android:orientation="horizontal"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:background="@drawable/mbi2"
            android:id="@+id/rlMain"
            android:layout_centerHorizontal="true">
    <Button android:text="Compose" 
            android:id="@+id/btnCompose" 
            android:layout_height="wrap_content"  
            android:layout_width="wrap_content"/>
    <Button android:text="Messages" 
            android:id="@+id/btnMessages" 
            android:layout_height="wrap_content" 
            android:layout_width="wrap_content" 
            android:layout_toRightOf="@id/btnCompose"/>
    <Button android:text="More >>" 
            android:id="@+id/btnMore" 
            android:layout_height="wrap_content" 
            android:layout_width="wrap_content" 
            android:layout_toRightOf="@id/btnMessages"/>                    
            </RelativeLayout>

Here's the java code which is giving me an error

            RelativeLayout llMain=null;
    try {
        llMain = (RelativeLayout) findViewById(R.id.rlMain);
        Resources res = getResources();
        Drawable drawMbi = res.getDrawable(drawBgId);
        llMain.setBackgroundDrawable(drawMbi);          
    } catch (Exception e) {
        e.printStackTrace();
    }

The error is

06-29 23:56:41.099: WARN/System.err(8487): java.lang.NullPointerException
06-29 23:56:41.099: WARN/System.err(8487):     at com.me2youmob.cwrap.ChickenWrapActivity.loadMBIIntoView(ChickenWrapActivity.java:65)
06-29 23:56:41.099: WARN/System.err(8487):     at com.me2youmob.cwrap.ChickenWrapActivity.onCreate(ChickenWrapActivity.java:23)
06-29 23:56:41.099: WARN/System.err(8487):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1066)
06-29 23:56:41.099: WARN/System.err(8487):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2797)
06-29 23:56:41.108: WARN/System.err(8487):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2854)
06-29 23:56:41.108: WARN/System.err(8487):     at android.app.ActivityThread.access$2300(ActivityThread.java:136)
06-29 23:56:41.118: WARN/System.err(8487):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2179)
06-29 23:56:41.118: WARN/System.err(8487):     at android.os.Handler.dispatchMessage(Handler.java:99)
06-29 23:56:41.118: WARN/System.err(8487):     at android.os.Looper.loop(Looper.java:143)
06-29 23:56:41.118: WARN/System.err(8487):     at android.app.ActivityThread.main(ActivityThread.java:5068)
06-29 23:56:41.118: WARN/System.err(8487):     at java.lang.reflect.Method.invokeNative(Native Method)
06-29 23:56:41.118: WARN/System.err(8487):     at java.lang.reflect.Method.invoke(Method.java:521)
06-29 23:56:41.118: WARN/System.err(8487):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:858)
06-29 23:56:41.118: WARN/System.err(8487):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
06-29 23:56:41.118: WARN/System.err(8487):     at dalvik.system.NativeStart.main(Native Method)

Another thing in this issue is that earlier I had the buttons within a linear layout which was then within a Relative Layout. I thought it was not able to reach the relative layout and remove the linear layout section. It still gives me the same error.

Any idea what is going on here ?

NEW UPDATE:

    @Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    RelativeLayout rlM = (RelativeLayout) findViewById(R.id.rlMain);
    if (rlM == null)
    {
        Log.d("NULL CHECK","Layout is null");
    }
    else
    {
        Log.d("NULL CHECK","Layout is not null");
    }
    //spPreferences = getSharedPreferences(PREFS_NAME, 0);
    //loadMBIIntoView();
    //handleButtonClicks();
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

简美 2024-11-24 23:11:04

View.findViewById() 函数只会查找您所在的当前视图的子视图。

在这种情况下,您尝试在子视图之一中获取主视图,或者您尚未在 Activity 类中使​​用 setContentView()

如果您想设置背景,我强烈建议您从“活动”中进行设置。

The View.findViewById() function will only find views that are children of the current view you are in.

In this case, you're trying to obtain the main view either within one of the children views, or you have not used setContentView() within the Activity class yet.

If you are trying to set your background, I would highly suggest doing so from the Activity.

巾帼英雄 2024-11-24 23:11:04

我刚才尝试了您的代码,没问题,我认为您还没有调用 setContentView()。
ps:获取主视图的指针就可以了。

I try your code just now ,no problem,I think you haven' t invokered the setContentView().
ps:get the pointer of the main view is OK.

岁月无声 2024-11-24 23:11:04

另一种选择是使用 LayoutInflater,如下所示:

LayoutInflater inflater = getLayoutInflater();
RelativeLayout rlMain = (RelativeLayout) inflater.inflate(R.layout.rlMain, null);

//do stuff with rlMain

setContentView(rlMain);

Another option is to use LayoutInflater like this:

LayoutInflater inflater = getLayoutInflater();
RelativeLayout rlMain = (RelativeLayout) inflater.inflate(R.layout.rlMain, null);

//do stuff with rlMain

setContentView(rlMain);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文