Android GridView在Tab Simulator上返回Null值

时间:2013-03-20 15:58:05

标签: android android-layout android-intent android-widget

我有我的活动代码:

public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.movies_category);
      GridView gridView = (GridView) findViewById(R.id.grid_view);//Here It's retuning Null Value For Simulator Tab
   // Instance of ImageAdapter Class
      gridView.setAdapter(new ImageAdapter(this));
}

movies_category.xml文件:

<?xml version="1.0" encoding="utf-8"?>
 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/relativeLayout1"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:background="@drawable/screen_background">      


 <LinearLayout
                android:id="@+id/linearLayout1"
                android:layout_width="wrap_content"
                android:layout_height="fill_parent"
                android:orientation="vertical"
                android:layout_alignBottom="@+id/linearLayout01"
                android:layout_marginBottom="55dp"> 



<ImageView 
                  android:id="@+id/imageView1"
                  android:layout_width="fill_parent"
                  android:layout_height="50dp"
                  android:src="@drawable/banner"
                  android:layout_alignParentTop="true"
                  android:scaleType="fitXY"/>

<GridView 
    android:id="@+id/grid_view"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:numColumns="auto_fit"
    android:columnWidth="90dp"
    android:horizontalSpacing="10dp"
    android:verticalSpacing="10dp"
    android:gravity="center"
    android:stretchMode="columnWidth" >  

</GridView>
</LinearLayout>
</RelativeLayout>

当我尝试在Nexusone仿真器上执行应用程序时,我能够成功显示My GridView(gridView对象具有值)

但是当我尝试在Tab Emulator上执行时,相同的代码,我无法显示GridView(显示Null的gridView对象)

堆栈跟踪:

FATAL EXCEPTION: main
java.lang.RuntimeException: Unable to start activity 

    ComponentInfo{com.tel.movies/com.tel.movies.MovieCategoryViewAcivity}: java.lang.NullPointerException
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
        at android.app.ActivityThread.access$2300(ActivityThread.java:125)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
        at android.os.Handler.dispatchMessage(Handler.java:99)
        at android.os.Looper.loop(Looper.java:123)
        at android.app.ActivityThread.main(ActivityThread.java:4627)
        at java.lang.reflect.Method.invokeNative(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:521)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:871)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:629)
        at dalvik.system.NativeStart.main(Native Method)
     Caused by: java.lang.NullPointerException
        at com.oone.movies.MovieCategoryViewAcivity.onCreate(MovieCategoryViewAcivity.java:49)
        at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
        at android.app.ActivityThread.performLaunchActivity

有人可以帮助我吗?

1 个答案:

答案 0 :(得分:1)

对于Tabs模拟器以及具有不同屏幕分辨率和尺寸的移动模拟器,我们在Android中具有单独的布局文件夹(布局,布局 - 地面,布局 - 大,布局 - 大地等......)我们需要定义我们定义的Layout xml Files ..

在我的查询中,我需要在所有LayoutFolders中定义movies_category.xml文件以成功执行app