无法解析符号setDataAndType和setFlags

时间:2016-08-03 04:09:16

标签: android class android-studio syntax-error symbols

此代码来自另一篇文章:Android open pdf file

这是我在1个类中的所有代码,名为openFile.class

package com.test.android;


import android.content.ActivityNotFoundException;
import android.content.Intent;
import android.net.Uri;
import android.os.Environment;

import java.io.File;

public class openFile {

  String namafile ;
  public openFile(String namafile){
    this.namafile = namafile;
  }

  File file = new
  File(Environment.getExternalStorageDirectory().getAbsolutePath() +"/"+   
  namafile);
  Intent target = new Intent(Intent.ACTION_VIEW);
  target.setDataAndType(Uri.fromFile(file),"application/pdf");
  target.setFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);

  Intent intent = Intent.createChooser(target, "Open File");
    try {
      startActivity(intent);
    } catch (ActivityNotFoundException e) {
    // Instruct the user to install a PDF reader here, or something
   }

}

任何建议都将被接受

1 个答案:

答案 0 :(得分:0)

评论中建议的@MikeM.

  

您从该答案中获得的所有代码都需要在方法中。