Reputation: 315
I have looked all around the internet but cant find an answer because i keep on getting errors from them. Im trying to make a picture from a url to show up on the image view, any help please? thank you very much :D
Here is my code for my fragment
package com.TripleC.twenty20;
import com.TripleC.twenty20.R;
import com.TripleC.twenty20.R.layout;
import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
public class PagesFragment extends Fragment {
public PagesFragment(){}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_pages, container, false);
return rootView;
}
}
I try this
try {
ImageView i = (ImageView)findViewById(R.id.image);
Bitmap bitmap = BitmapFactory.decodeStream((InputStream)new URL(imageUrl).getContent());
i.setImageBitmap(bitmap);
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
But it tells me i cant use the findviewbyid "The method findViewById(int) is undefined for the type PagesFragment"
This is the new error that comes up when i click the fragment, Logcat:
12-27 03:13:32.138: E/AndroidRuntime(1989): FATAL EXCEPTION: main
12-27 03:13:32.138: E/AndroidRuntime(1989): android.os.NetworkOnMainThreadException
12-27 03:13:32.138: E/AndroidRuntime(1989): at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1118)
12-27 03:13:32.138: E/AndroidRuntime(1989): at java.net.InetAddress.lookupHostByName(InetAddress.java:385)
12-27 03:13:32.138: E/AndroidRuntime(1989): at java.net.InetAddress.getAllByNameImpl(InetAddress.java:236)
12-27 03:13:32.138: E/AndroidRuntime(1989): at java.net.InetAddress.getAllByName(InetAddress.java:214)
12-27 03:13:32.138: E/AndroidRuntime(1989): at libcore.net.http.HttpConnection.<init>(HttpConnection.java:70)
12-27 03:13:32.138: E/AndroidRuntime(1989): at libcore.net.http.HttpConnection.<init>(HttpConnection.java:50)
12-27 03:13:32.138: E/AndroidRuntime(1989): at libcore.net.http.HttpConnection$Address.connect(HttpConnection.java:340)
12-27 03:13:32.138: E/AndroidRuntime(1989): at libcore.net.http.HttpConnectionPool.get(HttpConnectionPool.java:87)
12-27 03:13:32.138: E/AndroidRuntime(1989): at libcore.net.http.HttpConnection.connect(HttpConnection.java:128)
12-27 03:13:32.138: E/AndroidRuntime(1989): at libcore.net.http.HttpEngine.openSocketConnection(HttpEngine.java:315)
12-27 03:13:32.138: E/AndroidRuntime(1989): at libcore.net.http.HttpEngine.connect(HttpEngine.java:310)
12-27 03:13:32.138: E/AndroidRuntime(1989): at libcore.net.http.HttpEngine.sendSocketRequest(HttpEngine.java:289)
12-27 03:13:32.138: E/AndroidRuntime(1989): at libcore.net.http.HttpEngine.sendRequest(HttpEngine.java:239)
12-27 03:13:32.138: E/AndroidRuntime(1989): at libcore.net.http.HttpURLConnectionImpl.connect(HttpURLConnectionImpl.java:80)
12-27 03:13:32.138: E/AndroidRuntime(1989): at java.net.URLConnection.getContent(URLConnection.java:190)
12-27 03:13:32.138: E/AndroidRuntime(1989): at java.net.URL.getContent(URL.java:447)
12-27 03:13:32.138: E/AndroidRuntime(1989): at com.TripleC.twenty20.PhotosFragment.onCreateView(PhotosFragment.java:30)
12-27 03:13:32.138: E/AndroidRuntime(1989): at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:829)
12-27 03:13:32.138: E/AndroidRuntime(1989): at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:1035)
12-27 03:13:32.138: E/AndroidRuntime(1989): at android.app.BackStackRecord.run(BackStackRecord.java:635)
12-27 03:13:32.138: E/AndroidRuntime(1989): at android.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1397)
12-27 03:13:32.138: E/AndroidRuntime(1989): at android.app.FragmentManagerImpl$1.run(FragmentManager.java:426)
12-27 03:13:32.138: E/AndroidRuntime(1989): at android.os.Handler.handleCallback(Handler.java:615)
12-27 03:13:32.138: E/AndroidRuntime(1989): at android.os.Handler.dispatchMessage(Handler.java:92)
12-27 03:13:32.138: E/AndroidRuntime(1989): at android.os.Looper.loop(Looper.java:137)
12-27 03:13:32.138: E/AndroidRuntime(1989): at android.app.ActivityThread.main(ActivityThread.java:4918)
12-27 03:13:32.138: E/AndroidRuntime(1989): at java.lang.reflect.Method.invokeNative(Native Method)
12-27 03:13:32.138: E/AndroidRuntime(1989): at java.lang.reflect.Method.invoke(Method.java:511)
12-27 03:13:32.138: E/AndroidRuntime(1989): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1004)
12-27 03:13:32.138: E/AndroidRuntime(1989): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:771)
12-27 03:13:32.138: E/AndroidRuntime(1989): at dalvik.system.NativeStart.main(Native Method)
Upvotes: 2
Views: 6373
Reputation: 173
try {
ImageView i = (ImageView)rootView.findViewById(R.id.image);
Bitmap bitmap = BitmapFactory.decodeStream((InputStream)new URL(imageUrl).getContent());
i.setImageBitmap(bitmap);
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
Upvotes: 0
Reputation: 1
write your code in block of onActivityCreated
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
//write your code here
}
Upvotes: -1
Reputation: 23638
If you access the views in your fragment then you need access it as view.findViewById
as i have done below:
Try out as below:
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_pages, container, false);
try {
ImageView i = (ImageView)rootView.findViewById(R.id.image);
Bitmap bitmap = BitmapFactory.decodeStream((InputStream)new URL(imageUrl).getContent());
i.setImageBitmap(bitmap);
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return rootView;
}
Upvotes: 5