【问题标题】:java - Android Studio - Unable to instantiate activityjava - Android Studio - 无法实例化活动
【发布时间】:2018-03-05 21:17:36
【问题描述】:

伙计们,我找不到运行我的应用程序的方法。请帮忙。

这是错误: enter image description here

这是我的 MainActivity 文件:

package com.example.aman.text_to_speech;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.speech.tts.TextToSpeech;
import android.widget.EditText;

import java.util.Locale;

public class MainActivity extends AppCompatActivity
{
    TextToSpeech tt;
    EditText ed = (EditText)findViewById(R.id.editText);

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

        tt = new TextToSpeech(getApplicationContext(), new TextToSpeech.OnInitListener()
        {
            public void onInit(int status)
            {
                if(status != TextToSpeech.ERROR)
                    tt.setLanguage(new Locale("hi", "IN"));
            }
        });
    }

    public void speak_my_text(View vv)
    {
        String string = ed.getText().toString();
        tt.speak(string,TextToSpeech.QUEUE_FLUSH,null,null);
    }
}

这是我的 AndroidManifest 文件:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.aman.text_to_speech">

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

这是我的应用文件:

apply plugin: 'com.android.application'

android {
    compileSdkVersion 25
    buildToolsVersion "26.0.1"
    defaultConfig {
        applicationId "com.example.aman.text_to_speech"
        minSdkVersion 21
        targetSdkVersion 25
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
}

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
        exclude group: 'com.android.support', module: 'support-annotations'
    })
    compile 'com.android.support:appcompat-v7:25.3.1'
    compile 'com.android.support.constraint:constraint-layout:1.0.2'
    testCompile 'junit:junit:4.12'
}

我没有收到我的语法错误,我只是在 logcat 中收到错误。所以伙计们,我真的需要我的应用程序的帮助。如果有人可以给我建议,那将是一个很大的帮助。谢谢

【问题讨论】:

  • 在您的问题中发布任何相关文本。不要发布屏幕截图。
  • 你看不懂吗??我也使用屏幕截图在模拟器上显示错误
  • amandeep,你试过我的回答了吗?
  • 很抱歉这么晚的回复,但它奏效了。谢谢

标签: java android android-studio android-activity main-activity


【解决方案1】:
EditText ed = (EditText)findViewById(R.id.editText);

super.onCreate() 之后,不要调用从Activity 继承的方法(例如findViewById())。

View 存在之前不要调用findViewById(),例如在setContentView() 之后。

将该行更改为:

EditText ed;

并在setContentView(R.layout.activity_main);之后添加这一行:

ed = (EditText)findViewById(R.id.editText);

【讨论】:

  • 我会试一试,因为我的电脑速度很慢,所以可能需要一段时间。
【解决方案2】:

您甚至在创建活动并呈现编辑文本所在的布局之前就尝试访问编辑文本。因此NullPointerException

onCreate 中的以下行移到setContentView 之后进行活动。

EditText ed = (EditText)findViewById(R.id.editText);

如果您希望 ed 在 onCreate 之外可用,则只需声明 ed,然后在 onCreate 中声明,您可以通过 findViewById 为其分配一个值。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-12-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多