gpt4 book ai didi

android - 按下按钮后应用程式当机,但 Action 正常

转载 作者:行者123 更新时间:2023-12-02 10:54:33 30 4
gpt4 key购买 nike

也许有人看到一个错误,问题是当我按btn2 (button 2)btn3 (button 3)应用程序crashes时,但操作仍然有效,即video正在运行并且PDF打开,而button 1正常工作...

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

configureImageButton();

mCamera = getCameraInstance();

mPreview = new CameraPreview(this, mCamera);
FrameLayout layout = (FrameLayout) findViewById(R.id.camera_View);
layout.addView(mPreview);


controlInflater = LayoutInflater.from(getBaseContext());
View view = getLayoutInflater().inflate(R.layout.activity_main, layout, false);
layout.addView(view);

}

public Camera getCameraInstance(){
Camera c = null;
try {
c = Camera.open();
c.setDisplayOrientation(90);
}
catch (Exception e){
}
return c;
}

@Override
protected void onPause() {
super.onPause();
mpAudio.release();
}

private void configureImageButton() {

ImageButton btn1 = (ImageButton) findViewById(R.id.imageButton);// audio button
ImageButton btn2 = (ImageButton) findViewById(R.id.imageButton2); // video button
ImageButton btn3= (ImageButton) findViewById(R.id.imageButton3);//reading button

btn1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
mpAudio = MediaPlayer.create(MainActivity.this,R.raw.tirepressuremonitoringsystem);
mpAudio.start();
}
}
);

btn2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
startActivity(new Intent(MainActivity.this, Activity2.class));
}
}
);

btn3.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
readPDF();
}
}
);
}

private void readPDF()
{
AssetManager assetManager = getAssets();

InputStream in = null;
OutputStream out = null;
File file = new File(getFilesDir(), "tirepressuremonitoringsystem3.pdf");
try
{
in = assetManager.open("tirepressuremonitoringsystem3.pdf");
out = openFileOutput(file.getName(), Context.MODE_WORLD_READABLE);

copyFile(in, out);
in.close();
in = null;
out.flush();
out.close();
out = null;
} catch (Exception e)
{
Log.e("tag", e.getMessage());
}

Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(
Uri.parse("file://" + getFilesDir() + "/tirepressuremonitoringsystem3.pdf"),
"application/pdf");

startActivity(intent);
}

private void copyFile(InputStream in, OutputStream out) throws IOException
{
byte[] buffer = new byte[1024];
int read;
while ((read = in.read(buffer)) != -1)
{
out.write(buffer, 0, read);
}
}

public void playSound () throws IOException {
AssetFileDescriptor afd = getAssets().openFd("tirepressuremonitoringsystem.mp3");
mpAudio = new MediaPlayer();
mpAudio.setDataSource(afd.getFileDescriptor(),afd.getStartOffset(),afd.getLength());
mpAudio.prepare();
mpAudio.start();

}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();

//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}

return super.onOptionsItemSelected(item);
}



}

最佳答案

如果不按btn1,则崩溃,因为如果不按btn1,mpAudio将为null。
然后当onPause调用时,mpAudio.release();将导致NullPointerException。

请注意:只要 Activity 未显示在屏幕上但仍在运行,就会调用onPause(在您的情况下,您将使用btn2,3启动其他 Activity ,然后它将被调用)。

请更正为

@Override
protected void onPause() {
super.onPause();
if(mpAudio!=null)
mpAudio.release();
}

玩得开心!

关于android - 按下按钮后应用程式当机,但 Action 正常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30932154/

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