1

I am trying to set the background image of one activity from another activity

Activity 1 - is a horizontal scroll view where i am loading a few images for the user to choose using this method

//set an onclick listener for imageView
         imageView.setOnClickListener(new OnClickListener(){

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                Toast.makeText(getApplicationContext(), "Clicked - " + imageNames[i] , Toast.LENGTH_LONG).show();
                NewNote note = new NewNote();
                note.setMyBackground(images[i]);
                finish();
            }

         });

Here images[i] is definitely not null since ive used it previously to load the HorizontalScrollView

Activity 2 - NewNote - The Activity whose background I am trying to set using the following method

public void setMyBackground(Drawable d){
        FrameLayout fl = (FrameLayout)findViewById(R.id.FLNote);
        fl.setBackground(d);
    }

If i comment out the note.setMyBackground(images[i]); there is no null pointer exception

Other wise i get the following Logcat

01-06 03:58:34.150: E/AndroidRuntime(1430): FATAL EXCEPTION: main
01-06 03:58:34.150: E/AndroidRuntime(1430): Process: com.example.mynote, PID: 1430
01-06 03:58:34.150: E/AndroidRuntime(1430): java.lang.NullPointerException
01-06 03:58:34.150: E/AndroidRuntime(1430):     at android.app.Activity.findViewById(Activity.java:1883)
01-06 03:58:34.150: E/AndroidRuntime(1430):     at com.example.mynote.activities.NewNote.setMyBackground(NewNote.java:50)
01-06 03:58:34.150: E/AndroidRuntime(1430):     at com.example.mynote.activities.NoteBackGroundPicker$1.onClick(NoteBackGroundPicker.java:65)
01-06 03:58:34.150: E/AndroidRuntime(1430):     at android.view.View.performClick(View.java:4424)
01-06 03:58:34.150: E/AndroidRuntime(1430):     at android.view.View$PerformClick.run(View.java:18383)
01-06 03:58:34.150: E/AndroidRuntime(1430):     at android.os.Handler.handleCallback(Handler.java:733)
01-06 03:58:34.150: E/AndroidRuntime(1430):     at android.os.Handler.dispatchMessage(Handler.java:95)
01-06 03:58:34.150: E/AndroidRuntime(1430):     at android.os.Looper.loop(Looper.java:137)
01-06 03:58:34.150: E/AndroidRuntime(1430):     at android.app.ActivityThread.main(ActivityThread.java:4998)
01-06 03:58:34.150: E/AndroidRuntime(1430):     at java.lang.reflect.Method.invokeNative(Native Method)
01-06 03:58:34.150: E/AndroidRuntime(1430):     at java.lang.reflect.Method.invoke(Method.java:515)
01-06 03:58:34.150: E/AndroidRuntime(1430):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:777)
01-06 03:58:34.150: E/AndroidRuntime(1430):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:593)
01-06 03:58:34.150: E/AndroidRuntime(1430):     at dalvik.system.NativeStart.main(Native Method)

NewNote.java

package com.example.mynote.activities;

import android.app.Activity;
import android.content.Intent;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.widget.FrameLayout;
import android.widget.ImageView;

import com.example.mynote.R;

public class NewNote extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.note_layout);
        //getActionBar().setDisplayHomeAsUpEnabled(true);
        getActionBar().setHomeButtonEnabled(true);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.new_note_action_bar_items, menu);
        return super.onCreateOptionsMenu(menu);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item){
        switch(item.getItemId()){
        case R.id.action_done:
            finish();
            return true;
        case R.id.action_bgchooser:
            Log.d("ACTION","BackgroundChooser Activity");
            Intent i2 = new Intent(NewNote.this,NoteBackGroundPicker.class);
            startActivity(i2);
            return true;
        default:
            return super.onOptionsItemSelected(item);
        }
    }

    public void setMyBackground(Drawable d){
        FrameLayout fl = (FrameLayout)findViewById(R.id.FLNote);
        fl.setBackground(d);
    }

}

note_layout.xml

<FrameLayout 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:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:background="@drawable/diary"
    tools:context=".NewNote"
    android:id="@+id/FLNote"
     >

    <ImageView
        android:id="@+id/imageView1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="top"
        android:src="@drawable/icon" />

    <ScrollView
        android:id="@+id/scrollView1"
        android:layout_width="match_parent"
        android:layout_height="346dp"
        android:layout_gravity="bottom" >

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="vertical" >

         <EditText
             android:id="@+id/editText1"
             android:layout_width="match_parent"
             android:layout_height="290dp"
             android:ems="10" >

            </EditText>

         <Button
             android:id="@+id/button1"
             android:layout_width="wrap_content"
             android:layout_height="wrap_content"
             android:layout_gravity="center"
             android:text="Save" />

        </LinearLayout>



    </ScrollView>

