本人从git上找了一个PDF阅读器API,就是下面的这个class。
当本人在主界面用一个按钮点击start这个Activity的时候,它给本人报了如下错误。
android.content.ActivityNotFoundException: Unable to find explicit activity class {com.github.barteksc.sample/com.github.barteksc.sample.PDFViewActivity}; have you declared this activity in your AndroidManifest.xml?
本人的AndroidManifest.xml文件是这样的
<activity
android:name=”.PDFViewActivity_”
android:label=”@string/app_name” >
</activity>
本来class名字就是PDFViewActivity,AS给本人加了个_,说是这个不是component activity,是generated activity,必须加这个。
加上包名的方法也试过了,没有用。
更诡异的是,当本人把PDFViewActivity设为主界面启动的时候
<activity
android:name=”.PDFViewActivity_”
android:label=”@string/app_name” >
<intent-filter>
<action android:name=”android.intent.action.MAIN” />
<category android:name=”android.intent.category.LAUNCHER” />
</intent-filter>
</activity>
是启动成功的。
不知道怎么样解决?
package com.github.barteksc.sample; import android.content.Intent; import android.database.Cursor; import android.net.Uri; import android.provider.OpenableColumns; import android.support.v7.app.AppCompatActivity; import com.github.barteksc.pdfviewer.PDFView; import com.github.barteksc.pdfviewer.ScrollBar; import com.github.barteksc.pdfviewer.listener.OnPageChangeListener; import org.androidannotations.annotations.AfterViews; import org.androidannotations.annotations.EActivity; import org.androidannotations.annotations.NonConfigurationInstance; import org.androidannotations.annotations.OnActivityResult; import org.androidannotations.annotations.OptionsItem; import org.androidannotations.annotations.OptionsMenu; import org.androidannotations.annotations.ViewById; @EActivity(R.layout.activity_main) @OptionsMenu(R.menu.options) public class PDFViewActivity extends AppCompatActivity implements OnPageChangeListener { private static final String TAG = PDFViewActivity.class.getSimpleName(); private final static int REQUEST_CODE = 42; public static final String SAMPLE_FILE = "1_Introduction.pdf"; @ViewById PDFView pdfView; @ViewById ScrollBar scrollBar; @NonConfigurationInstance Uri uri; @NonConfigurationInstance Integer pageNumber = 1; String pdfFileName; @OptionsItem(R.id.pickFile) void pickFile() { Intent intent = new Intent(Intent.ACTION_GET_CONTENT); intent.setType("application/pdf"); startActivityForResult(intent, REQUEST_CODE); } @AfterViews void afterViews() { pdfView.setScrollBar(scrollBar); if (uri != null) { displayFromUri(uri); } else { displayFromAsset(SAMPLE_FILE); } setTitle(pdfFileName); } private void displayFromAsset(String assetFileName) { pdfFileName = assetFileName; pdfView.fromAsset(SAMPLE_FILE) .defaultPage(pageNumber) .onPageChange(this) .swipeVertical(true) .showMinimap(false) .load(); } private void displayFromUri(Uri uri) { pdfFileName = getFileName(uri); pdfView.fromUri(uri) .defaultPage(pageNumber) .onPageChange(this) .swipeVertical(true) .showMinimap(false) .load(); } @OnActivityResult(REQUEST_CODE) public void onResult(int resultCode, Intent intent) { if (resultCode == RESULT_OK) { uri = intent.getData(); displayFromUri(uri); } } @Override public void onPageChanged(int page, int pageCount) { pageNumber = page; setTitle(String.format("%s %s / %s", pdfFileName, page, pageCount)); } public String getFileName(Uri uri) { String result = null; if (uri.getScheme().equals("content")) { Cursor cursor = getContentResolver().query(uri, null, null, null, null); try { if (cursor != null && cursor.moveToFirst()) { result = cursor.getString(cursor.getColumnIndex(OpenableColumns.DISPLAY_NAME)); } } finally { if (cursor != null) { cursor.close(); } } } if (result == null) { result = uri.getLastPathSegment(); } return result; } @Override public void onBackPressed() { Intent intent = new Intent(PDFViewActivity.this , Main.class); //启动intent对应的Activity startActivity(intent); super.onBackPressed(); } }
35
5
假如你把PDFViewActivity设为主界面启动的时候
<activity
android:name=”.PDFViewActivity”
android:label=”@string/app_name” >
<intent-filter>
<action android:name=”android.intent.action.MAIN” />
<category android:name=”android.intent.category.LAUNCHER” />
</intent-filter>
</activity>
这么写没有问题
但是假如你不设他为主页面启动的时候,(假设这时候主页面为MainActivity)
你就要这么写
<activity
android:name=”.MainActivity”
android:label=”@string/app_name” >
<intent-filter>
<action android:name=”android.intent.action.MAIN” />
<category android:name=”android.intent.category.LAUNCHER” />
</intent-filter>
</activity>
<activity
android:name=”.PDFViewActivity”
android:label=”@string/PDFViewActivity” >
</activity>
红字的这个字段PDFViewActivity随意写,就在res-values-strings这里写
例如:<string name=”title_activity_PDFViewActivity”>PDFViewActivity</string>