Tal Kopel
Tal Kopel

Reputation: 38

nullpointerexcetion at findviewbyid

i'm trying to build a simple app that downloads a png file from my pc decodes it and sets it in an imageview in a new activity that start after a button press.

everything that has to do with the networking, and decoding of the file seems to work properly, but i seem to be getting a lot of errors when using "findviewbyid", (android studio) it suggests the correct id but when i run it, exceptions are thrown and the app crashes.

(When creating a new activity in android studio it creates a placeholder fragment. Should I write the code in the OncreateView of the fragment?)

activity xml:

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.app.show"
tools:ignore="MergeRootFrame" />

fragment xml:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin"
tools:context="com.example.app.show$PlaceholderFragment">

<ImageView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/fromaster"
    android:layout_centerVertical="true"
    android:layout_centerHorizontal="true" />
</RelativeLayout>

java code (onCreate):

public class show extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_show);
    ImageView v;
    v = (ImageView) findViewById(R.id.fromaster);
    byte[] raw;

    if (v == null){Log.d("at view","empty");}

    raw = getIntent().getByteArrayExtra("pic");

    Bitmap bitmap = BitmapFactory.decodeByteArray(raw,0,raw.length);
    if (bitmap == null){
        Toast.makeText(getApplicationContext(),"empty      bitmap",Toast.LENGTH_SHORT).show();
    }
    else{
        try{
            v.setImageBitmap(bitmap);}
        catch (Exception e){
            Log.d("at setting bitmap","error",e);
        }
    }


    if (savedInstanceState == null) {
        getFragmentManager().beginTransaction()
                .add(R.id.container, new PlaceholderFragment())
                .commit();
    }
}

logcat:

02-02 10:48:34.330  21950-21950/com.example.app E/FragmentManager﹕ No view found for id 0x7f080000 (com.example.app:id/container) for fragment PlaceholderFragment{41bd5c38 #0 id=0x7f080000}

EDIT: i have used the OncreateView of the fragment, still an exception when using findviewbyid:

java code:

 public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.fragment_show, container, false);

        ImageView v = (ImageView) getView().findViewById(R.id.fromaster);
        byte[] raw;

        if (v == null){Log.d("at view","empty");}

        raw = getArguments().getByteArray("pic");

        Bitmap bitmap = BitmapFactory.decodeByteArray(raw,0,raw.length);
        if (bitmap == null){
            Toast.makeText(getActivity().getApplicationContext(),"empty bitmap",Toast.LENGTH_SHORT).show();
        }
        else{
            try{
                v.setImageBitmap(bitmap);}
            catch (Exception e){
                Log.d("at setting bitmap","error",e);
            }
        }

        return rootView;
    }
}

logcat error:

02-02 11:29:24.335  25479-25479/com.example.app E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: com.example.app, PID: 25479
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.app/com.example.app.show}: java.lang.NullPointerException
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2215)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2265)
        at android.app.ActivityThread.access$800(ActivityThread.java:145)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1206)
        at android.os.Handler.dispatchMessage(Handler.java:102)
        at android.os.Looper.loop(Looper.java:136)
        at android.app.ActivityThread.main(ActivityThread.java:5081)
        at java.lang.reflect.Method.invokeNative(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:515)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:781)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
        at dalvik.system.NativeStart.main(Native Method)
 Caused by: java.lang.NullPointerException
        at com.example.app.show$PlaceholderFragment.onCreateView(show.java:69)
        at android.app.Fragment.performCreateView(Fragment.java:1700)
        at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:890)
        at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:1062)
        at android.app.BackStackRecord.run(BackStackRecord.java:684)
        at android.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1447)
        at android.app.Activity.performStart(Activity.java:5240)
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2178)

           

Upvotes: 0

Views: 911

Answers (1)

Raghunandan
Raghunandan

Reputation: 133560

activity_show.xml does not have ImageView

ImageView is in fragment.xml. findViewById looks for a view in the current inflated layout. So if you findViewById returns null then you referenced the wrong id for the view or you do not have the view in the xml that you inflate.

My guess is your activity has a framelayout which is a container to which you add or replace framents. The initialization of ImageView should go to onCreateView of Fragment and im your show activity you need to add the fragment to FrameLayout.

Edit:

View rootView = inflater.inflate(R.layout.fragment_show, container, false);
ImageView v = (ImageView) rootView.findViewById(R.id.fromaster);

Upvotes: 1

Related Questions