gpt4 book ai didi

java - 每当我点击按钮时 Android 应用程序就会关闭

转载 作者:行者123 更新时间:2023-12-01 23:41:16 27 4
gpt4 key购买 nike

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

文本到语音代码在新项目中单独测试时工作正常,但当通过按钮链接为导航页面时它会关闭

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

应用程序不断关闭而不是转到另一个页面

最佳答案

也许你可以改变

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

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

关于java - 每当我点击按钮时 Android 应用程序就会关闭,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58258091/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com