我尝试使用数组适配器填充列表。我有3个类,Bike,CustomAdapter和UseCustomAdapter。我有一个例子,我已经通过它将它与我的比较,似乎无法弄清楚它为什么不运行!只是说,每当我尝试加载它时,它就停止了工作。这是代码:
Bike.java
package com.example.tasktwo;
public class Bike {
private String title;
private String imageName;
//Constructor
Bike (String title, String imageName) {
this.title= title;
this.imageName = imageName;
}
public void setTitle(String title){
this.title = title;
}
public void setImage(String imageName){
this.imageName = imageName;
}
public String getBikeTitle(){
return title;
}
public String getBikeImageName(){
return imageName;
}
}
CustomerAdapter.java
public class CustomAdapter extends ArrayAdapter<Bike> {
private final Context context;
//List of Walk Objects
private ArrayList<Bike> bikes;
//Receives the context and the list of walk objects
public CustomAdapter (Context context, ArrayList<Bike> bikes) {
super(context, R.layout.bike_view, bikes);
this.context = context;
this.bikes = bikes;
}
@SuppressLint("ViewHolder")
public View getView(int position, View convertView, ViewGroup parent) {
Bike bike = bikes.get(position);
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View rowView = inflater.inflate(R.layout.bike_view, parent, false);
TextView textView = (TextView) rowView.findViewById(R.id.bike_names);
textView.setText(bike.getBikeTitle());
ImageView imageView = (ImageView) rowView.findViewById(R.id.bike_image);
Resources res = context.getResources();
int imageId = res.getIdentifier(bike.getBikeImageName(), "drawable", "com.example.tasktwo");
imageView.setImageResource(imageId);
return rowView;
}
}
UseCustomerAdapter.java
package com.example.tasktwo;
public class UseCustomAdapter extends ListActivity {
ArrayList<Bike> bikes = new ArrayList<Bike>();
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
bikes.add(new Bike("Honda", "honda"));
bikes.add(new Bike("Suzuki", "suzuki"));
bikes.add(new Bike("Kawasaki", "kawasaki"));
bikes.add(new Bike("KTM", "ktm"));
CustomAdapter adapter = new CustomAdapter(this, bikes);
setListAdapter(adapter);
}
protected void onListItemClick(ListView l, View v, int position, long id) {
super.onListItemClick(l, v, position, id);
Bike b = bikes.get(position);
Toast.makeText(this,"You selected the " + b.getBikeTitle()
+ "Motocross Bike",Toast.LENGTH_SHORT).show();
}
}
logcat的:
03-10 14:34:53.240: E/AndroidRuntime(1009): FATAL EXCEPTION: main
03-10 14:34:53.240: E/AndroidRuntime(1009): Process: com.example.tasktwo, PID: 1009
03-10 14:34:53.240: E/AndroidRuntime(1009): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.tasktwo/com.example.tasktwo.BikeList}: java.lang.NullPointerException
03-10 14:34:53.240: E/AndroidRuntime(1009): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2195)
03-10 14:34:53.240: E/AndroidRuntime(1009): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
03-10 14:34:53.240: E/AndroidRuntime(1009): at android.app.ActivityThread.access$800(ActivityThread.java:135)
03-10 14:34:53.240: E/AndroidRuntime(1009): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
03-10 14:34:53.240: E/AndroidRuntime(1009): at android.os.Handler.dispatchMessage(Handler.java:102)
03-10 14:34:53.240: E/AndroidRuntime(1009): at android.os.Looper.loop(Looper.java:136)
03-10 14:34:53.240: E/AndroidRuntime(1009): at android.app.ActivityThread.main(ActivityThread.java:5017)
03-10 14:34:53.240: E/AndroidRuntime(1009): at java.lang.reflect.Method.invokeNative(Native Method)
03-10 14:34:53.240: E/AndroidRuntime(1009): at java.lang.reflect.Method.invoke(Method.java:515)
03-10 14:34:53.240: E/AndroidRuntime(1009): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
03-10 14:34:53.240: E/AndroidRuntime(1009): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
03-10 14:34:53.240: E/AndroidRuntime(1009): at dalvik.system.NativeStart.main(Native Method)
03-10 14:34:53.240: E/AndroidRuntime(1009): Caused by: java.lang.NullPointerException
03-10 14:34:53.240: E/AndroidRuntime(1009): at com.example.tasktwo.BikeList.onCreate(BikeList.java:20)
03-10 14:34:53.240: E/AndroidRuntime(1009): at android.app.Activity.performCreate(Activity.java:5231)
03-10 14:34:53.240: E/AndroidRuntime(1009): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
03-10 14:34:53.240: E/AndroidRuntime(1009): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2159)
03-10 14:34:53.240: E/AndroidRuntime(1009): ... 11 more
的AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.tasktwo"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="19"
android:targetSdkVersion="19" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name=".BikeList"
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>
答案 0 :(得分:0)
根据您的回答,我认为这是解决方案。
<activity android:name=".MainActivity" >
</activity>
请注意,名称为 MainActivity ,这是默认名称。此名称取决于主Activity类的名称。因此,将其设置为类名称。 Android Studio或Eclipse的问题在于,当您开始更改项目名称时,所有引用都没有正确更改。