onclicklistener函数onclick崩溃

时间:2014-04-11 19:02:11

标签: java android xml android-layout

由于某些原因,当我点击Button时,我的应用程序崩溃了。我搜索了 在互联网上,一切似乎都很好。 我很感激你的建议,帮助我找出错误的原因。 这是我点击Button的第一个Java。 我想知道是什么问题。谢谢

 package nir.rauc.mapstutori;
    import android.app.Activity;
    import android.content.Intent;
    import android.os.Bundle;
    import android.view.View;
    import android.view.View.OnClickListener;
    import android.widget.Button;

    public class Details extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_details);
    final   Button button1 = (Button) findViewById(R.id.button1);
        button1.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                Intent intent = new Intent(Details.this, Pictures.class);
                startActivity(intent);
            }

        });
    }
    }

acitivty_details.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="#FF00FF00" >

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:layout_marginLeft="38dp"
        android:layout_marginTop="60dp"
        android:text="price : 2.4 M "
        android:textSize="40sp" />

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_alignRight="@+id/textView1"
        android:layout_marginBottom="31dp"
        android:background="#4004F0"
        android:text="Click to Check for pictures" />

    <TextView
        android:id="@+id/textView2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_below="@+id/textView1"
        android:layout_marginTop="44dp"
        android:text="Street: Biking botoom"
        android:textSize="40sp" />

</RelativeLayout>

The Second Jave FIle:

  package nir.rauc.mapstutori;

    import android.app.Activity;
    import android.os.Bundle;

    public class Pictures extends Activity {

        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.picturescroll);
        }
    }

picturescroll.xml

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
      android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:orientation="vertical" >
    <HorizontalScrollView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:fillViewport="true"
        android:scrollbars="horizontal|vertical" >

        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:orientation="horizontal" >

            <ImageView
                android:id="@+id/viewImage1"
                android:layout_width="125dp"
                android:layout_height="125dp"
                android:padding="15dp"
                android:src="@drawable/first" />

            <ImageView
                android:id="@+id/viewImage2"
                android:layout_width="125dp"
                android:layout_height="125dp"
                android:padding="15dp"
                android:src="@drawable/second" />

            <ImageView
                android:id="@+id/viewImage3"
                android:layout_width="125dp"
                android:layout_height="125dp"
                android:padding="15dp"
                android:src="@drawable/third" />


        </LinearLayout>
    </HorizontalScrollView>

    </LinearLayout>

EDITED:MyManifest

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="nir.rauc.mapstutori"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-feature
        android:glEsVersion="0x00020000"
        android:required="true" />

    <uses-sdk
        android:minSdkVersion="11"
        android:targetSdkVersion="18" />

    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
    <uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES" />
    <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <meta-data
            android:name="com.google.android.gms.version"
            android:value="@integer/google_play_services_version" />

        <activity
            android:name="nir.rauc.mapstutori.MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

        <meta-data
            android:name="com.google.android.maps.v2.API_KEY"
            android:value="CANTWRITHEITONSTACKOVERFLOW" />

        <activity
            android:name="nir.rauc.mapstutori.Details"
            android:label="@string/title_activity_details" >
        </activity>
        <activity
            android:name="nir.rauc.mapstutori.Pictures"
            android:label="something" >
        </activity>
    </application>

</manifest>

EDITED:Logtag

