why am I getting errors when I use different backg

2019-09-06 19:14发布

问题:

I have 3 activities which I want to use different backgrounds with. but when i do this i get this i get this error however if i only use different backgrounds on 2 activities then the app works

11-20 13:40:25.855: E/AndroidRuntime(849): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.medepad.community_virtual_ward/com.medepad.community_virtual_ward.Temperature}: android.view.InflateException: Binary XML file line #2: Error inflating class

the activities are called main welcome and temperature.

code for main xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".MainActivity"
    android:background="@drawable/main" >

    <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="400dp"
        android:layout_marginTop="115dp"
        android:text="TextView"
        android:textColor="#000000"
        android:textSize="40dp" />
</RelativeLayout>

java code

package com.medepad.community_virtual_ward;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.TextView;

public class MainActivity extends Activity implements OnClickListener {

    int a;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        TextView start = (TextView)findViewById(R.id.textView1);
        start.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
    // TODO Auto-generated method stub
    Intent welcome= new Intent (this,Welcome.class);
        startActivity(welcome);
    }
}

code for temperature

<?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="vertical"
    android:background="@drawable/temperature" >

</LinearLayout>

java code

package com.medepad.community_virtual_ward;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;

public class Temperature extends Activity implements OnClickListener {


@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.temperature);
}

@Override
public void onClick(View v) {
    // TODO Auto-generated method stub

}
}

welcome xml code

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:background="@drawable/welcome" >

    <TextView
        android:id="@+id/textView2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:layout_marginLeft="400dp"
        android:layout_marginTop="115dp"
        android:text="TextView"
        android:textColor="#000000"
        android:textSize="40dp" />

</RelativeLayout>

java code

package com.medepad.community_virtual_ward;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.TextView;

public class Welcome extends Activity implements OnClickListener {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.welcome);
        TextView next= (TextView)findViewById(R.id.textView2);
        next.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub
        Intent temperature= new Intent(this, Temperature.class);
        startActivity(temperature);
    }
}

why am i getting this error and what can i do to use the amount of backgrounds i want?

回答1:

The end of your first XML file has an extra >.



回答2:

Please check the following file

temperature.xml

Make sure, it has following contents

<?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="vertical"
    android:background="@drawable/temperature" >

</LinearLayout>

and you have either defined a drawable resource with name temperature.xml and main.xml under res/drawable folder or you have added image resources with name temperature and main in your Android project.