java - 想要抛出打开新 Activity 的 Intent ,但不断崩溃

标签 java android android-intent

这里没有什么花哨或复杂的东西。我只是想在按下 TextView 时打开一个新的空白 Activity 。从项目完成的文件来看,我似乎已经按照它的要求完成了。但是,每次我单击 TextView 以启动打开其他 Activity 的 Intent 时,它都会崩溃。我似乎也找不到原因。在一个单独的应用程序中,我制作了通过按钮从编辑文本字段发送文本字符串的应用程序,似乎工作正常,并且有一些其他更详细的代码来执行该操作,所以我不完全确定这里出现错误的位置。

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>

在 logcat 中,应用程序崩溃时会显示错误:

02-20 20:10:26.762 7582-7582/com.example.android.miwok E/AndroidRuntime:致命异常:main 进程:com.example.android.miwok,PID:7582 java.lang.RuntimeException:无法启动 Activity ComponentInfo{com.example.android.miwok/com.example.android.miwok.NumbersActivity}:android.view.InflateException:二进制 XML 文件行 #0:二进制 XML 文件行 #0 : 膨胀类 TextView 时出错

最佳答案

在 TextView 的 xml 布局中添加 ID 号

android:clickable="true"

仅当此属性设置为 true 时, TextView 的 onclick 处理程序才会被调用

并且还为所有 TextView 添加高度和宽度..像这样

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

这肯定导致了应用程序崩溃

有一篇关于如何将点击监听器设置为 TextView 的帖子 here .

关于java - 想要抛出打开新 Activity 的 Intent ,但不断崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48897308/

相关文章:

android - 使用来自 findViewById 的 View 时出现 NullPointerException

android - 使 Activity A 等待 Activity B 完成以继续

java - 从 Spring Web 服务获取 Jetty 实例

java - 需要 modbus Java 库

android - Apache cordova 还是即时通讯应用程序的 native 开发?

android - 模拟器在拍摄照片时崩溃(NullPointerException)

android - 如何将图像添加到android中的AlertDialog?

java - Spring Boot 使用 Json 作为请求参数而不是实体/模型

c# - 在 C# 中创建实例时重写抽象方法

Android In App Billing 仅在 Samsung tab 10.1 中崩溃