java - Android Studio - 无法实例化活动
伙计们我无法找到运行我的应用程序的方式。请帮忙。java - Android Studio - 无法实例化活动
以下是错误: 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中得到错误。所以我真的需要帮助我的应用程序。如果有人能给我建议,这将是一个很大的帮助。由于
EditText ed = (EditText)findViewById(R.id.editText);
不叫继承的方法从Activity
—如findViewById()
—直到super.onCreate()
后。
不要致电findViewById()
,直到View
存在,例如setContentView()
之后。
把上面一行是:
EditText ed;
和setContentView(R.layout.activity_main);
后添加此行:
ed = (EditText)findViewById(R.id.editText);
我会尝试一下,并让你知道哪些可能需要一段时间,因为我的电脑速度很慢。 –
您试图访问你的活动创建甚至在编辑文字和呈现布局编辑文本存在。因此NullPointerException
。
将您的setContentView
作为活动后,在onCreate
后移动下面的行。
EditText ed = (EditText)findViewById(R.id.editText);
如果你想编可用外onCreate
则只有声明编辑,后来在onCreate
您可以通过findViewById
为它分配一个值。
在您的问题中发布任何相关的文字。不要张贴截图。 – shmosel
你无法阅读?我使用屏幕截图来显示模拟器上的错误 –
[为什么不上传代码的图像时提问?](https://meta.*.com/a/285557/1553851) – shmosel