로마에 관한 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();
}
}
}
});
}
}
하지만 내 응용 프로그램에서이 활동에 이동하거나 버튼을 클릭 할 때마다이 파일을 열거 나 토스트를 제공하지 않습니다? 지금 이유가 무엇입니까?
코드는'onOptionsItemSelected' 방법입니다. 귀하의 설명이 잘못되었습니다. – rgettman
@rgettman 네, 감사합니다. 나는'return'을보고 그것이 함수의 끝이라고 가정하고 그들은'}'을 추가하는 것을 잊어 버렸습니다. 그리고 자연스럽게'Button '을 저를 통해 빠져 나갔습니다. 어쨌든, 내 대답을 업데이트했습니다 – codeMagic
당신이 편집 좀 봐 주시겠습니까? – Ide