启动时出现 RunTimeException activity

RunTimeException when starting activity

我知道在尝试开始 activity 时有数十亿个关于 RunTimeException 的问题,但我真的没有找到问题的答案。

LogCat:

09-06 13:35:38.283: E/AndroidRuntime(14080): FATAL EXCEPTION: main
09-06 13:35:38.283: E/AndroidRuntime(14080): Process: com.example.mylifeinformationsharedsocial, PID: 14080
09-06 13:35:38.283: E/AndroidRuntime(14080): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.mylifeinformationsharedsocial/com.example.mylifeinformationsharedsocial.LoginActivity}: android.view.InflateException: Binary XML file line #17: Error inflating class <unknown>
09-06 13:35:38.283: E/AndroidRuntime(14080):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2305)
09-06 13:35:38.283: E/AndroidRuntime(14080):    at android.app.ActivityThread.access0(ActivityThread.java:148)
09-06 13:35:38.283: E/AndroidRuntime(14080):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1283)
09-06 13:35:38.283: E/AndroidRuntime(14080):    at android.os.Handler.dispatchMessage(Handler.java:102)
09-06 13:35:38.283: E/AndroidRuntime(14080):    at android.os.Looper.loop(Looper.java:135)
09-06 13:35:38.283: E/AndroidRuntime(14080):    at android.app.ActivityThread.main(ActivityThread.java:5274)
09-06 13:35:38.283: E/AndroidRuntime(14080):    at java.lang.reflect.Method.invoke(Method.java:372)
09-06 13:35:38.283: E/AndroidRuntime(14080):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:909)
09-06 13:35:38.283: E/AndroidRuntime(14080): Caused by: android.view.InflateException: Binary XML file line #17: Error inflating class <unknown>
09-06 13:35:38.283: E/AndroidRuntime(14080):    at android.view.LayoutInflater.createView(LayoutInflater.java:633)
09-06 13:35:38.283: E/AndroidRuntime(14080):    at android.view.LayoutInflater.onCreateView(LayoutInflater.java:682)
09-06 13:35:38.283: E/AndroidRuntime(14080):    at android.view.LayoutInflater.rInflate(LayoutInflater.java:806)
09-06 13:35:38.283: E/AndroidRuntime(14080):    at android.view.LayoutInflater.rInflate(LayoutInflater.java:809)
09-06 13:35:38.283: E/AndroidRuntime(14080):    at android.view.LayoutInflater.inflate(LayoutInflater.java:414)
09-06 13:35:38.283: E/AndroidRuntime(14080):    at android.view.LayoutInflater.inflate(LayoutInflater.java:365)
09-06 13:35:38.283: E/AndroidRuntime(14080):    at android.support.v7.app.AppCompatDelegateImplV7.setContentView(AppCompatDelegateImplV7.java:249)
09-06 13:35:38.283: E/AndroidRuntime(14080):    at android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:106)
09-06 13:35:38.283: E/AndroidRuntime(14080):    at com.example.mylifeinformationsharedsocial.LoginActivity.onCreate(LoginActivity.java:15)
09-06 13:35:38.283: E/AndroidRuntime(14080):    at android.app.Activity.performCreate(Activity.java:5977)
09-06 13:35:38.283: E/AndroidRuntime(14080):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1105)
09-06 13:35:38.283: E/AndroidRuntime(14080):    ... 10 more
09-06 13:35:38.283: E/AndroidRuntime(14080): Caused by: java.lang.reflect.InvocationTargetException
09-06 13:35:38.283: E/AndroidRuntime(14080):    at java.lang.reflect.Constructor.newInstance(Constructor.java:288)
09-06 13:35:38.283: E/AndroidRuntime(14080):    at android.view.LayoutInflater.createView(LayoutInflater.java:607)
09-06 13:35:38.283: E/AndroidRuntime(14080):    ... 24 more
09-06 13:35:38.283: E/AndroidRuntime(14080):    at android.content.res.Resources.loadDrawableForCookie(Resources.java:2469)
09-06 13:35:38.283: E/AndroidRuntime(14080):    at android.content.res.TypedArray.getDrawable(TypedArray.java:749)
09-06 13:35:38.283: E/AndroidRuntime(14080):    at android.view.View.<init>(View.java:3737)
09-06 13:35:38.283: E/AndroidRuntime(14080):    at android.view.ViewGroup.<init>(ViewGroup.java:491)
09-06 13:35:38.283: E/AndroidRuntime(14080):    at android.widget.LinearLayout.<init>(LinearLayout.java:196)
09-06 13:35:38.283: E/AndroidRuntime(14080):    at android.widget.LinearLayout.<init>(LinearLayout.java:192)
09-06 13:35:38.283: E/AndroidRuntime(14080):    ... 27 more
09-06 13:35:38.283: E/AndroidRuntime(14080):    at android.graphics.drawable.Drawable.createFromXmlInner(Drawable.java:1091)
09-06 13:35:38.283: E/AndroidRuntime(14080):    at android.graphics.drawable.Drawable.createFromXml(Drawable.java:1017)
09-06 13:35:38.283: E/AndroidRuntime(14080):    at android.content.res.Resources.loadDrawableForCookie(Resources.java:2459)
09-06 13:35:38.283: E/AndroidRuntime(14080):    ... 34 more

activity_login.xml:

<ScrollView 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="com.example.mylifeinformationsharedsocial.LoginActivity" >

