#android #android-layout #gridview
#Android #android-макет #просмотр сетки
Вопрос:
<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>
Возникли проблемы при попытке прокрутить страницу вниз, чтобы просмотреть
Просмотр сетки.
я пробовал просмотр прокрутки, но при каждом запуске приложения выдает ошибку.
Кстати, это пользовательский вид сетки
какой-либо другой код мне нужно предоставить?
Есть какие-нибудь советы?
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
При создании результата
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;
}
Комментарии:
1. сообщение об ошибке logcat, какова связь между этим вопросом и
asp.net
?2. о, это мой первый раз на stack exchange, и я просто использовал предложенные теги.
3.
java.lang.ClassCastException: android.widget.TextView cannot be cast to android.widget.GridView
опубликуйте код результата onCreate() и укажите строку 394. попробуйте очистить свой проект и опубликовать весь код этого класса
Ответ №1:
07-06 03:05:34.994: E / AndroidRuntime (2019): Вызвано: java.lang.Исключение ClassCastException: android.widget.TextView не может быть преобразован в android.widget.Просмотр сетки
07-06 03:05:34.994: E / AndroidRuntime (2019): на com.example.caloriecal.Result.onCreate(Результат.java:39)
Проверьте, gridView
объявлена ли ваша переменная как TextView
или a GridView
.
Ответ №2:
Эта ошибка говорит сама за себя
Исключение ClassCastException: android.widget.TextView не может быть преобразован в android.widget.Просмотр сетки
И вы делаете это внутри com.example.caloriecal.Result
класса, в onCreate
методе, строке 39
. Вы, вероятно, используете findViewById
для поиска TextView
, но затем пытаетесь преобразовать его в GridView
.