我在这行遇到错误
list_view = (ListView)findViewById(R.id.listView_id);
显示错误
不可转换型;无法将 android.view.View 投射到 com.example.shabeer.listview.ListView
而且
list_view.setAdapter(adapter);
也不起作用。
ListView类:
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:
<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:
<?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>
将类名 “ListView” 更改为 “OtherName”
public class ListView extends AppCompatActivity {
到
public class ListViewSample extends AppCompatActivity {
您无法将 Android 列表视图转换为您自己的列表视图。
您需要一个“android.widget.ListView”,而不是“com.example.shabeer.listview.ListView”。这是您在 xml 布局文件中引用的文件。
public static android.widget.ListView list_view;
和
list_view = (android.widget.ListView)findViewById(R.id.listView_id);
在
listView()
内调用onCreate
方法然后尝试,应该可以工作。
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_list_view);
listView();
}
编辑: 如果您的
ListView
是 View
,这就是答案,但现在我发现它实际上是 Activity
。 ListView
是一个非常具有误导性的活动课程名称!
在 XML 中使用完全限定的类名:
<com.example.shabeer.listview.ListView
android:id="@+id/listView_id"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal" />
否则,它会假设您的意思是
android.widget.ListView
。
Java 类名不应与 Android 小部件“ListView”同名,因为这会引起混淆。