2013-05-02 90 views
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(); 
       } 
      } 

     } 
    }); 
} 
    } 

但在包含行:按钮OpenPDF =(按钮)findViewById(R.id.OpenPdfButton); OpenPDF.setOnClickListener(新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(); 
      } 
     } 

    } 
}); 
} 
} 

但每当我去这个活动中我的应用程序或点击按钮,它不会打开文件或给敬酒? 你现在为什么?

回答

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; 
+0

的代码是在'onOptionsItemSelected'方法。你的解释是不正确的。 – rgettman 2013-05-02 20:44:06

+0

@rgettman是的,我现在看到,谢谢。我想我看到了'return',并认为它是函数的结尾,并且他们忘了添加'}'并且当然''Button'通过我关闭。无论如何,我已经更新了我的答案 – codeMagic 2013-05-02 20:49:24

+0

你可以看看编辑吗? – Ide 2013-05-03 14:03:46

4

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

你的逻辑应使得return应该是它在发生块的最后一个语句。