我正在使用改造从服务器获取JSON,现在我想使用listView来显示它,但显然我卡住了。我对android比较新,所以我可能会做的完全错了。 我试图创建一个ListView并将POJO对象提供给适配器,但适配器似乎崩溃导致零点异常。任何帮助将不胜感激
清单:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.nmc.viewbeem">
<uses-permission android:name="android.permission.INTERNET"/>
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity
android:name=".MainActivity"
android:label="@string/app_name"
android:theme="@style/AppTheme.NoActionBar">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
&#13;
主要活动:
package com.nmc.viewbeem;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.ListAdapter;
import android.widget.ListView;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
Retrofit retrofit = new Retrofit.Builder()
.baseUrl("http://nmc.no-ip.org:8888/")
.addConverterFactory(GsonConverterFactory.create())
.build();
StreamingOfferService service = retrofit.create(StreamingOfferService.class);
Call<ResponseResult> response = service.getStreamingOffers();
response.enqueue(new Callback<ResponseResult>() {
@Override
public void onResponse(Call<ResponseResult> result, Response<ResponseResult> response) {
ListView listview = (ListView) findViewById(R.id.listview);
StreamingOffersAdapter<ListAdapter> adapter = new StreamingOffersAdapter(this,R.layout.listofusers,response);
listview.setAdapter(adapter);
}
@Override
public void onFailure(Call<ResponseResult> result, Throwable t) {
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
&#13;
适配器:
package com.nmc.viewbeem;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import java.util.List;
import retrofit2.Response;
/**
* Created by Katerina on 07.04.2016.
*/
public class StreamingAdapter extends BaseAdapter {
private Context context;
private int resource;
private LayoutInflater inflater;
private List<StreamingOffer> streamingOfferList;
public StreamingAdapter(Context context, int resource, Response<ResponseResult> response) {
this.context=context;
this.resource=resource;
this.inflater=LayoutInflater.from(context);
streamingOfferList = response.body().getData();
}
public class Holder extends User {
private TextView full_name;
private TextView location;
private TextView price;
private TextView currency_symbol;
private TextView total_streamed;
private ImageView background;
private ImageView offer;
private ImageView profile;
private ImageView iconUser;
private ImageView iconStar;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
Holder holder = new Holder();
View itemView = convertView;
StreamingOffer streamingOffer = getItem(position);
User u = streamingOffer.getUser();
if(itemView==null) {
itemView = inflater.inflate(resource, parent, false);
itemView.setTag(holder);
holder.full_name = (TextView) itemView.findViewById(R.id.full_name);
holder.full_name.setText(u.getFullName());
holder.location = (TextView) itemView.findViewById(R.id.location);
holder.location.setText(streamingOffer.getLocation());
holder.price = (TextView) itemView.findViewById(R.id.price);
holder.price.setText(streamingOffer.getPrice());
holder.currency_symbol = (TextView) itemView.findViewById(R.id.currency_symbol);
holder.currency_symbol.setText(streamingOffer.getCurrencySymbol());
holder.total_streamed = (TextView) itemView.findViewById(R.id.total_streamed);
holder.total_streamed.setText(u.getTotalStreamed());
}
return itemView;
}
@Override
public int getCount() {
return streamingOfferList.size();
}
@Override
public StreamingOffer getItem(int position) {
return streamingOfferList.get(position);
}
@Override
public long getItemId(int position) {
return 0;
}
}
&#13;
服务
package com.nmc.viewbeem;
import android.provider.SyncStateContract;
import java.util.List;
import retrofit2.Call;
import retrofit2.http.Path;
import retrofit2.http.Query;
import retrofit2.http.GET;
import retrofit2.http.Headers;
/**
* Created by Katerina on 30.03.2016.
*/
public interface StreamingOfferService {
@Headers("api-key: ukGspo1hmXGp1BkxzAMRYWcOsmpvacHlxPKKkmZX")
@GET("api/v1/offers")
Call<ResponseResult> getStreamingOffers();
}
&#13;
和logCat:
04-06 16:27:13.899 3518-3518/? E/Zygote: MountEmulatedStorage()
04-06 16:27:13.899 3518-3518/? E/Zygote: v2
04-06 16:27:13.899 3518-3518/? I/libpersona: KNOX_SDCARD checking this for 10272
04-06 16:27:13.899 3518-3518/? I/libpersona: KNOX_SDCARD not a persona
04-06 16:27:13.939 3518-3518/? I/SELinux: Function: selinux_compare_spd_ram, SPD-policy is existed. and_ver=SEPF_SM-G900F_5.0 ver=27
04-06 16:27:13.939 3518-3518/? I/SELinux: Function: selinux_compare_spd_ram , priority [2] , priority version is VE=SEPF_SM-G900F_5.0-1_0032
04-06 16:27:13.939 3518-3518/? E/SELinux: [DEBUG] get_category: variable seinfo: default sensitivity: NULL, cateogry: NULL
04-06 16:27:13.939 3518-3518/? I/art: Late-enabling -Xcheck:jni
04-06 16:27:13.999 3518-3518/? D/TimaKeyStoreProvider: TimaSignature is unavailable
04-06 16:27:14.009 3518-3518/? D/ActivityThread: Added TimaKeyStore provider
04-06 16:27:14.109 3518-3518/com.nmc.viewbeem D/ResourcesManager: creating new AssetManager and set to /data/app/com.nmc.viewbeem-1/base.apk
04-06 16:27:14.379 3518-3518/com.nmc.viewbeem D/Activity: performCreate Call secproduct feature valuefalse
04-06 16:27:14.379 3518-3518/com.nmc.viewbeem D/Activity: performCreate Call debug elastic valuetrue
04-06 16:27:14.399 3518-3588/com.nmc.viewbeem D/OpenGLRenderer: Render dirty regions requested: true
04-06 16:27:14.429 3518-3588/com.nmc.viewbeem I/Adreno-EGL: <qeglDrvAPI_eglInitialize:410>: EGL 1.4 QUALCOMM build: ()
OpenGL ES Shader Compiler Version: E031.25.01.03
Build Date: 03/03/15 Tue
Local Branch: LA.BF.1.1_RB1_20150108_025_1077123_1158499
Remote Branch:
Local Patches:
Reconstruct Branch:
04-06 16:27:14.429 3518-3588/com.nmc.viewbeem I/OpenGLRenderer: Initialized EGL, version 1.4
04-06 16:27:14.499 3518-3588/com.nmc.viewbeem I/OpenGLRenderer: HWUI protection enabled for context , &this =0xaf922088 ,&mEglDisplay = 1 , &mEglConfig = 8
04-06 16:27:14.499 3518-3588/com.nmc.viewbeem D/OpenGLRenderer: Enabling debug mode 0
04-06 16:27:14.589 3518-3518/com.nmc.viewbeem I/Timeline: Timeline: Activity_idle id: android.os.BinderProxy@1ddb875a time:316868548
04-06 16:27:14.909 3518-3518/com.nmc.viewbeem D/AndroidRuntime: Shutting down VM
04-06 16:27:14.909 3518-3518/com.nmc.viewbeem E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.nmc.viewbeem, PID: 3518
java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.ListView.setAdapter(android.widget.ListAdapter)' on a null object reference
at com.nmc.viewbeem.MainActivity$1.onResponse(MainActivity.java:45)
at retrofit2.ExecutorCallAdapterFactory$ExecutorCallbackCall$1$1.run(ExecutorCallAdapterFactory.java:68)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:145)
at android.app.ActivityThread.main(ActivityThread.java:5832)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1399)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1194)
&#13;
布局:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/profile_layout">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="RECENT STREAMING OFFERS"
android:gravity="center"
android:textStyle="bold"
/>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
>
<ImageView
android:layout_width="100dp"
android:layout_height="100dp"
android:id="@+id/offer"
android:layout_alignParentStart="true" />
<ImageView
android:layout_width="50dp"
android:layout_height="50dp"
android:id="@+id/profile"
android:layout_alignParentTop="true"
android:layout_toEndOf="@+id/offer" />
<ImageView
android:layout_width="10dp"
android:layout_height="10dp"
android:id="@+id/iconUser"
android:layout_toRightOf="@+id/offer"/>
<ImageView
android:layout_height="50dp"
android:layout_width="200dp"
android:id="@+id/iconStar"
android:layout_below="@+id/offer"
android:layout_alignEnd="@+id/location" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/full_name"
android:layout_toRightOf="@+id/profile"
android:text="Name Surname"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/offered"
android:text="Offered location to stream:"
android:layout_alignBottom="@+id/profile"
android:layout_toEndOf="@+id/profile" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/location"
android:text="Location"
android:layout_below="@+id/profile"
android:layout_toEndOf="@+id/profile" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/price"
android:text="10.00"
android:layout_alignParentTop="true"
android:layout_toEndOf="@+id/stream" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/currency_symbol"
android:text="$"
android:layout_alignParentTop="true"
android:layout_toEndOf="@+id/price" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/stream"
android:text="Stream time:"
android:layout_alignTop="@+id/iconStar"
android:layout_alignEnd="@+id/offered" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/total_streamed"
android:text="15 min:"
android:layout_alignTop="@+id/stream"
android:layout_alignParentEnd="true"
android:layout_alignBottom="@+id/stream"
android:layout_toEndOf="@+id/stream" />
</RelativeLayout>
</LinearLayout>
&#13;
的ListView:
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ListView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/listview">
</ListView>
</FrameLayout>
&#13;
activity_main:
<?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:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="100dp"
android:minHeight="100dp">
tools:context="com.nmc.viewbeem.MainActivity">
</RelativeLayout>
&#13;
答案 0 :(得分:0)
发布R.layout.activity_main代码也是.....
我怀疑你提供的listview ID不正确
您必须在ListView
中提供activity_main
使用以下代码替换activity_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:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="100dp"
android:minHeight="100dp"
tools:context="com.nmc.viewbeem.MainActivity">
<ListView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/listview">
</ListView>
</RelativeLayout>
修改您的List Adapter
,如下所示:
package com.nmc.viewbeem;
import android.app.Activity;
import android.content.Context;
import android.database.DataSetObserver;
import android.support.v7.view.menu.MenuView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListAdapter;
import android.widget.TextView;
import com.nmc.viewbeem.StreamingOffers;
import retrofit2.Callback;
import retrofit2.Response;
/**
* Created by Katerina on 05.04.2016.
*/
public class StreamingOffersAdapter<L> extends User implements ListAdapter {
private Activity activity;
private Context context;
private Callback<ResponseResult> callback;
private Response<ResponseResult> response;
private LayoutInflater inflater;
public StreamingOffersAdapter(Activity activity) {
this.activity=activity;
}
public StreamingOffersAdapter(Context context,Callback<ResponseResult> callback, Response<ResponseResult> response) {
this.callback=callback;
this.response=response;
this.context=context;
inflater=(LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
public class Holder {
private TextView full_name;
private TextView offeredStreams;
private TextView location;
private TextView price;
private TextView currency_symbol;
private TextView total_streamed;
private ImageView background;
private ImageView offer;
private ImageView profile;
private ImageView iconUser;
private ImageView icoStar;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
Holder holder = new Holder();
View itemView = convertView;
itemView = inflater.inflate(R.layout.listofusers, null);
StreamingOffers s = new StreamingOffers();
User u = new User();
if(itemView==null) {
holder.full_name = (TextView) itemView.findViewById(R.id.full_name);
holder.full_name.setText(getFullName());
holder.location = (TextView) itemView.findViewById(R.id.location);
holder.location.setText(s.getLocation());
holder.price = (TextView) itemView.findViewById(R.id.price);
holder.price.setText(s.getPrice());
holder.currency_symbol = (TextView) itemView.findViewById(R.id.currency_symbol);
holder.currency_symbol.setText(s.getCurrencySymbol());
holder.total_streamed = (TextView) itemView.findViewById(R.id.total_streamed);
holder.total_streamed.setText(u.getTotalStreamed());
}
return itemView;
}
@Override
public boolean areAllItemsEnabled() {
return false;
}
@Override
public boolean isEnabled(int position) {
return false;
}
@Override
public void registerDataSetObserver(DataSetObserver observer) {
}
@Override
public void unregisterDataSetObserver(DataSetObserver observer) {
}
@Override
public int getCount() {
return response.getCount();
}
@Override
public Object getItem(int position) {
return response[position];
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public boolean hasStableIds() {
return false;
}
@Override
public int getItemViewType(int position) {
return 0;
}
@Override
public int getViewTypeCount() {
return 0;
}
@Override
public boolean isEmpty() {
return false;
}
}