1

みんな私のアプリを実行する方法が見つかりません。助けてください。ここでjava - Androidスタジオ - アクティビティをインスタンス化できません

は誤りである:ここでは 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にエラーが表示されます。だから私は本当に私のアプリのための助けが必要です。誰かが私に提案を与えることができれば、それは大きな助けになるでしょう。おかげ

+1

質問に関連する文章を投稿してください。スクリーンショットを投稿しないでください。 – shmosel

+0

あなたは読めませんでしたか?私はスクリーンショットを使用してエミュレータのエラーも表示します –

+0

[質問するときにSOのコードをアップロードしてみませんか?](https://meta.stackoverflow.com/a/285557/1553851) – shmosel

答えて

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

super.onCreate()後まで、このようなfindViewById() —ようActivity —から継承されたメソッドを呼び出さないでください。

setContentView()の後など、Viewが存在するまでfindViewById()を呼び出しないでください。

変更その行がなければ:

EditText ed; 

setContentView(R.layout.activity_main);後に次の行を追加します。

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

私はそれを試して、私のPCが非常に遅いためにしばらく時間がかかることをお知らせします。 –

0

あなたはあなたの活動が作成される前でもエディットテキストにアクセスしようとすると上のレイアウトをレンダリングしています編集テキストが存在します。従ってNullPointerException

アクティビティについては、の後に、次の行をsetContentViewに移動してください。

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

あなたがEDはonCreate外で利用できるようにしたい場合は、唯一のEDを宣言し、後でonCreateにあなたはfindViewByIdを通じてそれに値を割り当てることができます。

関連する問題