到目前为止,我在我的一个片段中使用了此代码:
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));
}
}
答案 0 :(得分:0)
当您将代码移至活动时,您忘记了findViewById()
找不到当前 可见的视图。 R.id.imageView1
位于您在Fragment中使用的布局中,而不是您的Activity。因此findViewById()
会返回null
...
只需在Activity中保存对Bitmap的引用,但只尝试在Fragment中显示它(当ImageView可见时。)
<强>加成强>
由于fotoSetmanaBitmap
不是私有的,您可以将其保存在DownloadImageTask中,如下所示:
protected void onPostExecute(Bitmap result) {
Log.d("debugging","c");
activity.fotoSetmanaBitmap = result; // Where activity is an instance of AndroidViewPagerActivity
}
然后在不使用Intents的情况下加载它:
cont= (AndroidViewPagerActivity) getActivity(); //Recupero el contexte per poder pasar-lo.
if(cont.fotoSetmanaBitmap != null) {
ImageView image = (ImageView) myFragmentView.findViewById(R.id.imageView1);
image.setImageBitmap(cont.fotoSetmanaBitmap);
}