ActivityNotFoundException: No Activity found to ha

2019-07-19 16:04发布

问题:

I have a requirement to display pdf file from SD card. This pdf file is created after a network operation. I'm getting ActivityNotFoundException when trying to open pdf through activity.startActivity(intent); . Below is code folder_list_details.xml

  <?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
 <ListView 
    android:layout_height="wrap_content"
    android:layout_width="fill_parent" android:id="@+id/listV_main"/>
<ImageView 
    android:id="@+id/photo"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:paddingRight="15dp"
    android:paddingTop="10dp" />
        <TextView android:id="@+id/name"
    android:textSize="14sp" 
    android:textStyle="bold" 
    android:textColor="#000000" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content"
    android:layout_toRightOf="@id/photo"/>

      </RelativeLayout>

FolderActivity.java

    public class FolderListActivity extends ListActivity{

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    Log.i("FolderListActivity", "onCreate" );
    FolderDisplay folderObj = new FolderDisplay(FolderListActivity.this);
    folderObj.execute((Void) null);
     }
    } 

FolderDisplay.java :

     @Override
       protected void onPostExecute(Void v) {
    try{
                //activity is an  instance of FolderListActivity
        final ListView lv1 =activity.getListView(); 
        lv1.setOnItemClickListener(new OnItemClickListener() {
            @Override
      public void onItemClick(AdapterView<?> arg0, View v, int position,long id) {                           
            File file =Utility.getInstance().getFile();
                Intent intent = new Intent(Intent.ACTION_VIEW);
                 intent.setDataAndType(Uri.fromFile(file), "application/pdf");
                 intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                  activity.startActivity(intent);
            }  
        });
    }catch(Exception e){
        }
 }

Mainfest.xml

                  <?xml version="1.0" encoding="utf-8"?>
             <manifest xmlns:android="http://schemas.android.com/apk/res/android"
           package="com.example.testlogin"
           android:versionCode="1"
             android:versionName="1.0" >

      <uses-sdk
       android:minSdkVersion="11"
     android:targetSdkVersion="17" />
       <uses-permission android:name="android.permission.INTERNET"/>
     <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE">
       </uses-  permission>

      <application
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" 
    android:debuggable="true"
    >
    <activity
        android:name="com.example.testlogin.LoginActivity"
        android:label="@string/app_name"
        android:windowSoftInputMode="adjustResize|stateVisible" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
      </activity>
        <activity
        android:name="com.example.testlogin.RepoListActivity"
        android:label="@string/title_activity_repo_list" >
       </activity>
       <activity
        android:name="com.example.testlogin.FolderListActivity"
        android:label="@string/title_activity_folder_list" >
      </activity>
     </application>

     </manifest>

Error I'm getting

android.content.ActivityNotFoundException: No Activity found to handle Intent { act=android.intent.action.VIEW dat=file:///mnt/sdcard/Alfdata/testdata5.pdf typ=application/pdf flg=0x4000000 }

回答1:

That is because your device or emulator does not have an application capable of viewing a local PDF file.



回答2:

I faced the same problem apparently , Android does not have a stock PDF Viewer but as an alternative if you have the pdf up on the cloud then u can open it in a webView which opens up a google docs link pointing to your pdf url .

Look at this post here



回答3:

Whenever you start an intent, you would have to have the native app installed on the emulator to handle that intent. Ex. If you invoke an intent with Maps as the action, you would have to use the Google API's based emulator. By default, android emulator does not have a PDF reader. You could test this on a device with a PDF reader and it should work fine.

Pls mark the answer as correct if you think your issue has been resolved.