Ich versuche einen Kundenadapter auf ListView zu setzen, erhalte aber immer NullException. Das ist mein Adapter:Benutzeradapter kann nicht auf ListView gesetzt werden
public class MainActivity extends AppCompatActivity {
private static final String TAG = MainActivity.class.getSimpleName();
MovieAdapter mAdapter;
List<Movie> mMovies = new ArrayList<Movie>();
private NowPlaying mNowPlaying;
@BindView(android.R.id.list)
ListView mListView;
@BindView(android.R.id.empty)
TextView mEmptyView;
private MovieApi mMovieApi;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ButterKnife.bind(this);
// final MovieAdapter adapter = new MovieAdapter(this, mMovies);
//
mMovieApi = RetrofitUtils.get(getString(R.string.api_key)).create(MovieApi.class);
mMovieApi.getNowPlaying().enqueue(new Callback<NowPlaying>() {
@Override
public void onResponse(Call<NowPlaying> call, Response<NowPlaying> response) {
Log.d("Response", String.valueOf(response.isSuccessful()));
mMovies = response.body().getMovies();
mAdapter = new MovieAdapter(this, mMovies);
mListView.setAdapter(mAdapter);
mListView.setEmptyView(mEmptyView);
}
@Override
public void onFailure(Call<NowPlaying> call, Throwable t) {
Log.e("Error", t.getMessage());
Toast.makeText(MainActivity.this, "OK", Toast.LENGTH_SHORT).show();
}
});
}
}
Mein Stack Trace:
10-15 23:04:19.480 12537-12537/com.example.rubit1359.bigcornbox E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.rubit1359.bigcornbox, PID: 12363
java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.ListView.setAdapter(android.widget.ListAdapter)' on a null object reference
at com.example.rubit1359.bigcornbox.ui.MainActivity$1.onResponse(MainActivity.java:57)
at retrofit2.ExecutorCallAdapterFactory$ExecutorCallbackCall$1$1.run(ExecutorCallAdapterFactory.java:68)
at android.os.Handler.handleCallback(Handler.java:815)
at android.os.Handler.dispatchMessage(Handler.java:104)
at android.os.Looper.loop(Looper.java:194)
at android.app.ActivityThread.main(ActivityThread.java:5637)
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:960)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:755)
ich gedebuggt haben die App, um sicherzustellen, dass die mMovies Listview gefüllt sind
public class MovieAdapter extends BaseAdapter{
private Context mContext;
// Add mVar of resource, an array for example
private List<Movie> mMovies;
// Automatically create constructor for all mVar
public MovieAdapter(Context context, List<Movie> movies) {
mContext = context;
mMovies = movies;
}
public MovieAdapter(Callback<NowPlaying> callback, List<Movie> movies) {
}
// Automatically create required methods and override them with mVar
@Override
public int getCount() {
return mMovies.size();
}
@Override
public Object getItem(int position) {
return mMovies.get(position);
}
@Override
public long getItemId(int position) {
return 0; // Use to easier get position reference
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
if (convertView == null) {
// If the ListView is brand new
convertView = LayoutInflater.from(mContext).inflate(R.layout.list_movie_item,
parent, false);
// Get layout from the context and inflate it the daily_list_item.
// Use ViewHolder to create smooth scrolling list
holder = new ViewHolder(convertView);
// Set the tag for reuse the View
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
//Set content to holderVar from get
//Create the ModelClass object as an element in the array
// Remember even the Image need to be set (different from MainAct)
Movie movie = mMovies.get(position);
holder.mTvLeftTitle.setText(movie.getTitle());
holder.mTvLeftOverview.setText(movie.getOverview());
// holder.mTvLeftCast.setText(movie.getCast());
Glide.with(mContext)
.load(movie.getPosterPath())
.into(holder.mImgLeftPoster);
holder.mTvRightTitle.setText(movie.getTitle());
holder.mTvRightOverview.setText(movie.getOverview());
// holder.mTvLeftCast.setText(movie.getCast());
Glide.with(mContext)
.load(movie.getPosterPath())
.into(holder.mImgRightPoster);
return convertView;
}
// Create class ViewHolder with Widget as variable based on Model class
static class ViewHolder {
// Using ButterKnife to create and hook Widget (remember the Image, too)
@BindView(R.id.rlLeftLayout)
RelativeLayout mRlLeftLayout;
@BindView(R.id.tvLeftTitle)
TextView mTvLeftTitle;
@BindView(R.id.tvLeftOverview)
TextView mTvLeftOverview;
@BindView(R.id.tvLeftCast)
TextView mTvLeftCast;
@BindView(R.id.imgLeftPoster)
ImageView mImgLeftPoster;
@BindView(R.id.rlRightLayout)
RelativeLayout mRlRightLayout;
@BindView(R.id.tvRightTitle)
TextView mTvRightTitle;
@BindView(R.id.tvRightOverview)
TextView mTvRightOverview;
@BindView(R.id.tvRightCast)
TextView mTvRightCast;
@BindView(R.id.imgRightPoster)
ImageView mImgRightPoster;
ViewHolder(View view) {
ButterKnife.bind(this, view);
}
}
}
Diese meine Aktivität ist . Allerdings kann ich den Adapter nicht einstellen. Jeder kann mir helfen, den Fehler zu erkennen. Danke.
UPDATE XML
XML Aktivität
<?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/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/darkness"
tools:context="com.example.rubit1359.bigcornbox.ui.MainActivity">
<ListView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:id="@+id/ListView"
android:layout_alignParentStart="true"
android:layout_alignParentLeft="true"/>
<TextView
android:text="There is no data to display"
android:textStyle="bold"
android:textSize="20sp"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:textColor="@color/colorWhite"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/EmptyView"
/>
</RelativeLayout>
Adapter xml:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/darkness">
<RelativeLayout
android:id="@+id/rlLeftLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:background="@color/skypeGray">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignBottom="@+id/imgLeftScoreBackground"
android:layout_alignTop="@+id/imgLeftPoster"
android:layout_toEndOf="@+id/imgLeftScoreBackground"
android:layout_toRightOf="@+id/imgLeftScoreBackground"
android:gravity="center_vertical"
android:orientation="vertical"
android:padding="16dp">
<TextView
android:id="@+id/tvLeftTitle"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:lineSpacingExtra="5dp"
android:text="Captain America: Civil War"
android:textAlignment="viewEnd"
android:textColor="@color/redmilk"
android:textSize="15sp"
android:textStyle="bold"/>
<TextView
android:id="@+id/tvLeftOverview"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="15dp"
android:ellipsize="end"
android:lineSpacingExtra="5dp"
android:maxLines="5"
android:text="Following the events of Age of Ultron, the collective goverments of the world pass an act designed to regulate all superhuman activity. This polarizes opinion amongst the Avengers, causing two factions to side..."
android:textAlignment="viewEnd"
android:textColor="@color/colorWhite"
android:textSize="12sp"/>
<TextView
android:id="@+id/tvLeftCast"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:text="Chris Evans, Robert Downey Jr."
android:textAlignment="viewEnd"
android:textColor="@color/colorWhite"
android:textSize="12sp"
android:textStyle="bold|italic"/>
</LinearLayout>
<ImageView
android:id="@+id/imgLeftPoster"
android:layout_width="120dp"
android:layout_height="180dp"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
app:srcCompat="@drawable/poster"/>
<ImageView
android:id="@+id/imgLeftScoreBackground"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/imgLeftPoster"
android:layout_marginTop="3dp"
app:srcCompat="@drawable/background"/>
</RelativeLayout>
<RelativeLayout
android:id="@+id/rlRightLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:background="@color/skypeGray">
<ImageView
android:id="@+id/imgRightPoster"
android:layout_width="120dp"
android:layout_height="180dp"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
app:srcCompat="@drawable/poster"/>
<ImageView
android:id="@+id/imgRightScoreBackground"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:layout_below="@+id/imgRightPoster"
android:layout_marginTop="3dp"
app:srcCompat="@drawable/background_right"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignBottom="@+id/imgRightScoreBackground"
android:layout_alignTop="@+id/imgRightPoster"
android:layout_toLeftOf="@+id/imgRightScoreBackground"
android:layout_toStartOf="@+id/imgRightScoreBackground"
android:gravity="center_vertical"
android:orientation="vertical"
android:padding="16dp">
<TextView
android:id="@+id/tvRightTitle"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:lineSpacingExtra="5dp"
android:text="Captain America: Civil War"
android:textAlignment="viewStart"
android:textColor="@color/redmilk"
android:textSize="15sp"
android:textStyle="bold"/>
<TextView
android:id="@+id/tvRightOverview"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="15dp"
android:ellipsize="end"
android:lineSpacingExtra="5dp"
android:maxLines="5"
android:text="Following the events of Age of Ultron, the collective goverments of the world pass an act designed to regulate all superhuman activity. This polarizes opinion amongst the Avengers, causing two factions to side..."
android:textAlignment="viewStart"
android:textColor="@color/colorWhite"
android:textSize="12sp"/>
<TextView
android:id="@+id/tvRightCast"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:text="Chris Evans, Robert Downey Jr."
android:textAlignment="viewStart"
android:textColor="@color/colorWhite"
android:textSize="12sp"
android:textStyle="bold|italic"/>
</LinearLayout>
</RelativeLayout>
</RelativeLayout>
Ihre mListView null ist –
Ja, ich weiß, aber Ich weiß nicht, warum es Null ist –
Haben Sie die ID li st in activity_main? –