直到现在,我在我的一个片段中有这段代码:
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
this.myFragmentView = inflater.inflate(R.layout.foto_setmana, container, false); //Això conté els "edittext i altres"
cont=getActivity(); //Recupero el contexte per poder pasar-lo.
try {
new DownloadImageTask((ImageView) myFragmentView.findViewById(R.id.imageView1)).execute("http://192.168.1.36/testing/fotos/foto1.jpg");
} catch (Exception e){
Log.d("errors", "Wohoops. Crashed");
}
return myFragmentView;
}
但是由于在片段之间滑动,每次我回到那个片段时它都会不断地加载这个图像,我决定将代码移动到主要的 FragmentActivity。此外,我读到不要在片段中使用任何这样的代码是一个好习惯。
我试过这个:
public class AndroidViewPagerActivity extends FragmentActivity {
[...]
try {
Log.d("debugging","a");
new DownloadImageTask((ImageView) this.findViewById(R.id.imageView1)).execute("http://192.168.1.36/testing/fotos/foto1.jpg");
} catch (Exception e){
Log.d("errors", "Wohoops. Crashed");
}
}
但它正在崩溃。
这是日志:
01-09 21:37:11.264: E/AndroidRuntime(976): FATAL EXCEPTION: main
01-09 21:37:11.264: E/AndroidRuntime(976): java.lang.NullPointerException
01-09 21:37:11.264: E/AndroidRuntime(976): at com.exercise.AndroidViewPager.DownloadImageTask.onPostExecute(DownloadImageTask.java:34)
01-09 21:37:11.264: E/AndroidRuntime(976): at com.exercise.AndroidViewPager.DownloadImageTask.onPostExecute(DownloadImageTask.java:1)
01-09 21:37:11.264: E/AndroidRuntime(976): at android.os.AsyncTask.finish(AsyncTask.java:631)
01-09 21:37:11.264: E/AndroidRuntime(976): at android.os.AsyncTask.access$600(AsyncTask.java:177)
01-09 21:37:11.264: E/AndroidRuntime(976): at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:644)
01-09 21:37:11.264: E/AndroidRuntime(976): at android.os.Handler.dispatchMessage(Handler.java:99)
01-09 21:37:11.264: E/AndroidRuntime(976): at android.os.Looper.loop(Looper.java:137)
01-09 21:37:11.264: E/AndroidRuntime(976): at android.app.ActivityThread.main(ActivityThread.java:5039)
01-09 21:37:11.264: E/AndroidRuntime(976): at java.lang.reflect.Method.invokeNative(Native Method)
01-09 21:37:11.264: E/AndroidRuntime(976): at java.lang.reflect.Method.invoke(Method.java:511)
01-09 21:37:11.264: E/AndroidRuntime(976): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
01-09 21:37:11.264: E/AndroidRuntime(976): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
01-09 21:37:11.264: E/AndroidRuntime(976): at dalvik.system.NativeStart.main(Native Method)
正如你所看到的,当我从我的片段中调用它时,它在我之前使用的类中崩溃(并且它有效)。
这是类代码,如果你想检查它:
public class DownloadImageTask extends AsyncTask<String, Void, Bitmap> {
ImageView bmImage;
public DownloadImageTask(ImageView bmImage) {
this.bmImage = bmImage;
}
protected Bitmap doInBackground(String... urls) {
String urldisplay = urls[0];
Bitmap mIcon11 = null;
Log.d("debugging","b");
try {
InputStream in = new java.net.URL(urldisplay).openStream();
mIcon11 = BitmapFactory.decodeStream(in);
} catch (Exception e) {
Log.e("Error", e.getMessage());
e.printStackTrace();
}
return mIcon11;
}
protected void onPostExecute(Bitmap result) {
Log.d("debugging","c");
bmImage.setImageBitmap(result);
}
}
logcat 指的是最后一行,确切地说:
bmImage.setImageBitmap(result);
有小费吗?
谢谢。
编辑。这样我在片段中导航
public class AndroidViewPagerActivity extends FragmentActivity {
ViewPager mViewPager;
TabsAdapter mTabsAdapter;
ImageView imatgeSetmana;
Bitmap fotoSetmanaBitmap;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mViewPager = new ViewPager(this);
mViewPager.setId(R.id.pager);
setContentView(mViewPager);
final ActionBar bar = getActionBar();
bar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
bar.setDisplayOptions(0, ActionBar.DISPLAY_SHOW_HOME);
bar.hide();
mTabsAdapter = new TabsAdapter(this, mViewPager);
mTabsAdapter.addTab(bar.newTab().setText("Foto setmana"), MyFragmentA.class, null);
mTabsAdapter.addTab(bar.newTab().setText("Entries this week"), MyFragmentB.class, null);
mTabsAdapter.addTab(bar.newTab().setText("Nothing yet"), MyFragmentC.class, null);
try {
//Descarrego la foto
new DownloadImageTask(this.imatgeSetmana).execute("http://192.168.1.36/testing/fotos/foto1.jpg");
//Guardo el bitmap de la foto
this.fotoSetmanaBitmap=((BitmapDrawable)imatgeSetmana.getDrawable()).getBitmap();
} catch (Exception e){
Log.d("errors", "Wohoops. Crashed");
}
if (savedInstanceState != null) {
bar.setSelectedNavigationItem(savedInstanceState.getInt("tab", 0));
}
}