无法访问代码错误

时间:2013-05-02 20:37:41

标签: java android eclipse error-handling

我正在创建一个名为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给了我一个错误:无法访问的代码 我不知道如何解决这个问题,所以我问你。 你知道怎么解决这个以及为什么会出现这个错误吗?

提前致谢, 井

P.S。我不是母语为英语的人,所以请看我的问题,而不是我的语法。

编辑:

我现在得到了以下有用的答案:

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

    }
});
}
}

但每当我在我的应用程序中进行此活动或单击按钮时,它将无法打开文件或提供吐司? 你现在为什么?

2 个答案:

答案 0 :(得分:4)

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

你的逻辑应该是return应该是它出现的块的最后一个语句。

答案 1 :(得分:1)

如果它不是选项按钮,则不需要在该方法中。如果它不是一个选项而是常规onCreate()

,请将其移至Button
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;