GridView不可滚动

时间:2014-07-06 07:27:31

标签: android android-layout gridview

  <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".Result" >


<ImageView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:background="@drawable/mall"
    android:contentDescription="mall"
    android:padding="30dp"
    />

<RelativeLayout
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginTop="35dp"
    >
<TextView
    android:id="@+id/default1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Address"
    />
<TextView
    android:id="@+id/addressTxt"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="******************" 
    android:layout_toRightOf="@+id/default1"
    />



</RelativeLayout>

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Nearest Mrt Would Be:  "
    android:layout_marginBottom="35dp"
    android:textSize="20dp"
    />
<TextView
    android:id="@+id/NearestMrt"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="..............."
    android:padding="10dp"
    android:layout_marginBottom="35dp"
    android:textSize="20dp"
    />


<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Bus Available there would be: "
    android:layout_marginBottom="35dp"
    android:textSize="20dp"
    android:gravity="center"
    />
 <GridView
    android:id="@+id/gridView1"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_margin="4dp"
    android:columnWidth="80dp"
    android:gravity="center"
    android:numColumns="auto_fit"
    android:stretchMode="columnWidth" >
</GridView>
</LinearLayout>

尝试向下滚动页面以查看时出现问题 Gridview 。 我尝试过滚动视图但每次运行应用程序时都会出现错误。 它是一个自定义网格视图顺便说一句 我需要提供的任何其他代码? 有什么建议? logcat的:

07-06 03:05:34.994: E/AndroidRuntime(2019): FATAL EXCEPTION: main
07-06 03:05:34.994: E/AndroidRuntime(2019): Process: com.example.caloriecal, PID: 2019

07-06 03:05:34.994: E/AndroidRuntime(2019): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.caloriecal/com.example.caloriecal.Result}: java.lang.ClassCastException: android.widget.TextView cannot be cast to android.widget.GridView

07-06 03:05:34.994: E/AndroidRuntime(2019):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2176)

07-06 03:05:34.994: E/AndroidRuntime(2019):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2226)

07-06 03:05:34.994: E/AndroidRuntime(2019):     at android.app.ActivityThread.access$700(ActivityThread.java:135)

07-06 03:05:34.994: E/AndroidRuntime(2019):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1397)

07-06 03:05:34.994: E/AndroidRuntime(2019):     at android.os.Handler.dispatchMessage(Handler.java:102)

07-06 03:05:34.994: E/AndroidRuntime(2019):     at android.os.Looper.loop(Looper.java:137)

07-06 03:05:34.994: E/AndroidRuntime(2019):     at android.app.ActivityThread.main(ActivityThread.java:4998)

07-06 03:05:34.994: E/AndroidRuntime(2019):     at java.lang.reflect.Method.invokeNative(Native Method)

07-06 03:05:34.994: E/AndroidRuntime(2019):     at java.lang.reflect.Method.invoke(Method.java:515)

07-06 03:05:34.994: E/AndroidRuntime(2019):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:777)

07-06 03:05:34.994: E/AndroidRuntime(2019):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:593)

07-06 03:05:34.994: E/AndroidRuntime(2019):     at dalvik.system.NativeStart.main(Native Method)

07-06 03:05:34.994: E/AndroidRuntime(2019): Caused by: java.lang.ClassCastException: android.widget.TextView cannot be cast to android.widget.GridView

07-06 03:05:34.994: E/AndroidRuntime(2019):     at com.example.caloriecal.Result.onCreate(Result.java:39)

07-06 03:05:34.994: E/AndroidRuntime(2019):     at android.app.Activity.performCreate(Activity.java:5243)

07-06 03:05:34.994: E/AndroidRuntime(2019):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)

07-06 03:05:34.994: E/AndroidRuntime(2019):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2140)

07-06 03:05:34.994: E/AndroidRuntime(2019):     ... 11 more

onCreate of Result

    protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_result);
    Bitmap homeIcon =BitmapFactory.decodeResource(this.getResources(),R.drawable.home);
    Bitmap userIcon=BitmapFactory.decodeResource(this.getResources(),R.drawable.personal);      
    gridArray.add(new Item(homeIcon,"Home"));
    gridArray.add(new Item(userIcon,"Personal"));
    gridArray.add(new Item(homeIcon,"Home"));
    gridArray.add(new Item(userIcon,"User"));
    gridArray.add(new Item(homeIcon,"Building"));
    gridArray.add(new Item(userIcon,"User"));
    gridArray.add(new Item(homeIcon,"Home"));
    gridArray.add(new Item(userIcon,"xyz"));
    gridArray.add(new Item(homeIcon,"Home"));
    gridArray.add(new Item(userIcon,"User"));
    gridArray.add(new Item(homeIcon,"House"));
    gridArray.add(new Item(userIcon,"Friend"));



    gridView = (GridView) findViewById(R.id.gridView1);
    customGridAdapter = new CustomGridViewAdapter(this, R.layout.row_grid, gridArray);
    gridView.setAdapter(customGridAdapter);
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
//39
    getMenuInflater().inflate(R.menu.result, menu);
    return true;
}

2 个答案:

答案 0 :(得分:0)

此错误表示所有内容

  

ClassCastException:android.widget.TextView无法强制转换为   android.widget.GridView

您可以在com.example.caloriecal.Result课程内,onCreate方法,第39行进行。您可能使用findViewById来查找TextView,但之后尝试将其投放到GridView

答案 1 :(得分:0)

  

07-06 03:05:34.994:E / AndroidRuntime(2019):引起:   java.lang.ClassCastException:无法转换android.widget.TextView   到android.widget.GridView

     

07-06 03:05:34.994:E / AndroidRuntime(2019):at   com.example.caloriecal.Result.onCreate(Result.java:39)

检查您的变量gridView是否已声明为TextViewGridView

相关问题