我正在尝试创建一个介绍洛杉矶景点和洛杉矶餐厅的应用程序。主页是景点和餐厅的图片,当您单击其中一个时,它会打开更多景点或餐厅供人们阅读详细信息。我正在实施列表视图,但是我的 setadapter 不起作用。请看看我的代码并帮助新手!谢谢!错误代码如下,之后是我的程序代码。Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.ListView.setAdapter(android.widget.ListAdapter)' on a null object referenceMainActivity.java: package com.example.android.tourguide; import android.content.Context; import android.content.Intent; import android.media.Image; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.view.View; import android.widget.ImageView; import android.widget.Toast; public class MainActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); } public void attractionList(View v) { Intent intent = new Intent(this, attractionList.class); startActivity(intent); } }attractionList.java: package com.example.android.tourguide;import android.support.v7.app.AppCompatActivity;import android.os.Bundle;import android.widget.ListView;import java.util.ArrayList;public class attractionList extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_attraction_list); ArrayList<Attraction> attractionArrayList = new ArrayList<>(); attractionArrayList.add(new Attraction(R.drawable.griffith, R.raw.attraction, "Griffith Observatory")); AttractionAdapter attractionAdapter = new AttractionAdapter(this, attractionArrayList); ListView listView = (ListView) findViewById(R.id.list); listView.setAdapter(attractionAdapter); }}
1 回答
千万里不及你
TA贡献1784条经验 获得超9个赞
您尝试查找不存在的列表视图,activity_attraction_list.xml因此您的列表视图在您的情况下为空。
改变这个
public class attractionList extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_attraction_list); //Change this line
到
setContentView(R.layout.listview);
添加回答
举报
0/150
提交
取消