-2

Am new to Android programming and I decided to build a quiz app. I built the layout and wanted to set up a button to start a new activity which begins the quiz. I also set up the method in the MainActivity correctly but when I run the app and click on the button, the app crashes and leaves a ton of error messages on logcat.

Any help will be appreciated. Below are the codes.

XML Code:

<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 Message

06-25 18:32:34.259 13705-13705/com.example.android.udacityquizapp E/AndroidRuntime: FATAL EXCEPTION: main Process: com.example.android.udacityquizapp, PID: 13705 java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.android.udacityquizapp/com.example.android.udacityquizapp.QuestionOneActivity}: android.view.InflateException: Binary XML file line #23: Error inflating class null at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2344) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2396) at android.app.ActivityThread.access$800(ActivityThread.java:154) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1324) at android.os.Handler.dispatchMessage(Handler.java:110) at android.os.Looper.loop(Looper.java:193) at android.app.ActivityThread.main(ActivityThread.java:5328) at java.lang.reflect.Method.invokeNative(Native Method) at java.lang.reflect.Method.invoke(Method.java:515) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:828) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:644) at dalvik.system.NativeStart.main(Native Method) Caused by: android.view.InflateException: Binary XML file line #23: Error inflating class null at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:713) at android.view.LayoutInflater.rInflate(LayoutInflater.java:755) at android.view.LayoutInflater.inflate(LayoutInflater.java:492) at android.view.LayoutInflater.inflate(LayoutInflater.java:397) at android.view.LayoutInflater.inflate(LayoutInflater.java:353) at android.support.v7.app.AppCompatDelegateImplV9.setContentView(AppCompatDelegateImplV9.java:287) at android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:139) at com.example.android.udacityquizapp.QuestionOneActivity.onCreate(QuestionOneActivity.java:11) at android.app.Activity.performCreate(Activity.java:5279) at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1088) at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2308) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2396)  at android.app.ActivityThread.access$800(ActivityThread.java:154)  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1324)  at android.os.Handler.dispatchMessage(Handler.java:110)  at android.os.Looper.loop(Looper.java:193)  at android.app.ActivityThread.main(ActivityThread.java:5328)  at java.lang.reflect.Method.invokeNative(Native Method)  at java.lang.reflect.Method.invoke(Method.java:515)  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:828)  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:644)  at dalvik.system.NativeStart.main(Native Method)  Caused by: java.lang.NullPointerException at android.support.v7.app.AppCompatViewInflater.createView(AppCompatViewInflater.java:99) at android.support.v7.app.AppCompatDelegateImplV9.createView(AppCompatDelegateImplV9.java:1035) at android.support.v7.app.AppCompatDelegateImplV9.onCreateView(AppCompatDelegateImplV9.java:1092) at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:684) at android.view.LayoutInflater.rInflate(LayoutInflater.java:755)  at android.view.LayoutInflater.inflate(LayoutInflater.java:492)  at android.view.LayoutInflater.inflate(LayoutInflater.java:397)  at android.view.LayoutInflater.inflate(LayoutInflater.java:353)  at android.support.v7.app.AppCompatDelegateImplV9.setContentView(AppCompatDelegateImplV9.java:287)  at android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:139)  at com.example.android.udacityquizapp.QuestionOneActivity.onCreate(QuestionOneActivity.java:11)  at android.app.Activity.performCreate(Activity.java:5279)  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1088)  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2308)  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2396)  at android.app.ActivityThread.access$800(ActivityThread.java:154)  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1324)  at android.os.Handler.dispatchMessage(Handler.java:110)  at android.os.Looper.loop(Looper.java:193)  at android.app.ActivityThread.main(ActivityThread.java:5328)  at java.lang.reflect.Method.invokeNative(Native Method)  at java.lang.reflect.Method.invoke(Method.java:515)  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:828)  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:644)  at dalvik.system.NativeStart.main(Native Method) 

This is the full 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>

Above is the XML for LoginActivity which contains a button. When the button is clicked, its supposed to start a new activity called QuestionOneActivity, but instead, the app crashes.

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);
}
}
Ram Koti
  • 2,203
  • 7
  • 26
  • 36
William
  • 13
  • 3
  • The error indicates there's an issue in your QuestionOneActivity's XML file on line 23. If you investigate that and can't figure it out, posting the full file (or the full application) will make it easier to help out. From the RuntimeException you posted: "QuestionOneActivity: android.view.InflateException: Binary XML file line #23: Error inflating class null" – clay_to_n Jun 25 '18 at 18:01
  • information is not sufficient – Godfather Jun 25 '18 at 18:04
  • May be casting problem in question one activity – Faisal Mohammad Jun 25 '18 at 18:06
  • I have edited the post to add the full codes from XML and Java. – William Jun 25 '18 at 18:35
  • @clay_to_n I added a view tag on line 23. Also, at the top right side of the XML layout, i get a Render Error, Unable to inflate view tag without class attribute – William Jun 25 '18 at 18:44
  • If you mean you have a `` tag in the layout for `QuestionOneActivity`, it needs to be ``, with a uppercase `V`. – Mike M. Jun 25 '18 at 22:48
  • @William do you have the .java and .xml files for the QuestionOneActivity? It sounds like the error might be in one of those files. I think the XML error referenced is probably coming from there. – clay_to_n Jun 25 '18 at 22:49

2 Answers2

0

firstly bind Button in onCreate() as

Button b = (Button) findViewById(R.id.button_view);

then check if your activity QuestionOneActivity is declared in manifest file after that check QuestionOneActivity activity xml file for error if have any...

TejpalBh
  • 427
  • 4
  • 13
0

Thanks clay_to_n, based on your suggestions, i found out that i used a lower case 'v' for my View tag. When i adjusted it, the app runs as expected.

William
  • 13
  • 3