按下按钮后应用程序崩溃,但操作有效
App crashing after pushing button, but the action is working
也许有人看到错误,问题是当我推送 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 它会崩溃,因为不按它,mpAudio
将为空。
那么当onPause
调用时,mpAudio.release();
会引发NullPointerException。
请注意:当 activity 未显示在屏幕上但仍为 运行 时,将调用 onPause(在您的情况下,您使用 btn2,3 启动其他 activity 然后它将被调用)。
请更正为
@Override
protected void onPause() {
super.onPause();
if(mpAudio!=null)
mpAudio.release();
}
玩得开心!
也许有人看到错误,问题是当我推送 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 它会崩溃,因为不按它,mpAudio
将为空。
那么当onPause
调用时,mpAudio.release();
会引发NullPointerException。
请注意:当 activity 未显示在屏幕上但仍为 运行 时,将调用 onPause(在您的情况下,您使用 btn2,3 启动其他 activity 然后它将被调用)。
请更正为
@Override
protected void onPause() {
super.onPause();
if(mpAudio!=null)
mpAudio.release();
}
玩得开心!