我正在嘗試創建一個Android應用程序來打開PDF文件。目標是能夠通過電子郵件發送並以數字方式對其進行簽名。我只是想通過按下按鈕來打開文件。啓動android應用程序
應用程序啓動,但我得到了以下錯誤:
The application "PdfSigner" has stopped unexpectedly. please try again
activitymain.xml:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".PdfActivity" >
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="168dp"
android:text="@string/Ouvrir" />
</RelativeLayout>
Activity.java:
package com.example.pdfsigner;
import ltm.test.R;
import android.app.Activity;
import android.content.ActivityNotFoundException;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;
public class PdfActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activitymain);
//
Button b1 = (Button)findViewById(R.id.button1);
b1.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
String surl = "/ENGLISH.pdf";
String apptype = "application/pdf";
if(openURLWithType(surl, apptype) == false)
Toast.makeText(PdfActivity.this,
"Echec de Open URL", Toast.LENGTH_LONG).show();
}});
}
public boolean openURLWithType(String url, String type) {
Uri uri = Uri.parse("android.resource://" + getPackageName() + "/R.raw.ENGLISH");
Intent intent = new Intent(Intent.ACTION_VIEW, uri);
intent.setDataAndType(uri, type);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
try {
PdfActivity.this.startActivity(intent);
return true;
} catch (ActivityNotFoundException e) {
Log.e("LTM" ,"Activity not found: " + url, e);
}
return false;
}
}
清單:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="ltm.test"
android:versionCode="1"
android:versionName="1.0">
<uses-sdk android:minSdkVersion="7"
android:targetSdkVersion="7" />
<uses-permission android:name="android.permission.INTERNET" />
<application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name">
<activity android:name=".PdfActivity"
android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
任何幫助或批評會受到歡迎
請編輯您的答案,並從Logcat添加堆棧跟蹤 – mt0s