1

Guys I can not find a way to run my app. Please help.

Here is the error: enter image description here

Here is my MainActivity file:

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);
    }
}

Here is my AndroidManifest file:

<?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>

Here is my app file:

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'
}

Im not getting my syntax error, im only getting the error in the logcat. So guys I really need help for my app. If anyone can give me suggestions, it will be a great help. Thanks

2 Answers2

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

Do not call inherited methods from Activity — such as findViewById() — until after super.onCreate().

Do not call findViewById() until the View exists, such as after setContentView().

Change that line to be:

EditText ed;

and add this line after setContentView(R.layout.activity_main);:

ed = (EditText)findViewById(R.id.editText);
CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
  • I'll try that and let you know which may take a while because my PC is very slow. –  Sep 25 '17 at 00:43
0

You are trying to access an edit text even before your activity is created and rendered the layout on which the edit text exists. Hence NullPointerException.

Move the below line in onCreate after you setContentView for the activity.

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

If you want ed to be available outside onCreate then only declare ed and later in onCreate you can assign it a value through findViewById.

JRG
  • 4,037
  • 3
  • 23
  • 34