Java - Android Studio - .xml文件不存在错误



我使用的是Android Studio。我目前正在尝试制作一个自定义适配器,但我的.xml文件遇到了问题。尽管我已经创建了它们,并添加了我希望在其中看到的内容,但当我在主活动Java文件中调用它们时,我会收到一个错误,说它不存在。此外,SetOnItemClickListener和setAdapter将不起作用。我的其他文件都没有显示任何错误。

.xml,标题为characteritem_layout:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/detail_name"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/detail_status"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/detail_explanation"/>
</LinearLayout>

我的主要活动代码:

package com.example.app.activities;
import ...
public class MainActivity extends AppCompatActivity {
private Button denButton;
private Button sweButton;
private Button aboutButton;
private TextView welcome;
private ArrayList<CharacterItem> characters;
private ListView charList;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
setContentView(R.layout.characteritem_layout)
welcome = findViewById(R.id.welcome_screen);
//The other buttons work perfectly well.
initializeList();
final CharacterAdapter charAdapter = new CharacterAdapter(this, R.layout.characteritem_layout, characters);
characters.setAdapter(charAdapter);
characters.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Intent intent = new Intent(getApplicationContext(), CharacterActivity.class);
intent.putExtra("charItem", characters.get(position));

startActivity(intent);
}
});
private void initializeList(){
characters = new ArrayList<CharacterItem>();
characters.add(new CharacterItem("Finland", false, "Not in progress yet"));
characters.add(new CharacterItem("Norway", true, "Getting the Viking trio in first!"));
characters.add(new CharacterItem("Iceland",false,"He's next!"));
}

}

首先,两次使用setContentView不会以您想要的方式工作

在我的情况下,右键单击布局文件夹,然后选择新的布局资源文件作品

最新更新