2012-10-18 94 views
7

我完全不熟悉Android SDK开发。我试图从AIR的本机扩展(在Android上)启动Adobe Reader。从适用于Android的Adobe AIR本机扩展启动活动

这是我做的(我确实按照这个教程:http://www.adobe.com/devnet/air/articles/extending-air.html)。

我有一个控制器:

package com.tokom.adobereader 
{ 
import com.tokom.adobereader.events.AdobeReaderEvent; 

import flash.events.EventDispatcher; 
import flash.events.StatusEvent; 
import flash.external.ExtensionContext; 

/** 
* A controller used to interact with the system volume on iOS and 
* Android devices. Ways to change the volume programmatically 
* and to respond to the hardware volume buttons are included. 
* 
* @author Nathan Weber 
*/ 
public class AdobeReaderController extends EventDispatcher 
{ 
    //---------------------------------------- 
    // 
    // Variables 
    // 
    //---------------------------------------- 

    private static var _instance:AdobeReaderController; 
    private var extContext:ExtensionContext; 


    //---------------------------------------- 
    // 
    // Public Methods 
    // 
    //---------------------------------------- 

    public static function get instance():AdobeReaderController { 
     if (!_instance) { 
      _instance = new AdobeReaderController(new SingletonEnforcer()); 
      _instance.init(); 
     } 

     return _instance; 
    } 



    public function openPdf(path:String):void 
    { 
     trace("[AdobeReaderController.as] openPdf "+path); 
     var ret = extContext.call("openPdf", path); 
     trace("ret = "+ret); 
    } 

    /** 
    * Cleans up the instance of the native extension. 
    */  
    public function dispose():void { 
     extContext.dispose(); 
    } 

    //---------------------------------------- 
    // 
    // Handlers 
    // 
    //---------------------------------------- 

    private function init():void { 
     trace("[AdobeReaderController.as] init"); 
     extContext.call("init"); 
    } 

    //---------------------------------------- 
    // 
    // Handlers 
    // 
    //---------------------------------------- 

    private function onStatus(event:StatusEvent):void { 
     //systemVolume = Number(event.level); 
     //dispatchEvent(new VolumeEvent(VolumeEvent.VOLUME_CHANGED, systemVolume, false, false)); 
    } 

    //---------------------------------------- 
    // 
    // Constructor 
    // 
    //---------------------------------------- 

    /** 
    * Constructor. 
    */  
    public function AdobeReaderController(enforcer:SingletonEnforcer) { 
     super(); 

     extContext = ExtensionContext.createExtensionContext("com.tokom.adobereader", ""); 

     if (!extContext) { 
      trace("Adobe Reader native extension is not supported on this platform."); 
      throw new Error("Adobe Reader native extension is not supported on this platform."); 
     } 

     //extContext.addEventListener(StatusEvent.STATUS, onStatus); 
    } 
} 
} 

class SingletonEnforcer { 

} 

这里是我的openPdf()函数:

package com.tokom.adobereader.functions; 

import java.io.File; 

import android.app.Activity; 
import android.content.ActivityNotFoundException; 
import android.content.Context; 
import android.content.Intent; 
import android.media.AudioManager; 
import android.net.Uri; 
import android.util.Log; 

import com.adobe.fre.FREContext; 
import com.adobe.fre.FREFunction; 
import com.adobe.fre.FREObject; 

public class OpenPdfFunction extends Activity implements FREFunction 
{ 
public static final String TAG = "OpenPdfFunction"; 

public FREObject call(FREContext context, FREObject[] args) 
{ 
    Log.d(TAG, "open pdf = "); 

    String filePath = null; 
    try 
    { 
     filePath = args[0].getAsString(); 
     Log.d(TAG, filePath); 
    } catch (Exception e) { 
     // TODO 
    } 
    Log.d(TAG, "trying now to open adobeReader"); 
    try 
    { 
     Intent intent = new Intent(); 

     //intent.setClassName("com.adobe.reader", "com.adobe.reader.AdobeReader"); 
     intent.setAction(Intent.ACTION_VIEW); 
     intent.setDataAndType(Uri.fromFile(new File(filePath)), "application/pdf"); 
     intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 
     Log.d(TAG, "about to startActivity"); 
     startActivity(intent); 
    } 
    catch (ActivityNotFoundException activityNotFoundException) 
    { 
     Log.d(TAG, "cannot start activity"); 
     activityNotFoundException.printStackTrace(); 
    } 
    catch (Exception otherException) 
    { 
     otherException.printStackTrace(); 
     Log.d(TAG, "cannot start activity"); 
    } 
    Log.d(TAG, "activity should have started"); 
    return null; 
} 
} 

的问题是,在运行时,我得到一个NPE(上startActivity()):

10-18 18:31:44.160: I/InputReader(289): dispatchTouch::touch event's action is 0, pending(waiting finished signal)=0 
10-18 18:31:44.160: I/InputDispatcher(289): Delivering touch to current input target: action: 0, channel '40b0d0b0 air.testMyANE.debug/air.testMyANE.debug.AppEntry (server)' 
10-18 18:31:44.160: I/InputDispatcher(289): Delivering touch to current input target: action: 0, channel 'TouchIntercepter (server)' 
10-18 18:31:44.280: I/InputReader(289): dispatchTouch::touch event's action is 1, pending(waiting finished signal)=0 
10-18 18:31:44.280: I/InputDispatcher(289): Delivering touch to current input target: action: 1, channel '40b0d0b0 air.testMyANE.debug/air.testMyANE.debug.AppEntry (server)' 
10-18 18:31:44.280: I/InputDispatcher(289): Delivering touch to current input target: action: 1, channel 'TouchIntercepter (server)' 
10-18 18:31:44.310: D/AdobeReaderExtension(28990): Extension initialized. 
10-18 18:31:44.310: I/air.testMyANE.debug(28990): [AdobeReaderController.as] init 
10-18 18:31:44.320: I/InitFunction(28990): in init 
10-18 18:31:44.320: I/air.testMyANE.debug(28990): [AdobeReaderController.as] openPdf /sdcard/Download/test.pdf 
10-18 18:31:44.320: D/OpenPdfFunction(28990): open pdf = 
10-18 18:31:44.320: D/OpenPdfFunction(28990): /sdcard/Download/test.pdf 
10-18 18:31:44.320: D/OpenPdfFunction(28990): trying now to open adobeReader 
10-18 18:31:44.320: D/OpenPdfFunction(28990): about to startActivity 
10-18 18:31:44.320: W/System.err(28990): java.lang.NullPointerException 
10-18 18:31:44.320: W/System.err(28990): at android.app.Activity.startActivityForResult(Activity.java:3095) 
10-18 18:31:44.320: W/System.err(28990): at android.app.Activity.startActivity(Activity.java:3201) 
10-18 18:31:44.320: W/System.err(28990): at com.tokom.adobereader.functions.OpenPdfFunction.call(OpenPdfFunction.java:69) 
10-18 18:31:44.320: W/System.err(28990): at com.adobe.air.customHandler.nativeOnTouchCallback(Native Method) 
10-18 18:31:44.320: W/System.err(28990): at com.adobe.air.customHandler.nativeOnTouchCallback(Native Method) 
10-18 18:31:44.320: W/System.err(28990): at com.adobe.air.customHandler.handleMessage(customHandler.java:27) 
10-18 18:31:44.320: W/System.err(28990): at android.os.Handler.dispatchMessage(Handler.java:99) 
10-18 18:31:44.320: W/System.err(28990): at android.os.Looper.loop(Looper.java:132) 
10-18 18:31:44.320: W/System.err(28990): at android.app.ActivityThread.main(ActivityThread.java:4028) 
10-18 18:31:44.320: W/System.err(28990): at java.lang.reflect.Method.invokeNative(Native Method) 
10-18 18:31:44.320: W/System.err(28990): at java.lang.reflect.Method.invoke(Method.java:491) 
10-18 18:31:44.320: W/System.err(28990): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:844) 
10-18 18:31:44.320: W/System.err(28990): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:602) 
10-18 18:31:44.320: W/System.err(28990): at dalvik.system.NativeStart.main(Native Method) 

