当在android中单击图像按钮时,如何使一个布局转到另一个布局?

时间:2016-03-26 04:50:59

标签: java android eclipse android-intent imagebutton

我有一个程序,当点击图像按钮时,它应该从一个布局转到另一个布局。这是我的MainActivity代码:

public class MainActivity extends Activity {
    private ImageButton abt;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        abt = (ImageButton) findViewById(R.id.about);

        abt.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(MainActivity.this, AboutActivity.class);
                 startActivity(intent);
            }
        });
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        int id = item.getItemId();
        if (id == R.id.action_settings) {
            return true;
        }
        return super.onOptionsItemSelected(item);
    }
}

这是我的AboutActivity:

   public class AboutActivity extends Activity{
        private ImageButton aboutbk;
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_about);
            aboutbk = (ImageButton) findViewById(R.id.aboutBack);

            aboutbk.setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View v) {
                    finish();
                }
            });
        }

        @Override
        public boolean onCreateOptionsMenu(Menu menu) {
            return super.onCreateOptionsMenu(menu);
        }

        @Override
        public boolean onOptionsItemSelected(MenuItem item) {
            return super.onOptionsItemSelected(item);
        }

我的activity_main xml布局文件:(我已经为我的实际程序完成了布局,但现在我只是使用了我的关于图像按钮)

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="@drawable/background"
    android:orientation="horizontal" >

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

        <ImageView
            android:id="@+id/imageView1"
            android:layout_width="192dp"
            android:layout_height="600dp"
            android:layout_marginLeft="20dp"
            android:src="@drawable/welcome" />
    </LinearLayout>

    <LinearLayout
        android:layout_width="288dp"
        android:layout_height="593dp"
        android:orientation="vertical"
        android:paddingLeft="25dp"
        android:paddingTop="15dp" >

        <ImageView
            android:id="@+id/imageView2"
            android:layout_width="140dp"
            android:layout_height="158dp"
            android:src="@drawable/view" />

        <ImageButton
            android:id="@+id/letter"
            android:layout_width="match_parent"
            android:layout_height="43dp"
            android:background="@android:color/transparent"
            android:src="@drawable/letter" />

        <ImageButton
            android:id="@+id/topic"
            android:layout_width="match_parent"
            android:layout_height="43dp"
            android:layout_marginTop="5dp"
            android:background="@android:color/transparent"
            android:src="@drawable/topic" />


        <ImageButton
            android:id="@+id/about"
            android:layout_width="match_parent"
            android:layout_height="60dp"
            android:layout_gravity="bottom"
            android:layout_marginTop="25dp"
            android:background="@android:color/transparent"
            android:paddingTop="10dp"
            android:src="@drawable/buttonabout" />

        <ImageView
            android:id="@+id/imageView3"
            android:layout_width="73dp"
            android:layout_height="78dp"
            android:layout_marginTop="20dp"
            android:src="@drawable/search" />

        <EditText
            android:id="@+id/wrdsearch"
            android:layout_width="200dp"
            android:layout_height="wrap_content"
            android:hint="search" />

        <ImageButton
            android:id="@+id/searchgo"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginLeft="40dp"
            android:layout_marginTop="15dp"
            android:background="@android:color/transparent"
            android:src="@drawable/go" />
    </LinearLayout>

</LinearLayout>

我的activity_about xml布局文件:

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

    <ImageButton
        android:id="@+id/aboutBack"
        android:layout_width="515dp"
        android:layout_height="630dp"
        android:background="@drawable/aboutbg"
        android:src="@drawable/about2" />

</LinearLayout>

我的logcat错误:

