因此,如果我使用“下一步”或“上一步”按钮来更改已加载的内容,并使用tts来读取内容,那么下面的代码就可以正常工作。如果我使用这个手势来更改progressL1 int的值,那么整个应用程序就会崩溃,tts永远无法工作。奇怪的行为是,如果我按下下一步(或上一步)按钮,然后在tts工作正常且应用程序没有崩溃后使用手势。
我认为可能会发生的是,如果我只是使用手势来推进内容,onInit()永远不会被调用,但我不知道如何解决这个问题。我是新来的java和android开发,所以已经撞上了这堵墙,不知道如何进步。
代码是:
import java.io.IOException;
import java.io.InputStream;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.gesture.GestureOverlayView;
import android.gesture.GestureOverlayView.OnGestureListener;
import android.graphics.drawable.Drawable;
import android.view.GestureDetector;
import android.view.Gravity;
import android.view.Menu;
import android.view.MenuItem;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
import android.speech.tts.TextToSpeech;
import android.speech.tts.TextToSpeech.OnInitListener;
import android.support.v4.app.NavUtils;
import android.text.method.ScrollingMovementMethod;
public class Lessonsinglegroup extends Activity implements OnClickListener, OnInitListener, android.view.GestureDetector.OnGestureListener {
//define the image and text view for use later
ImageView Image;
TextView Text;
//define the texttospeak stuff
private int MY_DATA_CHECK_CODE = 0;
public int progressL1 = 0;
private TextView inputText;
private TextToSpeech tts;
private ImageButton speakButton;
private ImageButton nextButton;
private ImageButton previousButton;
private Button babout;
private Button bhome;
//deal with the swipe to advance
private GestureDetector gDetector;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_lessonsinglegroup);
getActionBar().setDisplayHomeAsUpEnabled(true);
//link the image and text boxes to the xml
Image = (ImageView)findViewById(R.id.image);
Text = (TextView)findViewById(R.id.text);
loadDataFromAsset(progressL1);
gDetector = new GestureDetector(this);
//finish with the asset load
//define tts stuff
inputText = (TextView) findViewById(R.id.text);
inputText.setMovementMethod(new ScrollingMovementMethod());
speakButton = (ImageButton) findViewById(R.id.ibtalk);
nextButton = (ImageButton) findViewById(R.id.ibnext);
previousButton = (ImageButton) findViewById(R.id.ibprevious);
babout = (Button)findViewById(R.id.babout); //about button
bhome = (Button)findViewById(R.id.bhome); //about button
//new onclick listener style
speakButton.setOnClickListener(this);
nextButton.setOnClickListener(this);
previousButton.setOnClickListener(this);
babout.setOnClickListener(this);
bhome.setOnClickListener(this);
}
public void onClick(View v) {
if (v==babout) {
Intent startabout = new Intent(Lessonsinglegroup.this, AboutScreen.class); //this is about screen
startActivity(startabout);
}
if (v==bhome) {
Intent startabout = new Intent(Lessonsinglegroup.this, Main.class); //this is main screen
startActivity(startabout);
}
if (v==nextButton && progressL1==10) {
Toast toast = Toast.makeText(Lessonsinglegroup.this,
"End of Lesson", Toast.LENGTH_LONG);
toast.setGravity(Gravity.CENTER,0,0);
LinearLayout toastview=(LinearLayout) toast.getView();
ImageView imageCodeProject = new ImageView(getApplicationContext());
imageCodeProject.setImageResource(R.drawable.lessoncomplete); //logo name
toastview.addView(imageCodeProject, 0);
toast.show();
}
if (v==nextButton && progressL1<10) {
progressL1++;
//toast not required now its working
// Toast.makeText(Lessonsinglegroup.this, "progress +1", Toast.LENGTH_LONG).show();
Lessonsinglegroup.this.loadDataFromAsset(progressL1);
}
if (v==previousButton) {
progressL1--;
//toast not required now its working
// Toast.makeText(Lessonsinglegroup.this, "progress +1", Toast.LENGTH_LONG).show();
Lessonsinglegroup.this.loadDataFromAsset(progressL1);
}
if (v==speakButton){
String text = inputText.getText().toString();
if (text!=null && text.length()>0) {
tts.speak(text, TextToSpeech.QUEUE_ADD, null);
}
};
Intent checkIntent = new Intent();
checkIntent.setAction(TextToSpeech.Engine.ACTION_CHECK_TTS_DATA);
startActivityForResult(checkIntent, MY_DATA_CHECK_CODE);
}
//gesture handling down here
public boolean onDown(MotionEvent arg0) {
// TODO Auto-generated method stub
return false;
}
public boolean onFling(MotionEvent start, MotionEvent finish, float xVelocity, float yVelocity) {
if (start.getRawX() > finish.getRawX() && (progressL1<10)) {
//swipe left action here
progressL1++;
Lessonsinglegroup.this.loadDataFromAsset(progressL1);
}
if (start.getRawX() > finish.getRawX() && (progressL1==10)) {
Toast toast = Toast.makeText(Lessonsinglegroup.this,
"End of Single Group Lesson", Toast.LENGTH_LONG);
toast.setGravity(Gravity.CENTER,0,0);
LinearLayout toastview=(LinearLayout) toast.getView();
ImageView imageCodeProject = new ImageView(getApplicationContext());
imageCodeProject.setImageResource(R.drawable.lessoncomplete); //logo name
toastview.addView(imageCodeProject, 0);
toast.show();
}
if (start.getRawX() < finish.getRawX()) {
//swipe rightaction here
progressL1--;
Lessonsinglegroup.this.loadDataFromAsset(progressL1);
}
return true;
}
public void onLongPress(MotionEvent arg0) {
// TODO Auto-generated method stub
}
public boolean onScroll(MotionEvent arg0, MotionEvent arg1, float arg2, float arg3) {
// TODO Auto-generated method stub
return false;
}
public void onShowPress(MotionEvent arg0) {
// TODO Auto-generated method stub
}
public boolean onSingleTapUp(MotionEvent arg0) {
// TODO Auto-generated method stub
return false;
}
public boolean onTouchEvent(MotionEvent me) {
return gDetector.onTouchEvent(me);
}
//end of gesture based advance
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == MY_DATA_CHECK_CODE) {
if (resultCode == TextToSpeech.Engine.CHECK_VOICE_DATA_PASS) {
//sucess with TTS create it
tts = new TextToSpeech(this, this);
}
else {
//missing TTS so install it
Intent installIntent = new Intent();
installIntent.setAction(TextToSpeech.Engine.ACTION_INSTALL_TTS_DATA);
startActivity(installIntent);
}
}
}
public void onInit(int status) {
if (status == TextToSpeech.SUCCESS) {
Toast.makeText(Lessonsinglegroup.this, "Text to Speech initialised", Toast.LENGTH_SHORT).show();
}
else if (status == TextToSpeech.ERROR) {
Toast.makeText(Lessonsinglegroup.this, "Error starting Text to Speech", Toast.LENGTH_LONG).show();
}
}
//actually load the text file and image file
public void loadDataFromAsset(int progressL1) {
//load the asset files themselves
try {
InputStream is = getAssets().open("lessons/singlegp/" + progressL1 + ".txt");
//check file size
int size = is.available();
//create a buffer to handle it
byte[] buffer = new byte[size];
//send the data to the buffer
is.read(buffer);
//close the stream down
is.close();
//set the text we recovered to the TextView
Text.setText(new String(buffer));
}
catch (IOException ex) {
return;
}
//image file next
try {
InputStream ims = getAssets().open("lessons/singlegp/" + progressL1 + ".jpg");
//load the image as drawable
Drawable d = Drawable.createFromStream(ims, null);
//set the drawable image to the imageview
Image.setImageDrawable(d);
}
catch (IOException ex) {
return;
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_lessonsinglegroup, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
NavUtils.navigateUpFromSameTask(this);
return true;
}
return super.onOptionsItemSelected(item);
}
}
我不确定这是否是最好的方式来编码我想要的,但它的工作原理除了TTS时,只有手势被用来改变进度L1值。不知道为什么,请帮助!
谢谢。
你的日志怎么说?在我的情况下,我有手势监听器的问题,因为它以某种方式使事件队列,和事件开始堆积起来,如果你做手势真的很快,当android试图运行所有的事件它给一个崩溃,由于我认为运行内存溢出。所以检查logcat并复制粘贴错误日志。如果这是我的错误,也许我可以帮忙。
每当我单击登录页面中的“注册”按钮时,应用程序就会崩溃,无法移动到下一个活动。我已经在这个问题上纠缠了一天,似乎在任何地方都找不到解决办法。 下面是我为注册按钮方法编写的代码。(和是文本输入布局) Logcat在第7行显示错误,即ref.child(…行)。我尝试了第7行的替代方法,如:
我使用的是IntelliJIDEA CE edition(11.0.4)的最新版本。有一件事我在任何地方都找不到,它阻碍了我对Java/Spring的进一步改进。 控制器: 类:导入com.example.demo.interfaces.ISave; 类接口: 没什么特别的,只是为了掌握Spring和Java。 在我的控制器里,当我试图使用 我收到一个阻止应用启动的错误。以下是错误: 任务应用程序
E/AndroidRuntime:致命异常:主进程:com。维内特。计算器,PID:29435 java。lang.NullPointerException:尝试调用虚拟方法的布尔java。朗,布尔<代码>在此输入代码booleanValue()”在com的空对象引用上。维内特。计算器主要活动16美元。android上的onClick(MainActivity.java:177)。看法看法andr
在这个孩子的acitivy我有一个页面有一个旋转器和一个按钮。 首先,按钮是工作的,但当我点击旋转器,应用程序将崩溃。为了解决这个问题,我必须改变 至 我在活动中得到了这个方法,但他没有找到这个方法,因为我查看了contentView=LayoutInflater.from(getParent()).Inflate(r.layout.show_add_expresse_event,null);se
我使用getStringArrayListExtra()搜索将列表从一个活动传输到另一个活动。这在第一次(从MainActivity到Diag2Activity)时运行良好,但在第二次(从Diag2Activity到SSToActivity)时应用程序崩溃。 每次我使用相同的方法:第一次活动: 第二项活动: 谁能告诉我错误可能来自哪里?提前谢谢 请在下面找到完整的代码: 主要活动。JAVA } 主
问题内容: 当我退出“活动”退出(通过“后退”按钮)时,我的应用程序崩溃。据我所知,这是在Android代码库中发生的,而不是我的,但是我并不完全相信。 这是来自adb的stacktrace: 有人有什么想法/建议吗? 问题答案: 无助,因为没有代码。还在看 造成原因:android.app.Activity.performStop(Activity.java:3575)上的java.lang.N