尝试使用Intents启动新活动时,我的Android应用程序崩溃了

时间:2018-06-25 17:57:06

标签: java android android-studio android-intent

是Android编程的新手,所以我决定构建一个测验应用程序。我建立了布局,并想设置一个按钮来启动一个新活动,该活动开始了测验。我还正确地在MainActivity中设置了该方法,但是当我运行该应用程序并单击该按钮时,该应用程序崩溃并在logcat上留下了大量错误消息。

任何帮助将不胜感激。下面是代码。

XML代码:

<Button
    android:id="@+id/button_view"
    android:layout_width="200dp"
    android:layout_height="60dp"
    android:layout_below="@id/name_view"
    android:layout_centerHorizontal="true"
    android:layout_marginTop="30dp"
    android:background="@drawable/button_round"
    android:fontFamily="sans-serif-light"
    android:inputType="textCapWords"
    android:text="Go!"
    android:onClick="startQuestionOne"
    android:textSize="24dp" />

MainActivity

public class LoginActivity extends AppCompatActivity {

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

public void startQuestionOne(View view) {
    Intent myIntent = new Intent(LoginActivity.this, QuestionOneActivity.class);
    startActivity(myIntent);
}
}

Logcat消息

  

06-25 18:32:34.259 13705-13705 / com.example.android.udacityquizapp E / AndroidRuntime:FATAL EXCEPTION:main       流程:com.example.android.udacityquizapp,PID:13705       java.lang.RuntimeException:无法启动活动ComponentInfo {com.example.android.udacityquizapp / com.example.android.udacityquizapp.QuestionOneActivity}:android.view.InflateException:二进制XML文件行#23:错误,将类夸大了           在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2344)           在android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2396)           在android.app.ActivityThread.access $ 800(ActivityThread.java:154)           在android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1324)           在android.os.Handler.dispatchMessage(Handler.java:110)           在android.os.Looper.loop(Looper.java:193)           在android.app.ActivityThread.main(ActivityThread.java:5328)           在java.lang.reflect.Method.invokeNative(本机方法)           在java.lang.reflect.Method.invoke(Method.java:515)           在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:828)           在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:644)           在dalvik.system.NativeStart.main(本机方法)        原因:android.view.InflateException:二进制XML文件行#23:错误夸大了类null           在android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:713)           在android.view.LayoutInflater.rInflate(LayoutInflater.java:755)           在android.view.LayoutInflater.inflate(LayoutInflater.java:492)           在android.view.LayoutInflater.inflate(LayoutInflater.java:397)           在android.view.LayoutInflater.inflate(LayoutInflater.java:353)           在android.support.v7.app.AppCompatDelegateImplV9.setContentView(AppCompatDelegateImplV9.java:287)           在android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:139)           在com.example.android.udacityquizapp.QuestionOneActivity.onCreate(QuestionOneActivity.java:11)           在android.app.Activity.performCreate(Activity.java:5279)           在android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1088)           在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2308)           在android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2396)           在android.app.ActivityThread.access $ 800(ActivityThread.java:154)           在android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1324)           在android.os.Handler.dispatchMessage(Handler.java:110)           在android.os.Looper.loop(Looper.java:193)           在android.app.ActivityThread.main(ActivityThread.java:5328)           在java.lang.reflect.Method.invokeNative(本机方法)           在java.lang.reflect.Method.invoke(Method.java:515)           在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:828)           在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:644)           在dalvik.system.NativeStart.main(本机方法)        造成原因:java.lang.NullPointerException           在android.support.v7.app.AppCompatViewInflater.createView(AppCompatViewInflater.java:99)           在android.support.v7.app.AppCompatDelegateImplV9.createView(AppCompatDelegateImplV9.java:1035)           在android.support.v7.app.AppCompatDelegateImplV9.onCreateView(AppCompatDelegateImplV9.java:1092)           在android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:684)           在android.view.LayoutInflater.rInflate(LayoutInflater.java:755)           在android.view.LayoutInflater.inflate(LayoutInflater.java:492)           在android.view.LayoutInflater.inflate(LayoutInflater.java:397)           在android.view.LayoutInflater.inflate(LayoutInflater.java:353)           在android.support.v7.app.AppCompatDelegateImplV9.setContentView(AppCompatDelegateImplV9.java:287)           在android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:139)           在com.example.android.udacityquizapp.QuestionOneActivity.onCreate(QuestionOneActivity.java:11)           在android.app.Activity.performCreate(Activity.java:5279)           在android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1088)           在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2308)           在android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2396)           在android.app.ActivityThread.access $ 800(ActivityThread.java:154)           在android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1324)           在android.os.Handler.dispatchMessage(Handler.java:110)           在android.os.Looper.loop(Looper.java:193)           在android.app.ActivityThread.main(ActivityThread.java:5328)           在java.lang.reflect.Method.invokeNative(本机方法)           在java.lang.reflect.Method.invoke(Method.java:515)           在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:828)           在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:644)           在dalvik.system.NativeStart.main(本机方法)

