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????