findViewById()方法在某些设备上返回null对象

时间:2016-06-15 05:20:08

标签: android android-layout imageview picasso findviewbyid

我正在使用Picasso库加载图片。但findViewById()方法在三星7英寸标签上返回null对象而不是ImageView对象,但在较小的移动设备上完美运行。

代码段:

public class SplashScreenActivity extends FragmentActivity {
private ProgressBar progressBar;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    getWindow().getAttributes().windowAnimations = R.style.ActivityTransition;
    setContentView(R.layout.activity_splash);
    Picasso.with(this).load(R.drawable.splash_new).into((ImageView) findViewById(R.id.background_image));
    progressBar = (ProgressBar) findViewById(R.id.progress_bar);
    registerForGcm();
}

}

堆栈跟踪

    Fatal Exception: java.lang.RuntimeException: Unable to start      activity ComponentInfo{com.example.buyer/com.example.buyer.activity.SplashScreenActivity}:                
java.lang.IllegalArgumentException: Target must not be null.    
       at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2262)
       at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2316)
       at android.app.ActivityThread.access$700(ActivityThread.java:158)
       at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1296)
       at android.os.Handler.dispatchMessage(Handler.java:99)
       at android.os.Looper.loop(Looper.java:176)
       at android.app.ActivityThread.main(ActivityThread.java:5365)
       at java.lang.reflect.Method.invokeNative(Method.java)
       at java.lang.reflect.Method.invoke(Method.java:511)
       at         com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1102)
       at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:869)
       at dalvik.system.NativeStart.main(NativeStart.java)
    Caused by java.lang.IllegalArgumentException: Target must not be null.

各种外形规格的布局文件:

布局/ activity_splash

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

<ImageView
    android:id="@+id/background_image"
    android:layout_width="fill_parent"
    android:layout_height="match_parent"
    android:scaleType="centerCrop"
    android:src="@drawable/splash_new" />

<ProgressBar
    android:id="@+id/progress_bar"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:layout_centerHorizontal="true"
    android:layout_marginBottom="150dp" />
</RelativeLayout>

大/ activity_splash

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

<ImageView
    android:id="@+id/background_image"
    android:scaleType="centerCrop"
    android:layout_width="fill_parent"
    android:layout_height="match_parent" />

<ProgressBar
    android:id="@+id/progress_bar"
    android:layout_marginBottom="230dp"
    android:layout_centerHorizontal="true"
    android:layout_alignParentBottom="true"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" />
</RelativeLayout>

小/ activity_splash

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

<ImageView
    android:id="@+id/background_image"
    android:scaleType="centerCrop"
    android:layout_width="fill_parent"
    android:layout_height="match_parent" />

<ProgressBar
    android:id="@+id/progress_bar"
    android:layout_marginBottom="120dp"
    android:layout_centerHorizontal="true"
    android:layout_alignParentBottom="true"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" />
</RelativeLayout>

XLARGE / activity_splash

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

<ImageView
    android:id="@+id/background_image"
    android:layout_width="fill_parent"
    android:layout_height="match_parent"
    android:scaleType="centerCrop"/>

<ProgressBar
    android:id="@+id/progress_bar"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:layout_centerHorizontal="true"
    android:layout_marginBottom="350dp" />

 </RelativeLayout>

0 个答案:

没有答案