user3081157
user3081157

Reputation:

image.setImageDrawable crashes app

Can't figure out why, but I have no errors on eclipse, (simple classroom app - button creates new activity, sending in a textView and an imageView to the new activity) but as soon as I run my app and I press my button, the app crashes and I recieve this error on the LogCat

Logcat

02-12 17:13:26.297: E/AndroidRuntime(398): Caused by: java.lang.NullPointerException
02-12 17:13:26.297: E/AndroidRuntime(398):  at edu.colum.iam.SecondPage.onCreate(SecondPage.java:38)

When I take that line out (and any other line that would affect it) the app runs fine, and it sends my textView over. Here is the .java I have, and I'll post the xml just incase as well.

IntentsActivity.java

public class IntentsActivity extends Activity {
/** Called when the activity is first created. */

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    Button btn = (Button)this.findViewById(R.id.button1);
    btn.setOnClickListener(myListener);

}




// Create an anonymous implementation of OnClickListener
View.OnClickListener myListener = new View.OnClickListener() 
{ 

    public void onClick(View v) {


Intent myIntent = new Intent(IntentsActivity.this, SecondPage.class);
        String text = ((TextView)findViewById(R.id.textView1)).getText().toString();
        ImageView Selection = ((ImageView) findViewById(R.id.imageView1));
        myIntent.putExtra("Text", text);
        myIntent.putExtra("img", R.drawable.icon);
or an image)
        startActivity(myIntent);
//endclass

SecondPage.java

public class SecondPage extends Activity {

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.second);

    //get extras
    Intent intent = this.getIntent();
    Bundle b = intent.getExtras();
    String text = b.getString("Text");

    //show text
    TextView tv = (TextView) findViewById(R.id.textView1);
    tv.setText(text);

    //show image
    ImageView image = (ImageView) findViewById(R.id.imageView1);
    int resource = getIntent().getIntExtra("img", R.drawable.icon);
    image.setImageDrawable(getResources().getDrawable(resource));
    // ^ this is the line my logcat crashes as an error 

main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TextView  
android:layout_width="fill_parent" 
android:layout_height="wrap_content" 
android:text="@string/hello"
/>
<Button android:text="@string/Button1" android:id="@+id/button1"
android:layout_width="wrap_content" android:layout_height="wrap_content"></Button>

<TextView android:id="@+id/textView1" android:text="@string/FirstLayout"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:layout_width="wrap_content"></TextView>

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

</LinearLayout>

second.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:orientation="vertical"
  android:layout_width="match_parent"
  android:layout_height="match_parent">

<TextView android:layout_width="wrap_content" android:id="@+id/textView1"   
android:text="@string/SecondLayout"
android:textAppearance="?android:attr/textAppearanceLarge"
android:layout_height="wrap_content"></TextView>

<Button android:text="@string/Button2" android:id="@+id/button1"          
android:layout_width="wrap_content" android:layout_height="wrap_content"></Button>

</LinearLayout>

Upvotes: 0

Views: 711

Answers (2)

Eenvincible
Eenvincible

Reputation: 5626

You can have the same ImageViews in different layout files (I don't know why you would name them the same) and then access them from within your code as long as you have set the right layout inside onCreate or wherever you want to access those components. So, yes, just copy the ImageView to the second.xml and it should work.

Upvotes: 0

codeMagic
codeMagic

Reputation: 44571

According to the layout you posted, imageView1 is in your first xml not in second.xml which is the one you inflate in SecondPage.java so naturally it is null when you try to call a method on it.

You will need to add the ImageView to your second.xml.

Upvotes: 2

Related Questions