只有在更改设备方向时,应用才会崩溃。 这是主要活动:
public class MainActivity extends AppCompatActivity implements Listener {
boolean mTwoPane;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
if (findViewById(R.id.fragment_two) != null) {
mTwoPane = true;
} else {
mTwoPane = false;
}
if (null == savedInstanceState) {
MoviesFragment moviesFragment = new MoviesFragment();
moviesFragment.setListener(this);
getSupportFragmentManager().beginTransaction().add(R.id.fragment_movies, moviesFragment).commit();
}
}
@Override
public void setSelectedMovie(MoviesData.ResultsBean movie) {
if (mTwoPane == true) {
DetailFragment detailsFragment = new DetailFragment();
Bundle extras = new Bundle();
extras.putSerializable("MoviesData", movie);
detailsFragment.setArguments(extras);
getSupportFragmentManager().beginTransaction().replace(R.id.fragment_two, detailsFragment).commit();
} else {
Intent intent = new Intent(this, DetailActivity.class);
intent.putExtra("MoviesData", movie);
startActivity(intent);
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.sort_menu, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
if (id == R.id.action_settings) {
startActivity(new Intent(this, SettingsActivity.class));
return true;
}
return super.onOptionsItemSelected(item);
}
}
主要片段代码:
public class MoviesFragment extends Fragment {
ArrayList<MoviesData.ResultsBean> moviesList;
private ImagesAdapter moviesAdapter;
private ArrayList<String> mImageUrlList;
Listener mListener;
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_movies, container, false);
mImageUrlList = new ArrayList<String>();
moviesAdapter = new ImagesAdapter(getActivity(), mImageUrlList);
GridView list = (GridView) rootView.findViewById(R.id.gridview_movies);
list.setAdapter(moviesAdapter);
list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int position, long l) {
Toast.makeText(getActivity(), "title: " + moviesList.get(position).getOriginal_title(), Toast.LENGTH_SHORT).show();
mListener.setSelectedMovie(moviesList.get(position));
}
});
return rootView;
}
@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setHasOptionsMenu(true);
}
public void updateMoviesDisplay() {
FetchMoviesTask fetchMoviesTask = new FetchMoviesTask();
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(getActivity());
String type = prefs.getString(getString(R.string.pref_display_type_key),
getString(R.string.pref_display_categeory_defualt));
String pop = "popular";
//String displayType="popular?";
Log.i("type00", type);
System.out.println("type= " + type);
if (type.equals("favourite")) {
System.out.println("type= " + type);
mImageUrlList.clear();
MovieDb db = new MovieDb(getActivity());
moviesList = db.getAllMovie();
List<MoviesData.ResultsBean> mResultsBean;
mResultsBean = db.getAllMovie();
for (int i = 0; i < mResultsBean.size(); i++) {
mImageUrlList.add(mResultsBean.get(i).getPoster_path());
}
moviesAdapter.notifyDataSetChanged();
} else if (type.equals("popular")) {
fetchMoviesTask.execute("popular");
} else if (type.equals("top_rated")) {
fetchMoviesTask.execute("top_rated");
}
Log.i("kkkk", String.valueOf(type.equals(pop)));
}
public boolean onOptionsItemSelected(MenuItem item) {
/* FetchMoviesTask task = new FetchMoviesTask();
if (id == R.id.popular) {
task.execute("popular");
return true;
} else if (id == R.id.top_rated) {
task.execute("top_rated");
return true;
} else if (id == R.id.favourite_sort) {
mImageUrlList.clear();
MovieDb db = new MovieDb(getActivity());
moviesList = db.getAllMovie();
List<MoviesData.ResultsBean> mResultsBean;
mResultsBean = db.getAllMovie();
for (int i = 0; i < mResultsBean.size(); i++) {
mImageUrlList.add(mResultsBean.get(i).getPoster_path());
}
moviesAdapter.notifyDataSetChanged();
}
moviesAdapter.notifyDataSetChanged();*/
return true;
}
public void setListener(Listener listener) {
this.mListener = listener;
}
public static String BuildImageUrl(String imagePath) {
Uri uri = Uri.parse("http://image.tmdb.org/t/p/").buildUpon().appendEncodedPath("w342").appendEncodedPath(imagePath).build();
return uri.toString();
}
public class FetchMoviesTask extends AsyncTask<String, Void, MoviesData> {
@Override
protected MoviesData doInBackground(String... params) {
try {
return new FetchMovies().fetchMovieData(params[0], 1 + "");
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
@Override
protected void onPostExecute(MoviesData movie) {
super.onPostExecute(movie);
if (movie == null) {
Toast toast = Toast.makeText(getActivity(), "No internet connection", Toast.LENGTH_LONG);
toast.show();
return;
}
moviesList = movie.getResults();
mImageUrlList.clear();
for (ResultsBean bean : movie.getResults()) {
mImageUrlList.add(BuildImageUrl(bean.getPoster_path()));
}
moviesAdapter.notifyDataSetChanged();
}
}
@Override
public void onStart() {
super.onStart();
updateMoviesDisplay();
}
}
我得到了一个nullpointerexception:
尝试调用接口方法&#39; void
com.example.mohamed.movieapp.Listener.setSelectedMovie(com.example.mohamed.movieapp.Data.MoviesData $ ResultsBean)&#39; 在null对象引用上 在com.example.mohamed.movieapp.MoviesFragment $ 1.onItem点击此处 排在&#34; MoviesFragment&#34; mListener.setSelectedMovie(moviesList.get(位置));在里面 setOnItemClickListener方法
答案 0 :(得分:0)
旋转设备时,savedInstanceState为非null。在您的活动中,如果savedInstanceState为null,则仅设置侦听器。因此,在设备旋转的情况下,片段中的mListener对象为NULL。因此,在其上调用任何东西都会产生NPI。
如果savedInstanceState为非null,则需要使用SupportFragmentManager查找片段或ID,例如在任何配置更改的情况下,例如设备旋转,并且在这种情况下将Listener设置为您找到的片段对象