尝试访问父布局时出现空指针异常
我有一个包含三个按钮的相对布局,我试图动态更改相对布局的背景。这是 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>
这是给我一个错误的 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
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.
我刚才尝试了您的代码,没问题,我认为您还没有调用 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.
另一种选择是使用 LayoutInflater,如下所示:
Another option is to use LayoutInflater like this: