應用程序不工作我不知道原因。Android應用程序 - ListView不工作
CustomView.java
public class CustomView extends Activity {
ListView list;
String titles[] = getResources().getStringArray(R.array.titles);
int images[] = getResources().getIntArray(R.array.movies);
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
CustomList adapter = new CustomList(CustomView.this);
list=(ListView)findViewById(R.id.listview);
list.setAdapter(adapter);
list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Toast.makeText(getBaseContext(),titles[+position],Toast.LENGTH_LONG).show();
}
});
}
public class CustomList extends ArrayAdapter<String>{
final Activity context;
public CustomList(Activity context){
super(context,R.layout.listitem,titles);
this.context = context;
}
@Override
public View getView(int pos, View convertView, ViewGroup parent){
double randomValue = Math.random();
double randomYear = Math.random();
LayoutInflater inflater =context.getLayoutInflater();
View rowView = inflater.inflate(R.layout.listitem,null,true);
ImageView imageView = (ImageView)rowView.findViewById(R.id.image);
TextView title = (TextView)rowView.findViewById(R.id.title);
TextView rating = (TextView)rowView.findViewById(R.id.rating);
TextView genre = (TextView)rowView.findViewById(R.id.genre);
TextView year = (TextView)rowView.findViewById(R.id.year);
title.setText(titles[pos]);
imageView.setImageResource(images[pos]);
rating.setText(((int)randomValue*9)+0+"."+pos) ;
genre.setText("DRAMA");
year.setText(((int)randomYear+1960)+1930);
return rowView;
}
}
}
佈局文件 main.xml中
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.example.kim.customlistview.CustomView">
<ListView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/listview" />
</RelativeLayout>
列表項listitem.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#eeeeee"
android:padding="8dp"
>
<ImageView
android:layout_width="80dp"
android:layout_height="80dp"
android:id="@+id/image"
android:layout_alignParentLeft="true"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/title"
android:layout_alignTop="@+id/image"
android:layout_toRightOf="@+id/image"
android:textSize="17dp"
/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/rating"
android:layout_below="@+id/title"
android:layout_marginTop="1dip"
android:layout_toRightOf="@id/image"
android:textSize="15dip"
/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/genre"
android:layout_below="@+id/rating"
android:layout_marginTop="5dp"
android:layout_toRightOf="@id/image"
android:textSize="13dip"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/year"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:textSize="12dip"
/>
</RelativeLayout>
資源string.xml
<resources>
<string name="app_name">CustomListView</string>
<string-array name="titles">
<item>"The Wizard of Oz (1939)"</item>
<item>"Citizen kane (1950)"</item>
<item>"A Hard Day's Night (1934)"</item>
<item>"A Funny (1959)"</item>
<item>"HELLO (1943)"</item>
</string-array>
</resources>
array.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<array name="movies">
<item>@drawable/movie1</item>
<item>@drawable/movie2</item>
<item>@drawable/movie3</item>
<item>@drawable/movie4</item>
<item>@drawable/movie5</item>
</array>
</resources>
堆棧跟蹤:
10-29 16:25:26.221 9745-9745/? E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.kim.customlistview, PID: 9745
java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.example.kim.customlistview/com.example.kim.customlistview.CustomView}: java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.res.Resources android.content.Context.getResources()' on a null object reference
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.res.Resources android.content.Context.getResources()' on a null object reference
at android.content.ContextWrapper.getResources(ContextWrapper.java:92)
最後字符串的標題[] = getResources()getStringArray(R.array.titles); int images [] = getResources()。getIntArray(R.array.movies);添加到onCreate()但不工作 –