如何在屏幕上放置静态ImageView

时间:2013-05-19 09:45:34

标签: android android-imageview

我最近在学习Android Development这很难。我试图将图像放在屏幕中间时遇到问题。到目前为止,我有两个图像按钮和一个文本。我在布局中的.xml文件中添加了图像,但是当我尝试在设备上运行它时应用程序关闭。所以,我认为它与我的代码有关,我不知道出了什么问题。

这是我的代码。一些帮助会很棒!谢谢!

public class AppActivity extends Activity implements OnTouchListener {

    private MediaPlayer mp;
    ImageButton button;
    ImageButton button3;
    ImageView image1;
    ImageView image;

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

        addListenerOnButton();
    }

    public void addListenerOnButton() {

        final Context context = this;
        button3  = (ImageButton) this.findViewById(R.id.imageButton5);
        button3.setOnTouchListener(this);

        mp = MediaPlayer.create(this, R.raw.vanilla_twilight);

        image = (ImageView) findViewById(R.id.imageView1);

        button = (ImageButton) findViewById(R.id.imageButton1);
        button.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View arg0) {

                Intent intent = new Intent(context, App2Activity.class);
                            startActivity(intent);   
            }
        });
    }

    @Override
    public boolean onTouch(View v, MotionEvent event) {
        if (event.getAction() == MotionEvent.ACTION_DOWN) {
            MediaPlayer mp = MediaPlayer.create(getBaseContext(),
                    R.raw.vanilla_twilight);
            mp.start();
            mp.setOnCompletionListener(new OnCompletionListener() {

                @Override
                public void onCompletion(MediaPlayer mp) {
                    mp.release();
                }
            });
        };
        return true;
    }
}

我的xml文件:

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

<ImageButton
    android:id="@+id/imageButton5"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="right"
    android:layout_marginRight="30dp"
    android:layout_marginTop="15dp"
    android:background="@drawable/ic_speak" />


<ImageView
    android:id="@+id/imageView1"
    android:layout_width="366dp"
    android:layout_height="125dp"
    android:layout_gravity="center_horizontal"
    android:layout_weight="0.39"
    android:src="@drawable/school_bus" />

<ImageButton
    android:id="@+id/imageButton1"
    android:layout_width="64dp"
    android:layout_height="92dp"
    android:layout_gravity="right"
    android:layout_marginRight="30dp"
    android:layout_marginTop="-155dp"
    android:background="@drawable/icon" />

<TextView
    android:id="@+id/textView1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="center_horizontal"
    android:layout_marginTop="45dp"
    android:text="@string/bus_pg"
    android:textAppearance="?android:attr/textAppearanceLarge"
    android:textSize="50px" />

</LinearLayout>

logcat的:

