【问题标题】:I need help getting an intent to open another activity我需要帮助以获得打开另一项活动的意图
【发布时间】:2017-07-13 00:32:58
【问题描述】:

我需要能够触摸 TextView 并让它打开另一个页面,但是,当我运行应用程序并触摸 TextView 时,应用程序崩溃了。

代码如下:

 <?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:padding= "10dp"
    android:background="@drawable/background"
    android:orientation="vertical"
    tools:context="com.example.android.practiceapp.MainActivity">

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

这是java代码:

    package com.example.android.practiceapp;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;

public class MainActivity extends Activity {

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

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


}

【问题讨论】:

  • 您是否已将Numbers 活动添加到清单?这个问题的可能性将与数字活动有关。如果您发布日志会很有帮助。
  • 应用程序崩溃 不是有用的问题描述。日志在哪里?堆栈跟踪告诉您发生了什么错误?见How to Ask。这个网站是针对特定问题应用程序崩溃。怎么了?绝不是特定的
  • 请提供logcat输出

标签: java android xml android-studio


【解决方案1】:

确保 Numbers 活动存在并且在 Android.manifest 中。

所有活动都必须由清单文件中的元素表示。任何未在此处声明的内容都不会被系统看到并且永远不会运行。

【讨论】:

    【解决方案2】:

    首先,你没有初始化你的TextView

    textView = (TextView)findViewById(R.id.numbers)
    

    其次,按照其他人的建议,在您的 Android.manifest

    中添加 Numbers 活动

    【讨论】:

      【解决方案3】:

      你没有添加 clickable 属性,没有它,点击处理程序不会被调用。所以,添加它...

      ...
      
      <TextView
              android:id="@+id/numbers"
              style="@style/CategoryStyle"
              android:layout_marginBottom="16dp"
              android:background="@drawable/numbers"
              android:text="@string/category_numbers"
              android:onClick="openNumbersList"
              android:clickable="true"/>
      
      ...
      

      【讨论】:

        【解决方案4】:

        感谢您的回复,我实际上运行了调试器,结果我不得不将 numbers 文件中的 java 代码从“extends AppCompatActivity”更改为“extends Activity”,就像 MainActivity java 文件一样

        【讨论】:

          【解决方案5】:

          尝试插入

          MainActivity.this
          

          而不是

          this
          

          像这样

          Intent i = new Intent(MainActivity.this, Numbers.class);
          

          【讨论】:

          • 我相信这不会解决任何问题;这不像new Intent() 是在内部类中实例化的。
          猜你喜欢
          • 2015-08-13
          • 2010-12-18
          • 1970-01-01
          • 2011-04-28
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多