在打开新活动的图像按钮上创建onclick粉碎

时间:2015-06-14 16:42:10

标签: android-activity onclick crash

我试着这样做: 当我点击图像按钮然后它打开了新的活动(名为:TakingPick.class)。 它崩溃了,我不理解logcat。 我需要做些什么来使它工作?

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" />

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 个答案:

答案 0 :(得分:2)

你有一个内存不足的例外。您尝试加载的位图之一是大而导致内存泄漏。 如果您使用的是drawable资源,请确保它位于正确的drawable目录中。还尝试减小图像大小和分辨率