</FrameLayout>

How can i get rid of the null pointer exception????

user27812
  • 13
  • 1
  • 6
  • 1
    fl is null. check the id in xml – Raghunandan Jan 06 '14 at 09:09
  • if `NewNote` is your new activity then you have it all wrong since you're not supposed to instantiate the activities from your code. Rather than that start it with an Intent and send a specific bundle key-value. – gunar Jan 06 '14 at 09:10
  • where you declared "images[i]" if it is on the first activity, then on finish() everything on the activity will not be existing any more, so the images[i] will be null. Better you create a Application class and define images[] there as static variable – Amitabh Sarkar Jan 06 '14 at 09:12

4 Answers4

0

NewNote is a Activity class.

And you have

  NewNote note = new NewNote();

Wrong.

Check the answer by Raghav Sood

Can i Create the object of a activity in other class?

You can use startActivityForResult. Use intent to pass the url or the resource id of the drawable and then set the background to FrameLayout (after initializing).

Community
  • 1
  • 1
Raghunandan
  • 132,755
  • 26
  • 225
  • 256
  • no the id isnt showing as null As an alternative i had tried changing the framelayout in Activity 1 itself that too gives me a null pointer exception – user27812 Jan 06 '14 at 09:13
  • @user27812 post the xml and post `NewNote.java` line 50 btw this is wrong `NewNote note = new NewNote(); note.setMyBackground(images[i]);` – Raghunandan Jan 06 '14 at 09:14
  • As i said earlier i had also tried the following code public void onClick(View v) { // TODO Auto-generated method stub Toast.makeText(getApplicationContext(), "Clicked - " + imageNames[i] , Toast.LENGTH_LONG).show(); FrameLayout fl = (FrameLayout)findViewById(R.id.FLNote); fl.setBackground(images[i]); //note.setMyBackground(images[i]); gotta set background correctly here finish(); } – user27812 Jan 06 '14 at 09:18
  • @user27812 What is line 50 Note Activity. You should never do this `NewNote note = new NewNote()` and what code did you try? – Raghunandan Jan 06 '14 at 09:19
  • i changed it as u had said to the above code still the same error – user27812 Jan 06 '14 at 09:20
  • @user27812 where did you try that in which activity class did you try those code and pls specify line 50 – Raghunandan Jan 06 '14 at 09:20
  • Line 50 was note.setMyBackground(images[i]); – user27812 Jan 06 '14 at 09:22
  • @user27812 as it earlier its bcoz of `NewNote note = new NewNote()` – Raghunandan Jan 06 '14 at 09:24
  • but in the new code i showed u in the above comment i havent done that ive directly changed the background of framelayout from Activity 1 without instantiating NewNote – user27812 Jan 06 '14 at 09:26
  • @user27812 that is also wrong. First `findViewById` looks for a view with the id in the current inflated layout. So even if you initialize framalyour of NewNote in Activity1 its wrong. Secondly how can you directly change the background of a view without initializing. that is also wrong. totally wrong – Raghunandan Jan 06 '14 at 09:28
0

As others have mentioned, you don't initiate activities your self in Android. Instead you call startActivity with an implicit Intent to start your new activity.

It seems like you should be passing the data from images[i] in the extras of this intent.

If images[i] is a bitmap it might be better to pass the source of that bitmap (the url it was loaded from, the resource id, etc) as the bitmap may be too big (or slow) to put into a bundle.

FunkTheMonk
  • 10,908
  • 1
  • 31
  • 37
0

If your NewNote activity is already opened previously, then you can use something like

NewNote.fl.setbackgroundResource(//your images);

declare you fl as global in Newnote activity.

or if you will create the NewNote activity from this activity then from that activity you can check a condition or whatever you need from the first activity same way.

or you can pass the images through intent if they are not too big.

but healthy and good coding will be if you create a utility or a helper class, keep these images static over there and call whenever you need from any activity you want.

Ari
  • 1,296
  • 1
  • 18
  • 36
0

declare public variable of your layout in NewNote Activity as

public static FrameLayout f1;

and in onCreate of NewNote Activity

fl = (FrameLayout)findViewById(R.id.FLNote);

to change its background from another activity ,say Activity A call as

NewNote.f1.setBackground(images[i]);

note that images[i] must be drawable image.