05-19 19:38:53.281: E/AndroidRuntime(492): FATAL EXCEPTION: main
05-19 19:38:53.281: E/AndroidRuntime(492): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.button/com.example.button.AppActivity}: android.view.InflateException: Binary XML file line #18: Error inflating class <unknown>
05-19 19:38:53.281: E/AndroidRuntime(492):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2110)
05-19 19:38:53.281: E/AndroidRuntime(492):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2135)
05-19 19:38:53.281: E/AndroidRuntime(492):  at android.app.ActivityThread.access$700(ActivityThread.java:143)
05-19 19:38:53.281: E/AndroidRuntime(492):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1241)
05-19 19:38:53.281: E/AndroidRuntime(492):  at android.os.Handler.dispatchMessage(Handler.java:99)
05-19 19:38:53.281: E/AndroidRuntime(492):  at android.os.Looper.loop(Looper.java:137)
05-19 19:38:53.281: E/AndroidRuntime(492):  at android.app.ActivityThread.main(ActivityThread.java:4950)
05-19 19:38:53.281: E/AndroidRuntime(492):  at java.lang.reflect.Method.invokeNative(Native Method)
05-19 19:38:53.281: E/AndroidRuntime(492):  at java.lang.reflect.Method.invoke(Method.java:511)
05-19 19:38:53.281: E/AndroidRuntime(492):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1004)
05-19 19:38:53.281: E/AndroidRuntime(492):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:771)
05-19 19:38:53.281: E/AndroidRuntime(492):  at dalvik.system.NativeStart.main(Native Method)
05-19 19:38:53.281: E/AndroidRuntime(492): Caused by: android.view.InflateException: Binary XML file line #18: Error inflating class <unknown>
05-19 19:38:53.281: E/AndroidRuntime(492):  at android.view.LayoutInflater.createView(LayoutInflater.java:613)
05-19 19:38:53.281: E/AndroidRuntime(492):  at com.android.internal.policy.impl.PhoneLayoutInflater.onCreateView(PhoneLayoutInflater.java:56)
05-19 19:38:53.281: E/AndroidRuntime(492):  at android.view.LayoutInflater.onCreateView(LayoutInflater.java:660)
05-19 19:38:53.281: E/AndroidRuntime(492):  at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:685)
05-19 19:38:53.281: E/AndroidRuntime(492):  at android.view.LayoutInflater.rInflate(LayoutInflater.java:746)
05-19 19:38:53.281: E/AndroidRuntime(492):  at android.view.LayoutInflater.inflate(LayoutInflater.java:489)
05-19 19:38:53.281: E/AndroidRuntime(492):  at android.view.LayoutInflater.inflate(LayoutInflater.java:396)
05-19 19:38:53.281: E/AndroidRuntime(492):  at android.view.LayoutInflater.inflate(LayoutInflater.java:352)
05-19 19:38:53.281: E/AndroidRuntime(492):  at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:316)
05-19 19:38:53.281: E/AndroidRuntime(492):  at android.app.Activity.setContentView(Activity.java:1915)
05-19 19:38:53.281: E/AndroidRuntime(492):  at com.example.button.AppActivity.onCreate(AppActivity.java:29)
05-19 19:38:53.281: E/AndroidRuntime(492):  at android.app.Activity.performCreate(Activity.java:5177)
05-19 19:38:53.281: E/AndroidRuntime(492):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1094)
05-19 19:38:53.281: E/AndroidRuntime(492):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2074)
05-19 19:38:53.281: E/AndroidRuntime(492):  ... 11 more
05-19 19:38:53.281: E/AndroidRuntime(492): Caused by: java.lang.reflect.InvocationTargetException
05-19 19:38:53.281: E/AndroidRuntime(492):  at java.lang.reflect.Constructor.constructNative(Native Method)
05-19 19:38:53.281: E/AndroidRuntime(492):  at java.lang.reflect.Constructor.newInstance(Constructor.java:417)
05-19 19:38:53.281: E/AndroidRuntime(492):  at android.view.LayoutInflater.createView(LayoutInflater.java:587)
05-19 19:38:53.281: E/AndroidRuntime(492):  ... 24 more
05-19 19:38:53.281: E/AndroidRuntime(492): Caused by: java.lang.OutOfMemoryError
05-19 19:38:53.281: E/AndroidRuntime(492):  at android.graphics.BitmapFactory.nativeDecodeAsset(Native Method)
05-19 19:38:53.281: E/AndroidRuntime(492):  at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:594)
05-19 19:38:53.281: E/AndroidRuntime(492):  at android.graphics.BitmapFactory.decodeResourceStream(BitmapFactory.java:447)
05-19 19:38:53.281: E/AndroidRuntime(492):  at android.graphics.drawable.Drawable.createFromResourceStream(Drawable.java:781)
05-19 19:38:53.281: E/AndroidRuntime(492):  at android.content.res.Resources.loadDrawable(Resources.java:1963)
05-19 19:38:53.281: E/AndroidRuntime(492):  at android.content.res.TypedArray.getDrawable(TypedArray.java:601)
05-19 19:38:53.281: E/AndroidRuntime(492):  at android.widget.ImageView.<init>(ImageView.java:120)
05-19 19:38:53.281: E/AndroidRuntime(492):  at android.widget.ImageView.<init>(ImageView.java:110)
05-19 19:38:53.281: E/AndroidRuntime(492):  ... 27 more

0 个答案:

没有答案