Creating on an imagebutton that openes a new activity onclick crushes

89 Views Asked by At

I tried to do this: when I click on the imagebutton then it opened new activity (named : TakingPick.class). It crashed and I don't understand the logcat. what do I need to do to make it work?

public class MainActivity extends ActionBarActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

}

@Override
public boolean onCreateOptionsMenu(Menu menu) {

    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}

public void sendMessage(View view) {
    Intent intent = new Intent(this, TakingPick.class);
    startActivity(intent);

}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    return false;

}

}

xml: activity_main

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


      android:background="@drawable/bridgebg"
          android:layout_height="match_parent"
          android:orientation="vertical" >

<ImageView
    android:id="@+id/imageView1"
    android:layout_width="300dp"
    android:layout_height="300dp"
    android:layout_alignParentTop="true"
    android:layout_centerHorizontal="true"
    android:src="@drawable/bridgenew" />

<ImageButton
    android:id="@+id/imageButton1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:layout_centerHorizontal="true"
    android:onClick="sendMessage"
    android:layout_marginBottom="61dp"
    android:src="@drawable/loginfacebook" />

The LOGCAT:

    06-14 19:36:56.111: E/dalvikvm-heap(14770): Out of memory on a 354168112-byte allocation.
06-14 19:36:56.211: E/AndroidRuntime(14770): FATAL EXCEPTION: main
06-14 19:36:56.211: E/AndroidRuntime(14770): Process: idan.nir.debridge, PID: 14770
06-14 19:36:56.211: E/AndroidRuntime(14770): java.lang.OutOfMemoryError
06-14 19:36:56.211: E/AndroidRuntime(14770):    at android.graphics.BitmapFactory.nativeDecodeAsset(Native Method)
06-14 19:36:56.211: E/AndroidRuntime(14770):    at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:677)
06-14 19:36:56.211: E/AndroidRuntime(14770):    at android.graphics.BitmapFactory.decodeResourceStream(BitmapFactory.java:507)
06-14 19:36:56.211: E/AndroidRuntime(14770):    at android.graphics.drawable.Drawable.createFromResourceStream(Drawable.java:872)
06-14 19:36:56.211: E/AndroidRuntime(14770):    at android.content.res.Resources.loadDrawable(Resources.java:3056)
06-14 19:36:56.211: E/AndroidRuntime(14770):    at android.content.res.Resources.getDrawable(Resources.java:1613)
06-14 19:36:56.211: E/AndroidRuntime(14770):    at com.android.internal.widget.ActionBarView.setIcon(ActionBarView.java:1016)
06-14 19:36:56.211: E/AndroidRuntime(14770):    at com.android.internal.policy.impl.PhoneWindow.setDefaultIcon(PhoneWindow.java:1543)
06-14 19:36:56.211: E/AndroidRuntime(14770):    at android.app.Activity.initActionBar(Activity.java:2001)
06-14 19:36:56.211: E/AndroidRuntime(14770):    at android.app.Activity.setContentView(Activity.java:2016)
06-14 19:36:56.211: E/AndroidRuntime(14770):    at idan.nir.debridge.TakingPick.onCreate(TakingPick.java:14)
06-14 19:36:56.211: E/AndroidRuntime(14770):    at android.app.Activity.performCreate(Activity.java:5431)
06-14 19:36:56.211: E/AndroidRuntime(14770):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1105)
06-14 19:36:56.211: E/AndroidRuntime(14770):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2269)
06-14 19:36:56.211: E/AndroidRuntime(14770):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2363)
06-14 19:36:56.211: E/AndroidRuntime(14770):    at android.app.ActivityThread.access$900(ActivityThread.java:161)
06-14 19:36:56.211: E/AndroidRuntime(14770):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1265)
06-14 19:36:56.211: E/AndroidRuntime(14770):    at android.os.Handler.dispatchMessage(Handler.java:102)
06-14 19:36:56.211: E/AndroidRuntime(14770):    at android.os.Looper.loop(Looper.java:157)
06-14 19:36:56.211: E/AndroidRuntime(14770):    at android.app.ActivityThread.main(ActivityThread.java:5356)
06-14 19:36:56.211: E/AndroidRuntime(14770):    at java.lang.reflect.Method.invokeNative(Native Method)
06-14 19:36:56.211: E/AndroidRuntime(14770):    at java.lang.reflect.Method.invoke(Method.java:515)
06-14 19:36:56.211: E/AndroidRuntime(14770):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1265)
06-14 19:36:56.211: E/AndroidRuntime(14770):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1081)
06-14 19:36:56.211: E/AndroidRuntime(14770):    at dalvik.system.NativeStart.main(Native Method)

TakingPick

public class TakingPick extends Activity {


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    // TODO Auto-generated method stub
    setContentView(R.layout.takingpick);

}


@Override
public boolean onCreateOptionsMenu(Menu menu) {

    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    return false;

}
}

xml:takingPick

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:background="@drawable/bridgebg"
    android:layout_height="match_parent" >


</RelativeLayout>
1

There are 1 best solutions below

2
On

You got an out of memory exception. One of the bitmaps you are trying to load is to big and causing a memory leak. If you are using a drawable resource, make sure it's in the correct drawable directory. Also try to reduce the image size and resolution