启动活动

时间:2015-09-06 11:11:54

标签: android android-intent android-activity android-xml runtimeexception

我知道在尝试开始活动时有关于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.access$800(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);
}

}

显然{/ 1}}是在

时引起的
RunTimeException

执行这两行。

请帮帮我。

LognActivity.java:

Intent intent = new Intent(MainActivity.this,LoginActivity.class);
startActivity(intent); 

header_gradient.xml:

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);
}
}

footer_repeat.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>

1 个答案:

答案 0 :(得分:1)

下面:

<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的绘图,选择器和形状的正确位置。然后使用:

android:background="@drawable/header_gradient"