2

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)
Crazycriss
  • 315
  • 1
  • 5
  • 19

3 Answers3

5

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;


}   
GrIsHu
  • 29,068
  • 10
  • 64
  • 102
  • Thank you, but the app crashes when i click on that fragment – Crazycriss Dec 27 '13 at 10:51
  • @Crazycriss what it says?? – Piyush Dec 27 '13 at 10:52
  • Are you trying to perform some time consuming tasks in the main thread of the application in your `PhotosFragment.onCreateView(` method? – GrIsHu Dec 27 '13 at 11:28
  • Please don`t download content on the main Thread. Use a Handler with a Thread or AsyncTaks with a Callback Interface, or a third party library and so on. http://developer.android.com/reference/android/os/Handler.html – narancs Jun 11 '15 at 09:35
0
    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();
}
Simon Wanjau.
  • 173
  • 1
  • 10
-1

write your code in block of onActivityCreated

  @Override
        public void onActivityCreated(Bundle savedInstanceState) {
            super.onActivityCreated(savedInstanceState);
           //write your code here
        }