0

There's nothing fancy or complicated here. I am just looking to open a new, blank, activity from when I press on the textview. From the projects completed files, I seem to have done it as it wants me to. However, every time I click on the textview to start the intent to open the other activity, it crashes. I cannot seem to find the cause either. In a separate app I've made that send a text string from an edit text field via a button, seems to work fine and that has some other more detailed code to perform that, so I am not entirely sure where the error is arising here.

MainActivity.java

package com.example.android.miwok;

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

public class MainActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    // Set the content of the activity to use the activity_main.xml layout file
    setContentView(R.layout.activity_main);
 }

public void openNumbersList(View view) {
    Intent i = new Intent(this, NumbersActivity.class);
    startActivity(i);
 }
}

NumbersActivity.java

package com.example.android.miwok;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.TextView;

public class NumbersActivity extends AppCompatActivity {

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

 }
}

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
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:background="@color/tan_background"
android:orientation="vertical"
tools:context="com.example.android.miwok.MainActivity">

<TextView
    android:id="@+id/numbers"
    style="@style/CategoryStyle"
    android:background="@color/category_numbers"
    android:onClick="openNumbersList"
    android:text="@string/category_numbers" />

<TextView
    android:id="@+id/family"
    style="@style/CategoryStyle"
    android:background="@color/category_family"
    android:text="@string/category_family" />

<TextView
    android:id="@+id/colors"
    style="@style/CategoryStyle"
    android:background="@color/category_colors"
    android:text="@string/category_colors" />

<TextView
    android:id="@+id/phrases"
    style="@style/CategoryStyle"
    android:background="@color/category_phrases"
    android:text="@string/category_phrases" />

</LinearLayout>

In logcat there's an error showing when the app crashes:

02-20 20:10:26.762 7582-7582/com.example.android.miwok E/AndroidRuntime: FATAL EXCEPTION: main Process: com.example.android.miwok, PID: 7582 java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.android.miwok/com.example.android.miwok.NumbersActivity}: android.view.InflateException: Binary XML file line #0: Binary XML file line #0: Error inflating class TextView

OneCricketeer
  • 179,855
  • 19
  • 132
  • 245
Yintii
  • 43
  • 11

2 Answers2

2

In your xml layout for the Text view with id numbers add

android:clickable="true"

Only if this attribute is set to true the text view's onclick handler will be called

And also add height and width to all your textviews.. like this

android:height="wrap_content"
android:width="wrap_content"

This must have caused the app crash

There is a post on how to set click listeners to textviews here .

  • 1
    Not having this set won't cause it to crash when clicking on it – codeMagic Feb 21 '18 at 02:47
  • I did add this before the verified fix to see if it was correct or caused the crash, and it was not. I just added the two android attributes clickable and then focusable (focusable after testing *just* clickable and it crashing still). Upon fixing my xml and verifying it worked, I removed these and it still works fine. – Yintii Feb 21 '18 at 03:42
0

"Error inflating class TextView" means that your xml in the next activity is faulty. Try removing all the views from the 2nd activity's layout, double check the layout and then try it. If it is just the intent launch you wanna confirm, this should work.

  • Very nice sir, as you stated, my xml in the new activity was incorrect. I had android:textSize="?android:textAppearanceLarge" – Yintii Feb 21 '18 at 03:39