2013-05-02 4 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() {

이클립스 나에게 오류 제공 : 도달 할 수없는 코드 내가 어떻게이 문제를 해결하는 방법 아무 생각을, 그래서 당신을 요구하고 당신이이 문제를 해결하는 방법을 알고 말고. ?이 오류가

사전에

감사합니다, 이데

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

옵션 버튼이 아닌 경우 해당 메소드에있을 필요는 없습니다. 이 옵션은 다음 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

+0

@rgettman 네, 감사합니다. 나는'return'을보고 그것이 함수의 끝이라고 가정하고 그들은'}'을 추가하는 것을 잊어 버렸습니다. 그리고 자연스럽게'Button '을 저를 통해 빠져 나갔습니다. 어쨌든, 내 대답을 업데이트했습니다 – codeMagic

+0

당신이 편집 좀 봐 주시겠습니까? – Ide

4

바로 Button OpenPDF = (Button) findViewById(R.id.OpenPdfButton); 앞에있는이 라인 return super.onOptionsItemSelected(item); 후 사용의 경우에 선택이 아닌 일반 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(); 
       } 
      } 

     } 
    }); 
} 
} 

되지 않은 경우, onCreate()로 이동 함수가 반환 될 것이므로 다음 줄 (및 그 이후의 모든 줄)은 실행되지 않습니다.

당신의 논리는 return 그것에서 발생하는 블록의 마지막 문이어야한다 것이어야한다.