只要我点击按钮

时间:2019-10-06 14:14:03

标签: java android xml

我正在为我的项目构建一个应用程序,其中包含一个布局,该布局可打开相机并拍照,而其他则将文本转换为语音,每当我单击导航到文本转换为语音的按钮时,该应用程序即关闭我无法达到该布局

文本到语音代码在新项目中进行单独测试时效果很好,但是当通过导航按钮链接到按钮时,它将关闭

// java代码//

     public class Text extends AppCompatActivity {


     Button ak,bk;
     protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.text);
    ak=(Button)findViewById(R.id.cam);
    ak.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent i=new Intent(Text.this,Camera.class);
            startActivity(i);
        }
    });
    bk=(Button)findViewById(R.id.spe);
    bk.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent k=new Intent(Text.this,Speech.class);
            startActivity(k);
        }
    });
}

// xml代码//

 <RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"

    tools:context=".major.Text">
    <LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical">

    <Button
        android:id="@+id/spe"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"


        android:text="text to speech"
        android:textColor="#2976d5"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <Button
        android:id="@+id/cam"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"

        android:layout_marginStart="16dp"

        android:layout_marginTop="190dp"
        android:text="camera"
        android:textColor="#2976d5"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

</LinearLayout>
</RelativeLayout>

//文本到语音Java类的代码

public class Speech extends AppCompatActivity {
    Button b1;
    EditText ed1;
    TextToSpeech t1;

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ed1 = (EditText) findViewById(R.id.editText);
        b1 = (Button) findViewById(R.id.button);

        t1 = new TextToSpeech(getApplicationContext(), new TextToSpeech.OnInitListener() {
            @Override
            public void onInit(int status) {
                if (status != TextToSpeech.ERROR) {
                    t1.setLanguage(Locale.UK);
                }
            }
        });

        b1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String toSpeak = ed1.getText().toString();
                Toast.makeText(getApplicationContext(), toSpeak, Toast.LENGTH_SHORT).show();
                t1.speak(toSpeak, TextToSpeech.QUEUE_FLUSH,null);
            }
        });
    }

    public void onPause() {
        if (t1 != null) {
            t1.stop();
            t1.shutdown();
        }
        super.onPause();
    }
}

// logcat:

     2019-10-06 20:15:53.294 20959-21006/com.example.omen.loginmajor 
     E/eglCodecCommon: glUtilsParamSize: unknow param 0x000082da
     2019-10-06 20:15:53.294 20959-21006/com.example.omen.loginmajor 
     E/eglCodecCommon: glUtilsParamSize: unknow param 0x000082da

应用程序保持关闭状态,而不是转到另一个页面

1 个答案:

答案 0 :(得分:0)

也许您可以更改

  

Intent k = new Intent( Text.this ,Speech.class);

  

Intent k = new Intent( getApplicationContext(),Speech.class);