<RelativeLayout
    android:layout_width="fill_parent"
    android:layout_height="wrap_content" 
    android:background="#ffffff">

    <!--  Header Starts-->
    <LinearLayout 
        android:id="@+id/linear_layout_header_id"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:background="@layout/header_gradient"
        android:paddingTop="5dip"
        android:paddingBottom="5dip">

            <!-- Logo Start-->
            <ImageView 
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:src="@drawable/hd_logo"
                android:layout_marginLeft="10dip"
                android:layout_marginStart="10dip"
                android:contentDescription="@string/imgae_view_logo_content_description"
                />

    </LinearLayout>         
            <!-- Logo Ends -->
    <!--  Header Ends -->

    <!-- Footer Start -->
    <LinearLayout
        android:id="@+id/linear_layout_footer_id"
        android:layout_width="fill_parent"
        android:layout_height="90dip"
        android:orientation="vertical"
        android:background="@layout/footer_repeat"
        android:layout_alignParentBottom="true">
    </LinearLayout>   
    <!-- Footer Ends -->

    <!-- Login Form -->
    <LinearLayout
        android:orientation="vertical"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:padding="10dip">
      <!--  Email Label -->
      <TextView 
          android:layout_width="fill_parent"
          android:layout_height="wrap_content"
          android:textColor="#372c24"
          android:text="Email"
          />
      <EditText 
          android:layout_width="fill_parent"
          android:layout_height="wrap_content"
          android:layout_marginTop="5dip"
          android:layout_marginBottom="20dip"
          android:singleLine="true"
          />
      <!--  Password Label -->
      <TextView 
          android:layout_width="fill_parent"
          android:layout_height="wrap_content"
          android:textColor="#372c24"
          android:text="Password"
          />
      <EditText 
          android:layout_width="fill_parent"
          android:layout_height="wrap_content"
          android:layout_marginTop="5dip"
          android:singleLine="true"
          android:password="true"
            />
      <!-- Login button -->
      <Button 
          android:id="@+id/btnLogin"
          android:layout_width="fill_parent"
          android:layout_height="wrap_content"
          android:layout_marginTop="10dip"
          android:text="Login"
          />
      <!-- Link to Registration Screen -->
      <TextView 
          android:id="@+id/link_to_register"
          android:layout_width="fill_parent"
          android:layout_height="wrap_content"
          android:layout_marginTop="40dip"
          android:layout_marginBottom="40dip"
          android:text="New to Twitter? Register here"
          android:gravity="center"
          android:textSize="20dip"
          android:textColor="#0b84aa"
          />

    </LinearLayout>
    <!-- Login Form Ends -->

  </RelativeLayout>

</ScrollView>

我不太了解 LogCat 但上面写着 android.view.InflateException: Binary XML file line #17: Error inflating class ,那是指 activity_login.xml 文件还是什么?

MainActivity.java:

    public class MainActivity extends AppCompatActivity 
{
TextView textView1;

Button hairCutsButton;
Button sexButton;
Button signUpButton;
Button loginButton;



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

    textView1 = (TextView) findViewById(R.id.textView_1);
    textView1.setTextColor(Color.rgb(0, 0, 0));

    hairCutsButton = (Button) findViewById(R.id.hairCutsButton_1);
    sexButton = (Button) findViewById(R.id.sexButton_1);
    signUpButton = (Button) findViewById(R.id.sign_up_button_id);
    loginButton = (Button) findViewById(R.id.login_button_id);

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

@Override
public boolean onCreateOptionsMenu(Menu menu) 
{
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) 
{
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();
    if (id == R.id.action_settings) 
    {
        return true;
    }
    return super.onOptionsItemSelected(item);
}

public void moveToHairCutsActivity(View view)
{
    Intent intent = new Intent(this,HairCutsActivity.class);
    startActivity(intent);
}
public void moveToSexActivity(View view)
{
    Intent intent = new Intent(this,SexAcivity.class);
    startActivity(intent);
}

}

显然 RunTimeException 是在

时引起的
Intent intent = new Intent(MainActivity.this,LoginActivity.class);
startActivity(intent); 

执行了这两行。

请帮帮我。

LognActivity.java:

public class LoginActivity extends AppCompatActivity 
{

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

}

@Override
public boolean onCreateOptionsMenu(Menu menu) 
{
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.login, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) 
{
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();
    if (id == R.id.action_settings) 
    {
        return true;
    }
    return super.onOptionsItemSelected(item);
}
}

header_gradient.xml:

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

<gradient
    android:startColor="#24b2eb"
    android:centerColor="#4ccbff"
    android:endColor="#24b2eb"
    android:angle="270"
    />
<corners 
    android:radius="5dp" 
    />

</Shape>

footer_repeat.xml:

<?xml version="1.0" encoding="utf-8"?>
<bitmap xmlns:android="http://schemas.android.com/apk/res/android"
    android:tileMode="repeat" 
    /> 

这里:

<LinearLayout 
        android:id="@+id/linear_layout_header_id"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:background="@layout/header_gradient" <<<<< line
        android:paddingTop="5dip"
        android:paddingBottom="5dip">
...
</LinearLayout>

当前正在从 layout 文件夹中设置可绘制的背景,这是不正确的。将 header_gradient 文件移动到 drawable 文件夹中,这是我们想要用作 background 的可绘制对象、选择器和形状的正确位置 view.then 使用:

android:background="@drawable/header_gradient"