Android 在初始屏幕中找不到线性布局-空指针异常
当我运行我的应用程序时,我的启动屏幕拉伸得很厉害,所以我尝试了一个解决方案来手动调整抖动和清除Android 在初始屏幕中找不到线性布局-空指针异常,android,android-layout,Android,Android Layout,当我运行我的应用程序时,我的启动屏幕拉伸得很厉害,所以我尝试了一个解决方案来手动调整抖动和清除 @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_splash); BitmapFactory.Options myOptions = ne
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_splash);
BitmapFactory.Options myOptions = new BitmapFactory.Options();
myOptions.inDither = true;
myOptions.inScaled = false;
myOptions.inPreferredConfig = Bitmap.Config.ARGB_8888;
myOptions.inDither = false;
myOptions.inPurgeable = true;
Bitmap preparedBitmap = BitmapFactory.decodeResource(getApplication()
.getResources(), R.drawable.bg, myOptions);
Drawable background = new BitmapDrawable(preparedBitmap);
((LinearLayout) findViewById(R.id.layout_main))
.setBackgroundDrawable(background);
我设置了背景和配置选项,而不是放置飞溅的图像,这会导致糟糕的拉伸。
我的xml看起来像这样
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="@+id/layout_main">
</LinearLayout>
它找不到线性布局。有什么想法吗?救命啊 我解决了这个问题,在布局中添加了一些虚拟内容,比如带有src=@null的imageview。否则,布局不会膨胀。在那之后,我做了我正在做的事情,瞧。寓意:为了通过编程方式找到一个布局,增加一些虚拟内容,而不仅仅是布局(我不知道官方文档怎么说,但这是我在我的案例中发现的)出于好奇,您是否确保
可绘制背景
不会返回null
对象?尝试将findViewById
语句拆分为单独的步骤<代码>线性布局ll=findViewById(R…)代码>然后ll.setBackgroundDrawable(..)
。Insertnull
在两者之间进行检查,以查看NPE的确切生成位置。其at LINEARLLAYOUT ll-findViewById基本上返回null
((LinearLayout) findViewById(R.id.layout_main))
.setBackgroundDrawable(background);