无法将“android.view.View"转换为“com.example.shabeer.listview.ListView" [英] Cannot cast 'android.view.View' to 'com.example.shabeer.listview.ListView'

查看:18
本文介绍了无法将“android.view.View"转换为“com.example.shabeer.listview.ListView"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在这行出现错误

list_view = (ListView)findViewById(R.id.listView_id); 显示错误

不可转换类型;无法将 android.view.View 投射到com.example.shabeer.listview.ListView

inconvertible type; cannot cast android.view.View to com.example.shabeer.listview.ListView

还有 list_view.setAdapter(adapter); 不起作用.

ListView 类:

ListView class:

package com.example.shabeer.listview;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.ArrayAdapter;

public class ListView extends AppCompatActivity {

    public static ListView list_view;
    private static String[] NAMES = new String[] { "Tom", "John", "Jack", "Pal"};

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_list_view);
    }

    public void listView(){
         list_view = (ListView)findViewById(R.id.listView_id);
        ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, R.layout.name_list, NAMES);
        list_view.setAdapter(adapter);
    }


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

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }
}

activity_list_view.xml:

activity_list_view.xml:

<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:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:orientation="vertical"
    android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".ListView">


    <ListView
        android:id="@+id/listView_id"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal" />

</LinearLayout>

AndroidManifect.xml:

AndroidManifect.xml:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.shabeer.listview" >

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name=".ListView"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

推荐答案

您不能将 Android 列表视图转换为您自己的列表视图.

You cannot cast an Android listview to your own listview.

您需要一个android.widget.ListView"而不是com.example.shabeer.listview.ListView".这是您在 xml 布局文件中引用的那个.

Instead of "com.example.shabeer.listview.ListView" you need a "android.widget.ListView". This is the one you are referencing in your xml layout file.

public static android.widget.ListView list_view;

list_view = (android.widget.ListView)findViewById(R.id.listView_id);

这篇关于无法将“android.view.View"转换为“com.example.shabeer.listview.ListView"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