04-11 22:16:05.631: E/dalvikvm-heap(12095): Out of memory on a 53934352-byte allocation.
04-11 22:16:05.656: E/AndroidRuntime(12095): FATAL EXCEPTION: main
04-11 22:16:05.656: E/AndroidRuntime(12095): java.lang.RuntimeException: Unable to start activity ComponentInfo{nir.rauc.mapstutori/nir.rauc.mapstutori.Pictures}: android.view.InflateException: Binary XML file line #33: Error inflating class <unknown>
04-11 22:16:05.656: E/AndroidRuntime(12095):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2295)
04-11 22:16:05.656: E/AndroidRuntime(12095):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2349)
04-11 22:16:05.656: E/AndroidRuntime(12095):    at android.app.ActivityThread.access$700(ActivityThread.java:159)
04-11 22:16:05.656: E/AndroidRuntime(12095):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1316)
04-11 22:16:05.656: E/AndroidRuntime(12095):    at android.os.Handler.dispatchMessage(Handler.java:99)
04-11 22:16:05.656: E/AndroidRuntime(12095):    at android.os.Looper.loop(Looper.java:137)
04-11 22:16:05.656: E/AndroidRuntime(12095):    at android.app.ActivityThread.main(ActivityThread.java:5419)
04-11 22:16:05.656: E/AndroidRuntime(12095):    at java.lang.reflect.Method.invokeNative(Native Method)
04-11 22:16:05.656: E/AndroidRuntime(12095):    at java.lang.reflect.Method.invoke(Method.java:525)
04-11 22:16:05.656: E/AndroidRuntime(12095):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1187)
04-11 22:16:05.656: E/AndroidRuntime(12095):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1003)
04-11 22:16:05.656: E/AndroidRuntime(12095):    at dalvik.system.NativeStart.main(Native Method)
04-11 22:16:05.656: E/AndroidRuntime(12095): Caused by: android.view.InflateException: Binary XML file line #33: Error inflating class <unknown>
04-11 22:16:05.656: E/AndroidRuntime(12095):    at android.view.LayoutInflater.createView(LayoutInflater.java:626)
04-11 22:16:05.656: E/AndroidRuntime(12095):    at com.android.internal.policy.impl.PhoneLayoutInflater.onCreateView(PhoneLayoutInflater.java:56)
04-11 22:16:05.656: E/AndroidRuntime(12095):    at android.view.LayoutInflater.onCreateView(LayoutInflater.java:675)
04-11 22:16:05.656: E/AndroidRuntime(12095):    at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:700)
04-11 22:16:05.656: E/AndroidRuntime(12095):    at android.view.LayoutInflater.rInflate(LayoutInflater.java:761)
04-11 22:16:05.656: E/AndroidRuntime(12095):    at android.view.LayoutInflater.rInflate(LayoutInflater.java:769)
04-11 22:16:05.656: E/AndroidRuntime(12095):    at android.view.LayoutInflater.rInflate(LayoutInflater.java:769)
04-11 22:16:05.656: E/AndroidRuntime(12095):    at android.view.LayoutInflater.inflate(LayoutInflater.java:498)
04-11 22:16:05.656: E/AndroidRuntime(12095):    at android.view.LayoutInflater.inflate(LayoutInflater.java:398)
04-11 22:16:05.656: E/AndroidRuntime(12095):    at android.view.LayoutInflater.inflate(LayoutInflater.java:354)
04-11 22:16:05.656: E/AndroidRuntime(12095):    at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:361)
04-11 22:16:05.656: E/AndroidRuntime(12095):    at android.app.Activity.setContentView(Activity.java:1956)
04-11 22:16:05.656: E/AndroidRuntime(12095):    at nir.rauc.mapstutori.Pictures.onCreate(Pictures.java:11)
04-11 22:16:05.656: E/AndroidRuntime(12095):    at android.app.Activity.performCreate(Activity.java:5372)
04-11 22:16:05.656: E/AndroidRuntime(12095):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1104)
04-11 22:16:05.656: E/AndroidRuntime(12095):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2257)
04-11 22:16:05.656: E/AndroidRuntime(12095):    ... 11 more
04-11 22:16:05.656: E/AndroidRuntime(12095): Caused by: java.lang.reflect.InvocationTargetException
04-11 22:16:05.656: E/AndroidRuntime(12095):    at java.lang.reflect.Constructor.constructNative(Native Method)
04-11 22:16:05.656: E/AndroidRuntime(12095):    at java.lang.reflect.Constructor.newInstance(Constructor.java:417)
04-11 22:16:05.656: E/AndroidRuntime(12095):    at android.view.LayoutInflater.createView(LayoutInflater.java:600)
04-11 22:16:05.656: E/AndroidRuntime(12095):    ... 26 more
04-11 22:16:05.656: E/AndroidRuntime(12095): Caused by: java.lang.OutOfMemoryError
04-11 22:16:05.656: E/AndroidRuntime(12095):    at android.graphics.BitmapFactory.nativeDecodeAsset(Native Method)
04-11 22:16:05.656: E/AndroidRuntime(12095):    at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:596)
04-11 22:16:05.656: E/AndroidRuntime(12095):    at android.graphics.BitmapFactory.decodeResourceStream(BitmapFactory.java:444)
04-11 22:16:05.656: E/AndroidRuntime(12095):    at android.graphics.drawable.Drawable.createFromResourceStream(Drawable.java:832)
04-11 22:16:05.656: E/AndroidRuntime(12095):    at android.content.res.Resources.loadDrawable(Resources.java:2988)
04-11 22:16:05.656: E/AndroidRuntime(12095):    at android.content.res.TypedArray.getDrawable(TypedArray.java:602)
04-11 22:16:05.656: E/AndroidRuntime(12095):    at android.widget.ImageView.<init>(ImageView.java:131)
04-11 22:16:05.656: E/AndroidRuntime(12095):    at android.widget.ImageView.<init>(ImageView.java:121)
04-11 22:16:05.656: E/AndroidRuntime(12095):    ... 29 more

1 个答案:

答案 0 :(得分:0)

问题在于加载位图图像,所以 尝试添加:

android:largeHeap="true"

到清单文件中的应用程序标记:

  <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:largeHeap="true"
        android:theme="@style/AppTheme" >