我相信我尝试启动adobe reader的方式不是很好,但是我做错了什么?

在此先感谢您的帮助!

Zab

回答

9

好的,我有我的答案。

而是在com.tokom.adobereader.functions.OpenPdfFunction扩大活动的,我所做的:

Context appContext = context.getActivity().getApplicationContext(); 

// (...) 

appContext.startActivity(intent); 

现在的作品。

下面是完整的代码:

package com.tokom.adobereader.functions; 

import java.io.File; 

import android.content.ActivityNotFoundException; 
import android.content.Context; 
import android.content.Intent; 
import android.net.Uri; 
import android.util.Log; 

import com.adobe.fre.FREContext; 
import com.adobe.fre.FREFunction; 
import com.adobe.fre.FREObject; 

public class OpenPdfFunction implements FREFunction 
{ 
public static final String TAG = "OpenPdfFunction"; 

public FREObject call(FREContext context, FREObject[] args) 
{ 
    Context appContext = context.getActivity().getApplicationContext(); 

    Log.d(TAG, "open pdf = "); 

    String filePath = null; 
    try 
    { 
     filePath = args[0].getAsString(); 
     Log.d(TAG, filePath); 
    } catch (Exception e) { 
     // TODO 
    } 
    Log.d(TAG, "trying now to open adobeReader"); 
    try 
    { 
     Intent intent = new Intent(); 

     intent.setClassName("com.adobe.reader", "com.adobe.reader.AdobeReader"); 
     intent.setAction(Intent.ACTION_VIEW); 
     intent.setDataAndType(Uri.fromFile(new File(filePath)), "application/pdf"); 
     intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 
     Log.d(TAG, "about to startActivity"); 
     appContext.startActivity(intent); 
    } 
    catch (ActivityNotFoundException activityNotFoundException) 
    { 
     Log.d(TAG, "cannot start activity"); 
     activityNotFoundException.printStackTrace(); 
    } 
    catch (Exception otherException) 
    { 
     otherException.printStackTrace(); 
     Log.d(TAG, "cannot start activity"); 
    } 
    Log.d(TAG, "activity should have started"); 
    return null; 
} 
} 

希望这将有助于其他任何AIR开发人员在他与Android原生扩展的第一步。

干杯!

相关问题