这是完整的XML

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/gradient"
tools:context=".LoginActivity">

<ImageView
    android:id="@+id/udacity_logo"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_centerHorizontal="true"
    android:layout_marginTop="30dp"
    android:src="@drawable/udacity" />

<TextView
    android:id="@+id/textview_text"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@id/udacity_logo"
    android:layout_centerHorizontal="true"
    android:layout_marginTop="20dp"
    android:fontFamily="sans-serif-ligh"
    android:text="Udacity Quiz App"
    android:textColor="#faebd7"
    android:textSize="36dp"
    android:textStyle="bold" />

<EditText
    android:id="@+id/name_view"
    android:layout_width="300dp"
    android:layout_height="40dp"
    android:layout_below="@id/textview_text"
    android:layout_centerHorizontal="true"
    android:layout_marginTop="30dp"
    android:background="@drawable/edit_round"
    android:drawableLeft="@drawable/ic_action_user"
    android:ems="10"
    android:fontFamily="sans-serif-light"
    android:hint="Name"
    android:inputType="textCapWords"
    android:textColor="#000000"
    android:textColorHint="#000000"
    android:textSize="16dp" />

<Button
    android:id="@+id/button_view"
    android:layout_width="200dp"
    android:layout_height="60dp"
    android:layout_below="@id/name_view"
    android:layout_centerHorizontal="true"
    android:layout_marginTop="30dp"
    android:background="@drawable/button_round"
    android:fontFamily="sans-serif-light"
    android:inputType="textCapWords"
    android:text="Go!"
    android:onClick="startQuestionOne"
    android:textSize="24dp" />

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:layout_alignParentRight="true"
    android:layout_marginBottom="8dp"
    android:layout_marginRight="8dp"
    android:fontFamily="sans-serif-light"
    android:text="powered by: @William"
    android:textColor="#faebd7" />
</RelativeLayout>

上面是LoginActivity的XML,其中包含一个按钮。单击该按钮后,它应该启动一个名为QuestionOneActivity的新活动,但应用程序崩溃。

LoginActivity

package com.example.android.udacityquizapp;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;

public class LoginActivity extends AppCompatActivity {

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

public void startQuestionOne(View view) {
    Intent myIntent = new Intent(LoginActivity.this, 
QuestionOneActivity.class);
    startActivity(myIntent);
}
}

2 个答案:

答案 0 :(得分:0)

首先将onCreate()中的Button绑定为

<!DOCTYPE html>
<html lang="">

<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title></title>
</head>

<body>
  <img src="https://user-images.githubusercontent.com/13046668/41792152-165b8206-7682-11e8-8182-dad5d193b494.png" class="img">
  <!--
-->
  <div class="container">
    <div class="overline">overline</div>
    <div class="content">superman is back</div>
  </div>
</body>

</html>

然后检查清单文件中是否声明了您的活动QuestionOneActivity 之后,检查QuestionOneActivity活动xml文件是否有错误...

答案 1 :(得分:0)

感谢clay_to_n,根据您的建议,我发现我在View标签中使用了小写的'v'。调整后,该应用程序将按预期运行。