30/09/2018, 23:44

Các bạn cho hỏi đoạn code Android sau sai ở đâu?

Khi mình run chương trình thì: Unfortunately Myapplication has stopped.

file.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:background="@color/colorLightMilk"
android:orientation="vertical">

<LinearLayout
    android:layout_marginTop="10dp"
    android:layout_width="match_parent"
    android:layout_height="85dp"
    android:orientation="horizontal">

    <RelativeLayout
        android:layout_width="170dp"
        android:layout_height="85dp">

        <TextView
            android:id="@+id/buoc1"
            android:text="CHUẨN BỊ NGUYÊN LIỆU."
            android:layout_width="170dp"
            android:layout_height="47dp"
            android:layout_marginTop="30dp"
            android:gravity="center"
            android:layout_centerVertical="true"
            android:textColor="@color/colorGreen"
            android:background="@color/colorRed"></TextView>
    </RelativeLayout>

    <RelativeLayout
        android:layout_width="210dp"
        android:layout_height="85dp">

        <ImageView
            android:id="@+id/image1"
            android:layout_height="80dp"
            android:layout_width="match_parent"
            android:src="@drawable/buocdau"
            android:layout_centerHorizontal="true" />
    </RelativeLayout>
</LinearLayout>

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="85dp"
    android:orientation="horizontal">

    <RelativeLayout
        android:layout_width="170dp"
        android:layout_height="85dp">

        <TextView
            android:id="@+id/buoc2"
            android:text="XAY THÀNH BỘT."
            android:layout_width="170dp"
            android:layout_height="47dp"
            android:gravity="center"
            android:layout_centerVertical="true"
            android:textColor="@color/colorGreen"
            android:background="@color/colorRed"></TextView>
    </RelativeLayout>

    <RelativeLayout
        android:layout_width="210dp"
        android:layout_height="85dp">
        <ImageView
            android:id="@+id/image2"
            android:layout_height="80dp"
            android:layout_width="match_parent"
            android:layout_centerHorizontal="true"
            android:src="@drawable/buoc2" />
    </RelativeLayout>
</LinearLayout>

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="85dp"
    android:orientation="horizontal">

    <RelativeLayout
        android:layout_width="170dp"
        android:layout_height="85dp">

        <TextView
            android:id="@+id/buoc4"
            android:text="ĐỢI."
            android:layout_width="170dp"
            android:layout_height="47dp"
            android:layout_centerVertical="true"
            android:gravity="center"
            android:textColor="@color/colorGreen"
            android:background="@color/colorRed"></TextView>
    </RelativeLayout>

    <RelativeLayout
        android:layout_width="210dp"
        android:layout_height="85dp">


        <ImageView
            android:id="@+id/image4"
            android:layout_height="80dp"
            android:layout_width="match_parent"
            android:layout_centerHorizontal="true"
            android:src="@drawable/buoc4" />
    </RelativeLayout>
</LinearLayout>

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="85dp"
    android:orientation="horizontal">

    <RelativeLayout
        android:layout_width="170dp"
        android:layout_height="85dp">

        <TextView
            android:id="@+id/buoc5"
            android:text="THÊM SỮA VÀ THƯỞNG THỨC."
            android:layout_width="170dp"
            android:layout_height="47dp"
            android:gravity="center"
            android:layout_centerVertical="true"
            android:textColor="@color/colorGreen"
            android:background="@color/colorRed"></TextView>
    </RelativeLayout>

    <RelativeLayout
        android:layout_width="210dp"
        android:layout_height="85dp">

        <ImageView
        android:id="@+id/image5"
        android:layout_height="80dp"
        android:layout_width="match_parent"
        android:layout_marginLeft="0dp"
        android:src="@drawable/buoc5sua" />
    </RelativeLayout>
</LinearLayout>


<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="150dp">

    <Button
        android:id="@+id/quayLai"
        android:text="QUAY LẠI"
        android:layout_marginTop="30dp"
        android:layout_width="200dp"
        android:layout_height="60dp"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true"
        android:textColor="@color/colorRed"
        android:background="@color/colorGreen" />
</RelativeLayout>

</LinearLayout>

Còn đây là file.java:

package inh_khc_quy_developer_apps.myapplication;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;


public class Test extends AppCompatActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_test);
    }
}

file manifest

    <?xml version="1.0" encoding="utf-8"?>
    <manifest xmlns:android="http://schemas.android.com/apk/res/android"
        package="inh_khc_quy_developer_apps.myapplication">

        <application
            android:allowBackup="true"
            android:icon="@mipmap/ic_launcher"
            android:label="@string/app_name"
            android:supportsRtl="true"
            android:theme="@style/AppTheme">
            <activity android:name=".Test">
                <intent-filter>
                    <action android:name="android.intent.action.MAIN" />

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

    </manifest>

Ảnh thì đầy đủ rồi nhé, MyApplication has stopped.

X viết 01:56 ngày 01/10/2018

Post thêm phần android manifest luôn đi bạn ơi

Ai Android viết 02:00 ngày 01/10/2018

có thể do dimension file ảnh của bạn to quá thử chỉnh lại nhỏ hơn xem =)) 1 số máy bị ntn, cũng ko hiểu tại sao

Module Đk viết 01:49 ngày 01/10/2018

Đúng rồi bạn, mình mất bao nhiêu công sức tìm cái lỗi này… Nó bảo of out memory exception mà chưa biết thế nào cho không outofmemory dung lượng memory là bao nhiêu. Fix lại ảnh cho nhỏ thì ok… Mình mới tìm hiểu thì cũng còn non.

Đoàn Hiếu Tâm viết 01:54 ngày 01/10/2018

Có 1 cách sửa là thêm android:largeHeap="true" vào thẻ application trong manifest. Cách này không được recomend cho lắm.
Bị vậy là do bộ nhớ Java Heap của mỗi máy được nhà sản xuất phần cứng quy định cố định, dù bộ nhớ trong có lớn bao nhiêu. Nếu quá giới hạn thì nó bị Out Of Memory Exception.

Module Đk viết 01:59 ngày 01/10/2018

Cảm ơn bạn, một kiến thức rất hữu ích

Bài liên quan
0