java - 无法访问代码错误

标签 java android eclipse error-handling

我正在制作一个名为 ROME 的应用程序,关于罗马市。我有一个名为 eten 的 Activity ,意思是食物,我希望该 Activity 在打开时打开一个名为 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 给我一个错误:无法访问代码 我不知道如何解决这个问题,所以才来问你。 您知道如何解决这个问题以及为什么会出现此错误吗?

提前致谢, IDE

附注我的母语不是英语,所以请看我的问题,而不是我的语法。

编辑:

我现在得到以下内容以及您有用的答案:

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(); 
            }
        }

    }
});
}
}

但是每当我在应用程序中进入此 Activity 或单击按钮时,它都不会打开文件或提供 toast ? 现在你知道为什么吗?

最佳答案

此行之后 return super.onOptionsItemSelected(item); 就在 Button OpenPDF = (Button) findViewById(R.id.OpenPdfButton); 之前,函数将返回,因此下一行(以及之后的所有行)将不会被执行。

您的逻辑应该是这样的:return 应该是它出现的 block 的最后一个语句。

关于java - 无法访问代码错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16347280/

相关文章:

java - 使用 Commons 或 Guava 将文本文件放入 Java List<String>

android - React-Native:当应用程序离线(无互联网访问)时,无法使用 firestore 添加或设置()文档

eclipse - GitHub for Windows 推/pull ,导致 Eclipse 无限刷新循环

eclipse - 来自 WTP 的 Tomcat 忽略分配的端口

java - 有没有办法告诉java总是创建一个新实例而不是实习

Java:如何在没有异常的情况下停止服务器(关闭套接字)?

android - 清除后 TextInputLayout 不显示错误消息

java - 从 for 循环抛出错误实例化多个按钮

eclipse - 如何在 JBoss AS 7.1.3 中添加更新版本的 Hibernate

java - 如何使用 jackson 获取 json 响应?