我試圖開始一個新的活動,並且我得到一個NullPointerException
。 我不太確定我從哪裏得到NullPointerException
。在調試期間,應用程序在創建適配器時崩潰,但我看不到在代碼中獲得NullPointerException
的錯誤。ListView適配器:無法啓動活動ComponentInfo {..}:java.lang.NullPointerException
錯誤日誌:
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.app.app/com.app.APP.SpaActivity}: java.lang.NullPointerException
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2394)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2446)
at android.app.ActivityThread.access$600(ActivityThread.java:165)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1373)
at android.os.Handler.dispatchMessage(Handler.java:107)
at android.os.Looper.loop(Looper.java:194)
at android.app.ActivityThread.main(ActivityThread.java:5434)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:525)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:834)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.NullPointerException
at com.app.adapter.SpaListAdapter.<init>(SpaListAdapter.java:25)
at com.app.APP.SpaActivity.onCreate(SpaActivity.java:43)
at android.app.Activity.performCreate(Activity.java:5122)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1146)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2358)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2446)
at android.app.ActivityThread.access$600(ActivityThread.java:165)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1373)
at android.os.Handler.dispatchMessage(Handler.java:107)
at android.os.Looper.loop(Looper.java:194)
at android.app.ActivityThread.main(ActivityThread.java:5434)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:525)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:834)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
at dalvik.system.NativeStart.main(Native Method)
SpaActivity.java
public class SpaActivity extends Activity {
private ProgressDialog pDialog;
private List<Spa> SpaList = new ArrayList<Spa>();
private ListView listView;
private SpaListAdapter adapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_spa);
listView = (ListView) findViewById(R.id.list);
adapter = new SpaListAdapter(this, SpaList);
listView.setAdapter(adapter);
activity_spa.xml
<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">
<ListView
android:id="@+id/list"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:divider="@color/list_divider"
android:dividerHeight="1dp"
android:listSelector="@layout/list_row_selector" />
</RelativeLayout>
爲ListView適配器:
public class SpaListAdapter extends BaseAdapter {
private Activity activity;
private LayoutInflater inflater;
private List<Spa> SpaItems;
ImageLoader imageLoader = AppController.getInstance().getImageLoader();
public SpaListAdapter(Activity activity, List<Spa> SpaItems) {
this.activity = activity;
this.SpaItems = SpaItems;
}
@Override
public int getCount() {
return SpaItems.size();
}
@Override
public Object getItem(int location) {
return SpaItems.get(location);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
if (inflater == null)
inflater = (LayoutInflater) activity
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
if (convertView == null)
convertView = inflater.inflate(R.layout.list_row, null);
if (imageLoader == null)
imageLoader = AppController.getInstance().getImageLoader();
NetworkImageView thumbNail = (NetworkImageView) convertView
.findViewById(R.id.thumbnail);
TextView title = (TextView) convertView.findViewById(R.id.title);
TextView duration = (TextView) convertView.findViewById(R.id.duration);
TextView description = (TextView) convertView.findViewById(R.id.description);
TextView price = (TextView) convertView.findViewById(R.id.price);
// getting spa data for the row
Spa s = SpaItems.get(position);
// thumbnail image
thumbNail.setImageUrl(s.getThumbnailUrl(), imageLoader);
// title
title.setText(s.getTitle());
// duration
duration.setText("Time: " + String.valueOf(s.getDuration()));
// description
description.setText(String.valueOf(s.getDescription()));
// price
price.setText("Price: " + String.valueOf(s.getPrice()));
return convertView;
}
}
最後的適配器
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="@layout/list_row_selector"
android:padding="8dp" >
<!-- Thumbnail Image -->
<com.android.volley.toolbox.NetworkImageView
android:id="@+id/thumbnail"
android:layout_width="80dp"
android:layout_height="80dp"
android:layout_alignParentLeft="true"
android:layout_marginRight="8dp" />
<!-- Spa Title -->
<TextView
android:id="@+id/title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignTop="@+id/thumbnail"
android:layout_toRightOf="@+id/thumbnail"
android:textSize="@dimen/title"
android:textStyle="bold" />
<!-- Duration -->
<TextView
android:id="@+id/duration"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="@id/title"
android:layout_marginTop="1dip"
android:layout_toRightOf="@+id/thumbnail"
android:textSize="@dimen/rating" />
<!-- Description -->
<TextView
android:id="@+id/description"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="@id/duration"
android:layout_marginTop="5dp"
android:layout_toRightOf="@+id/thumbnail"
android:textColor="@color/genre"
android:textSize="@dimen/genre" />
<!-- Price -->
<TextView
android:id="@+id/price"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:textColor="@color/year"
android:textSize="@dimen/year" />
</RelativeLayout>
什麼是線SpaListAdapter.java:25 ??? –
這是這一行: ImageLoader imageLoader = AppController.getInstance()。getImageLoader(); SpaActivity上的43一個是這樣的: adapter = new SpaListAdapter(this,SpaList); – viperx21