03-25 23:53:49.114: E/dalvikvm-heap(823): Out of memory on a 4096016-byte allocation.
03-25 23:53:49.444: E/AndroidRuntime(823): FATAL EXCEPTION: main
03-25 23:53:49.444: E/AndroidRuntime(823): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.javadictionary/com.example.javadictionary.AboutActivity}: android.view.InflateException: Binary XML file line #7: Error inflating class <unknown>
03-25 23:53:49.444: E/AndroidRuntime(823):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2211)
03-25 23:53:49.444: E/AndroidRuntime(823):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2261)
03-25 23:53:49.444: E/AndroidRuntime(823):  at android.app.ActivityThread.access$600(ActivityThread.java:141)
03-25 23:53:49.444: E/AndroidRuntime(823):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1256)
03-25 23:53:49.444: E/AndroidRuntime(823):  at android.os.Handler.dispatchMessage(Handler.java:99)
03-25 23:53:49.444: E/AndroidRuntime(823):  at android.os.Looper.loop(Looper.java:137)
03-25 23:53:49.444: E/AndroidRuntime(823):  at android.app.ActivityThread.main(ActivityThread.java:5103)
03-25 23:53:49.444: E/AndroidRuntime(823):  at java.lang.reflect.Method.invokeNative(Native Method)
03-25 23:53:49.444: E/AndroidRuntime(823):  at java.lang.reflect.Method.invoke(Method.java:525)
03-25 23:53:49.444: E/AndroidRuntime(823):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
03-25 23:53:49.444: E/AndroidRuntime(823):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
03-25 23:53:49.444: E/AndroidRuntime(823):  at dalvik.system.NativeStart.main(Native Method)
03-25 23:53:49.444: E/AndroidRuntime(823): Caused by: android.view.InflateException: Binary XML file line #7: Error inflating class <unknown>
03-25 23:53:49.444: E/AndroidRuntime(823):  at android.view.LayoutInflater.createView(LayoutInflater.java:620)
03-25 23:53:49.444: E/AndroidRuntime(823):  at com.android.internal.policy.impl.PhoneLayoutInflater.onCreateView(PhoneLayoutInflater.java:56)
03-25 23:53:49.444: E/AndroidRuntime(823):  at android.view.LayoutInflater.onCreateView(LayoutInflater.java:669)
03-25 23:53:49.444: E/AndroidRuntime(823):  at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:694)
03-25 23:53:49.444: E/AndroidRuntime(823):  at android.view.LayoutInflater.rInflate(LayoutInflater.java:755)
03-25 23:53:49.444: E/AndroidRuntime(823):  at android.view.LayoutInflater.inflate(LayoutInflater.java:492)
03-25 23:53:49.444: E/AndroidRuntime(823):  at android.view.LayoutInflater.inflate(LayoutInflater.java:397)
03-25 23:53:49.444: E/AndroidRuntime(823):  at android.view.LayoutInflater.inflate(LayoutInflater.java:353)
03-25 23:53:49.444: E/AndroidRuntime(823):  at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:267)
03-25 23:53:49.444: E/AndroidRuntime(823):  at android.app.Activity.setContentView(Activity.java:1895)
03-25 23:53:49.444: E/AndroidRuntime(823):  at com.example.javadictionary.AboutActivity.onCreate(AboutActivity.java:19)
03-25 23:53:49.444: E/AndroidRuntime(823):  at android.app.Activity.performCreate(Activity.java:5133)
03-25 23:53:49.444: E/AndroidRuntime(823):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
03-25 23:53:49.444: E/AndroidRuntime(823):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2175)
03-25 23:53:49.444: E/AndroidRuntime(823):  ... 11 more
03-25 23:53:49.444: E/AndroidRuntime(823): Caused by: java.lang.reflect.InvocationTargetException
03-25 23:53:49.444: E/AndroidRuntime(823):  at java.lang.reflect.Constructor.constructNative(Native Method)
03-25 23:53:49.444: E/AndroidRuntime(823):  at java.lang.reflect.Constructor.newInstance(Constructor.java:417)
03-25 23:53:49.444: E/AndroidRuntime(823):  at android.view.LayoutInflater.createView(LayoutInflater.java:594)
03-25 23:53:49.444: E/AndroidRuntime(823):  ... 24 more
03-25 23:53:49.444: E/AndroidRuntime(823): Caused by: java.lang.OutOfMemoryError
03-25 23:53:49.444: E/AndroidRuntime(823):  at android.graphics.BitmapFactory.nativeDecodeAsset(Native Method)
03-25 23:53:49.444: E/AndroidRuntime(823):  at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:503)
03-25 23:53:49.444: E/AndroidRuntime(823):  at android.graphics.BitmapFactory.decodeResourceStream(BitmapFactory.java:356)
03-25 23:53:49.444: E/AndroidRuntime(823):  at android.graphics.drawable.Drawable.createFromResourceStream(Drawable.java:800)
03-25 23:53:49.444: E/AndroidRuntime(823):  at android.content.res.Resources.loadDrawable(Resources.java:2110)
03-25 23:53:49.444: E/AndroidRuntime(823):  at android.content.res.TypedArray.getDrawable(TypedArray.java:601)
03-25 23:53:49.444: E/AndroidRuntime(823):  at android.view.View.<init>(View.java:3364)
03-25 23:53:49.444: E/AndroidRuntime(823):  at android.widget.ImageView.<init>(ImageView.java:121)
03-25 23:53:49.444: E/AndroidRuntime(823):  at android.widget.ImageButton.<init>(ImageButton.java:87)
03-25 23:53:49.444: E/AndroidRuntime(823):  at android.widget.ImageButton.<init>(ImageButton.java:83)
03-25 23:53:49.444: E/AndroidRuntime(823):  ... 27 more

这很简单,但不起作用?我已经在我的AndroidManifest上宣布了第二个活动,但它仍然不起作用?请帮帮我,我一直在谷歌搜索寻找解决方案,但我似乎找不到一个。

0 个答案:

没有答案