I want to make my ImageView as an image from an url, the apk successfully build but when i try to launch the imageView, the apps get crash. So how to fix my problem?
This is my java code:
public class tab1 extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
String imageUrl = "http://www.travelstar1.com/wp-content/uploads/2015/03/Nice-view-of-the-Earth-from-space-51.jpg";
View view = inflater.inflate(R.layout.tab1, container, false);
try {
ImageView i = (ImageView)view.findViewById(R.id.iklanpertama);
Bitmap bitmap = BitmapFactory.decodeStream((InputStream)new URL(imageUrl).getContent());
i.setImageBitmap(bitmap);
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return view;
}
}
and this is my layout code(xml), how to fix it?
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:weightSum="2"
android:orientation="vertical"
>
<ImageView
android:id="@+id/iklanpertama"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="0.8"
/>
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#fff3e0"
android:layout_weight="1.2"
android:textColor="#212326"
/>