[英]Unreachable Code error
我正在制作一个关于罗马的名为ROME的应用程序。 我有一个名为eten的活动,它意味着食物,我希望该活动在打开时打开一个名为etenlijst.pdf的pdf文件。
我得到以下代码:
package com.example.rome;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.MenuItem;
import android.support.v4.app.NavUtils;
import android.content.Intent;
import android.widget.Button;
import android.view.View;
import android.widget.Toast;
import android.net.Uri;
import java.io.File;
import android.content.ActivityNotFoundException;
public class Eten extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_eten);
// Show the Up button in the action bar.
// getActionBar().setDisplayHomeAsUpEnabled(true);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.activity_eten, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
// This ID represents the Home or Up button. In the case of this
// activity, the Up button is shown. Use NavUtils to allow users
// to navigate up one level in the application structure. For
// more details, see the Navigation pattern on Android Design:
//
// http://developer.android.com/design/patterns/navigation.html#up-vs-back
//
NavUtils.navigateUpFromSameTask(this);
return true;
}
return super.onOptionsItemSelected(item);
Button OpenPDF = (Button) findViewById(R.id.OpenPdfButton);
OpenPDF.setOnClickListener(new View.OnClickListener()
{
public void onClick(View v)
{
File pdfFile = new File("/ROME/Etenlijst.pdf");
if(pdfFile.exists())
{
Uri path = Uri.fromFile(pdfFile);
Intent pdfIntent = new Intent(Intent.ACTION_VIEW);
pdfIntent.setDataAndType(path, "application/pdf");
pdfIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
try
{
startActivity(pdfIntent);
}
catch(ActivityNotFoundException e)
{
Toast.makeText(Eten.this, "Installeer een geschikte applicatie om PDF's mee te openen", Toast.LENGTH_LONG).show();
}
}
}
});
}
}
但在包含以下内容的行中:Button OpenPDF =(Button)findViewById(R.id.OpenPdfButton); OpenPDF.setOnClickListener(new View.OnClickListener(){
Eclipse给了我一个错误:无法访问的代码我不知道如何解决这个问题,所以我问你。 您知道如何解决这个问题以及为什么会出现此错误吗?
预先感谢,井出
PS:我不是英语母语人士,所以请查看我的问题而不是语法。
编辑:
现在,我将为您提供有用的答案:
package com.example.rome;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.MenuItem;
import android.support.v4.app.NavUtils;
import android.content.Intent;
import android.widget.Button;
import android.view.View;
import android.widget.Toast;
import android.net.Uri;
import java.io.File;
import android.content.ActivityNotFoundException;
public class Eten extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_eten);
// Show the Up button in the action bar.
// getActionBar().setDisplayHomeAsUpEnabled(true);
/**** This looks like a good place for it *****/
Button OpenPDF = (Button) findViewById(R.id.OpenPdfButton);
OpenPDF.setOnClickListener(new View.OnClickListener()
{
public void onClick(View v)
{
File pdfFile = new File("/ROME/Etenlijst.pdf");
if(pdfFile.exists())
{
Uri path = Uri.fromFile(pdfFile);
Intent pdfIntent = new Intent(Intent.ACTION_VIEW);
pdfIntent.setDataAndType(path, "application/pdf");
pdfIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
try
{
startActivity(pdfIntent);
}
catch(ActivityNotFoundException e)
{
Toast.makeText(Eten.this, "Installeer een geschikte applicatie om PDF's mee te openen", Toast.LENGTH_LONG).show();
}
}
}
});
}
}
但是,每当我在应用程序中转到此活动或单击按钮时,它都不会打开文件或吐司吗? 你现在为什么呢?
在此行之后, return super.onOptionsItemSelected(item);
这恰好在Button OpenPDF = (Button) findViewById(R.id.OpenPdfButton);
,该函数将返回,因此将不执行下一行(及其后的所有行)。
您的逻辑应该这样, return
应该是它所在的块的最后一条语句。
如果它不是选项按钮,则不必在该方法中。 如果不是选项而是常规Button
请将其移至onCreate()
public class Eten extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_eten);
// Show the Up button in the action bar.
// getActionBar().setDisplayHomeAsUpEnabled(true);
/**** This looks like a good place for it *****/
Button OpenPDF = (Button) findViewById(R.id.OpenPdfButton);
OpenPDF.setOnClickListener(new View.OnClickListener()
{
public void onClick(View v)
{
File pdfFile = new File("/ROME/Etenlijst.pdf");
if(pdfFile.exists())
{
Uri path = Uri.fromFile(pdfFile);
Intent pdfIntent = new Intent(Intent.ACTION_VIEW);
pdfIntent.setDataAndType(path, "application/pdf");
pdfIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
try
{
startActivity(pdfIntent);
}
catch(ActivityNotFoundException e)
{
Toast.makeText(Eten.this, "Installeer een geschikte applicatie om PDF's mee te openen", Toast.LENGTH_LONG).show();
}
}
}
});
}
}
如果这是一个选择,那么使用您的switch
case (R.id.idOfThisOption):
//don't need onClick()...just put the functionality here
File pdfFile = new File("/ROME/Etenlijst.pdf");
if(pdfFile.exists())
{
Uri path = Uri.fromFile(pdfFile);
Intent pdfIntent = new Intent(Intent.ACTION_VIEW);
pdfIntent.setDataAndType(path, "application/pdf");
pdfIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
try
{
startActivity(pdfIntent);
}
catch(ActivityNotFoundException e)
{
Toast.makeText(Eten.this, "Installeer een geschikte applicatie om PDF's mee te openen", Toast.LENGTH_LONG).show();
}
}
break;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.