Yintii
Yintii

Reputation: 43

Wanting to throw an intent to open a new activity but continually crashes

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

Upvotes: 0

Views: 72

Answers (2)

Anshuman Rohella
Anshuman Rohella

Reputation: 105

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

Upvotes: 0

Daniel Appasamy
Daniel Appasamy

Reputation: 31

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 .

Upvotes: 2

Related Questions