import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.LoaderManager.LoaderCallbacks;
import android.support.v4.content.CursorLoader;
import android.support.v4.content.Loader;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.ShareActionProvider;
import com.squareup.picasso.Picasso;
import com.themovieapp.tonynguyen.themovieapp.data.MovieContract;
public class DetailActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_detail);
if (savedInstanceState == null) {
getSupportFragmentManager().beginTransaction()
.add(R.id.container, new DetailFragment())
.commit();
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.detail, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
startActivity(new Intent(this, SettingsActivity.class));
return true;
}
return super.onOptionsItemSelected(item);
}
public static class DetailFragment extends Fragment implements LoaderCallbacks<Cursor> {
private final String LOG_TAG = DetailFragment.class.getSimpleName();
private String mMovie;
ImageView poster;
private static final int DETAIL_LOADER = 0;
private final String[] DETAIL_COLUMNS = {
MovieContract.MovieEntry.TABLE_NAME + "." + MovieContract.MovieEntry._ID,
MovieContract.MovieEntry.COLUMN_MOVIE_POSTER
};
private static final int COL_MOVIE_POSTER = 1;
public DetailFragment() {
setHasOptionsMenu(true);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
return inflater.inflate(R.layout.fragment_detail, container, false);
}
@Override
public void onActivityCreated(Bundle savedInstanceState) {
getLoaderManager().initLoader(DETAIL_LOADER, null, this);
super.onActivityCreated(savedInstanceState);
}
@Override
public Loader<Cursor> onCreateLoader(int id, Bundle args) {
Log.v(LOG_TAG, "In onCreateLoader");
Intent intent = getActivity().getIntent();
if (intent == null) {
return null;
}
return new CursorLoader(
getActivity(),
intent.getData(),
DETAIL_COLUMNS,
null,
null,
null
);
}
@Override
public void onLoadFinished(Loader<Cursor> loader, Cursor data) {
Log.v(LOG_TAG, "In onLoadFinished");
if (!data.moveToFirst()) {
return;
}
String posterURL = data.getString(COL_MOVIE_POSTER);
final String POSTERIMAGE_BASE_URL = "http://image.tmdb.org/t/p/";
final String POSTERIMAGE_SIZE = "w500";
poster = (ImageView) getView().findViewById(R.id.poster_image_view);
final String POSTERIMAGE_URL = POSTERIMAGE_BASE_URL + POSTERIMAGE_SIZE + posterURL;
Picasso.with(this.getActivity()).load(POSTERIMAGE_URL).into(poster);
Log.v(LOG_TAG, "Poster Urls: " + POSTERIMAGE_URL);
}
@Override
public void onLoaderReset(Loader<Cursor> loader) {
}
}
}
我的应用在Gridlayout中加载电影海报的图像。当我点击海报时,它将加载DetailActivity并显示我点击的海报。然而,当我运行我的应用程序并单击它崩溃的海报时。我认为原因是我毕加索的背景:
Picasso.with(this.getActivity()).load(POSTERIMAGE_URL).into(poster);
08-30 00:33:05.498 10812-10812 / com.themovieapp.tonynguyen.themovieapp E / AndroidRuntime:FATAL EXCEPTION:main
过程:com.themovieapp.tonynguyen.themovieapp,PID:10812 java.lang.IllegalArgumentException:Target不能为null。 在com.squareup.picasso.RequestCreator.into(RequestCreator.java:618) 在com.squareup.picasso.RequestCreator.into(RequestCreator.java:601) 在 com.themovieapp.tonynguyen.themovieapp.DetailActivity $ DetailFragment.onLoadFinished(DetailActivity.java:154) 在 com.themovieapp.tonynguyen.themovieapp.DetailActivity $ DetailFragment.onLoadFinished(DetailActivity.java:61) 在 android.support.v4.app.LoaderManagerImpl $ LoaderInfo.callOnLoadFinished(LoaderManager.java:476) 在 android.support.v4.app.LoaderManagerImpl $ LoaderInfo.onLoadComplete(LoaderManager.java:444) 在android.support.v4.content.Loader.deliverResult(Loader.java:126) 在 android.support.v4.content.CursorLoader.deliverResult(CursorLoader.java:105) 在 android.support.v4.content.CursorLoader.deliverResult(CursorLoader.java:37) 在 android.support.v4.content.AsyncTaskLoader.dispatchOnLoadComplete(AsyncTaskLoader.java:249) 在 android.support.v4.content.AsyncTaskLoader $ LoadTask.onPostExecute(AsyncTaskLoader.java:77) 在 android.support.v4.content.ModernAsyncTask.finish(ModernAsyncTask.java:466) 在 android.support.v4.content.ModernAsyncTask.access $ 400(ModernAsyncTask.java:48) 在 android.support.v4.content.ModernAsyncTask $ InternalHandler.handleMessage(ModernAsyncTask.java:483) 在android.os.Handler.dispatchMessage(Handler.java:102) 在android.os.Looper.loop(Looper.java:158) 在android.app.ActivityThread.main(ActivityThread.java:7237) at java.lang.reflect.Method.invoke(Native Method) 在 com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:1230) 在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120)
答案 0 :(得分:0)
这一行有问题:
poster = (ImageView) getView().findViewById(R.id.poster_image_view);
可能您的观点在R.id.poster_image_view
布局中没有ImageView
R.layout.fragment_detail
错误表示接受图片的目标是null
(into(poster)
)