【问题标题】:Android Studio, error with intentsAndroid Studio,意图错误
【发布时间】:2014-07-03 03:59:30
【问题描述】:

我正在使用 Android Studio 编写和运行我的 android 应用程序,但是当我尝试通过意图从一个活动转到另一个活动时遇到此错误,我要么收到此错误:

android.content.ActivityNotFoundException: Unable to find explicit activity class 
{com.example.rudrunk/junit.framework.Test}; have you declared this activity in your
AndroidManifest.xml?

或 NullPointerException 如果我使用其他意图。

我的清单文件在下面,但所有代码都自动来自 IDE,所以我不明白发生了什么。

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.rudrunk"
android:versionCode="1"
android:versionName="1.0" >
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.CALL_PHONE" />
<application android:theme="@style/AppTheme" android:label="@string/app_name" android:icon="@drawable/ic_launcher" android:debuggable="true" android:allowBackup="true">
    <activity android:label="@string/app_name" android:name="com.example.rudrunk.MainActivity">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
    <activity android:label="@string/title_activity_test" android:name="com.example.rudrunk.Test" />
    <activity android:label="@string/title_activity_drunk_test" android:name="com.example.rudrunk.DrunkTest" />
</application>

意图的Java代码:

    final Intent localIntent1 = new Intent(MainActivity.this, DrunkTest.class);
    final Intent localIntent2 = new Intent(MainActivity.this, Test.class);
    Button localButton1 = (Button)findViewById(R.id.maybe);
    Button localButton2 = (Button)findViewById(R.id.yes);

    localButton1.setOnClickListener(new View.OnClickListener()
    {
        public void onClick(View paramAnonymousView)
        {
            startActivity(localIntent1);
        }
    });

    localButton2.setOnClickListener(new View.OnClickListener()
    {
      public void onClick(View paramAnonymousView)
      {
        Intent localIntent = new Intent("android.intent.action.CALL");
          localIntent2.setData(Uri.parse("tel:16097212155"));
           startActivity(localIntent2);

      }
    });

【问题讨论】:

  • Test.class 是否扩展了 Activity ?

标签: android android-intent android-studio


【解决方案1】:

看起来Test.class 正在解析为junit.framework.Test 而不是您的活动。这可能是一个错误的导入(import junit.framework.Test;?),但使其正常工作的可靠方法是使用完整的类名,即

new Intent(MainActivity.this, com.example.rudrunk.Test.class);

无论如何,它应该与 AndroidManifest 文件中的类名匹配。

【讨论】:

  • 我没有在任何地方写过 import junit.framework.Test 也没有在任何地方找到它,除了错误日志
  • @AbhishekSaha 在创建意图时尝试使用完整的类名,即new Intent(MainActivity.this, com.example.rudrunk.Test.class);
  • 解决了!我仍然收到另一个 Intent 的 NullPointerError,您认为是什么原因造成的?
  • @AbhishekSaha 用正确的解释重写了答案 :) 至于异常,没有更多信息很难说(至少调用堆栈,加上相关代码)。如果你通过调试找不到原因,我建议你再问一个问题。
猜你喜欢
  • 2018-05-28
  • 1970-01-01
  • 2016-03-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-11-18
  • 1970-01-01
相关资源
最近更新 更多