0

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

提前致谢, IDE

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

    }
});
}
}

但是,每当我在我的应用程序中进行此活动或单击按钮时,它都不会打开文件或敬酒吗?你现在为什么?

4

2 回答 2

4

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

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

于 2013-05-02T20:41:05.067 回答
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;
于 2013-05-02T20:39:04.043 回答