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 で次のエラーが表示されます: Unreachable Code これを解決する方法がわかりません。これを解決する方法と、このエラーが発生する理由を知っていますか?

前もってありがとう、井出

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 に答える