1

描画アプリを作成していますが、画像を共有したいと思います。しかし、私はそのstartActivity部分のエラーに遭遇し、そのエラーはを書き込みますThe method startActivity(Intent) is undefined for the type。それはどういう意味で、どのように取り組むことができますか?よろしくお願いします!!!

編集:

コードの詳細なコード設定を投稿しました:ActivityACallingin 。shareImage()PaintView

このようなコンテキストで問題がないかどうかわかりませんか?この新しく追加された共有機能を除いて、共有なしのコードは非常にスムーズに実行されます。

PaintView

// the main screen that is painted
public class PaintView extends View 
{      
   Context context_new;       
   private boolean isFileAlreadySaved = false;
   String savedFilePath = "";

   private static final float TOUCH_TOLERANCE = 10;
   // other declarations here

   // PaintView constructor initializes the PaintView
   public PaintView(Context context, AttributeSet attrs) 
   {
      super(context, attrs); // pass context to View's constructor
      this.context_new=context;
      paintScreen = new Paint(); // used to display bitmap onto screen

      // set the initial display settings for the painted line
      paintLine = new Paint();
      paintLine.setAntiAlias(true); // smooth edges of drawn line
      paintLine.setColor(Color.BLACK); // default color is black
      paintLine.setStyle(Paint.Style.STROKE); // solid line
      paintLine.setStrokeWidth(5); // set the default line width
      paintLine.setStrokeCap(Paint.Cap.ROUND); // rounded line ends
      pathMap = new HashMap<Integer, Path>();
      previousPointMap = new HashMap<Integer, Point>();
   } // end DoodleView constructor


   public void shareImage()
   {
        Intent share;
        File attachment = null;

        if(isFileAlreadySaved == true)
        {
            attachment = new File(savedFilePath);
            boolean isFileThere = attachment.exists();
            if (isFileThere == true)
            {
                share = new Intent(Intent.ACTION_SEND);
                share.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                share.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(attachment));
                share.setType("image/png");
                startActivity(Intent.createChooser(share, "Share drawing"));
            }
        }
        else
        {
            Toast.makeText(getContext(), "Please save the image first...", Toast.LENGTH_LONG).show();    
        };
   };

アクティビティA:

   public OnClickListener shareButtonListener = new OnClickListener()   
   {
      @Override
      public void onClick(View v) 
      {        
        vibrate();
        PaintView.shareImage(ActivtyA.this);
      };
   };

Logcat:

02-02 16:01:58.230: E/AndroidRuntime(9809):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1992)
02-02 16:01:58.230: E/AndroidRuntime(9809):     at android.app.ActivityThread.access$600(ActivityThread.java:127)
02-02 16:01:58.230: E/AndroidRuntime(9809):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1158)
02-02 16:01:58.230: E/AndroidRuntime(9809):     at android.os.Handler.dispatchMessage(Handler.java:99)
02-02 16:01:58.230: E/AndroidRuntime(9809):     at android.os.Looper.loop(Looper.java:137)
02-02 16:01:58.230: E/AndroidRuntime(9809):     at android.app.ActivityThread.main(ActivityThread.java:4511)
02-02 16:01:58.230: E/AndroidRuntime(9809):     at java.lang.reflect.Method.invokeNative(Native Method)
02-02 16:01:58.230: E/AndroidRuntime(9809):     at java.lang.reflect.Method.invoke(Method.java:511)
02-02 16:01:58.230: E/AndroidRuntime(9809):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:980)
02-02 16:01:58.230: E/AndroidRuntime(9809):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:747)
02-02 16:01:58.230: E/AndroidRuntime(9809):     at dalvik.system.NativeStart.main(Native Method)
02-02 16:01:58.230: E/AndroidRuntime(9809): Caused by: java.lang.NullPointerException
02-02 16:01:58.230: E/AndroidRuntime(9809):     at com.pearmak.drawing.ActivityA.onCreate(ActivityA.java:102)
02-02 16:01:58.230: E/AndroidRuntime(9809):     at android.app.Activity.performCreate(Activity.java:4470)
02-02 16:01:58.230: E/AndroidRuntime(9809):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1052)
02-02 16:01:58.230: E/AndroidRuntime(9809):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1931)
02-02 16:01:58.230: E/AndroidRuntime(9809):     ... 11 more
4

3 に答える 3

2

startActivity()あなたは自分のクラスではないクラスから電話をかけようとしていると思いますActivity。メソッドはクラスにstartActivity()属しているため、 (extends )からメソッドを呼び出すか、のインスタンスをこのクラスに渡して呼び出しを使用する必要があります。お役に立てれば。ContextActivityContextContextcontext.startActivity()

于 2013-02-02T07:24:53.940 に答える
1

コンストラクターを使用するか、メソッドをパラメーター化されたメソッドに変更して、ActivityContextをNonActivityクラスに渡す必要があります。

public class PaintView extends View 
{      
  Context context_new;
  public PaintView(Context context){
    this.context_new=context;
  }
   //.. your code here
   public void shareImage(Context context)
   {

     context.startActivity(Intent.createChooser(share, "Share drawing"));
     //OR
     //context_new.startActivity(Intent.createChooser(share, "Share drawing"));
      Toast.makeText(context, 
          "Please save the image first...",
                   Toast.LENGTH_LONG).show();   

      //OR
         // Toast.makeText(context_new, 
         // "Please save the image first...",
         //Toast.LENGTH_LONG).show(); 

   }

}

そして、ActivityからshareImageメソッドを次のように呼び出します。

PaintView paintview=new PaintView(Your_Current_Activity.this);
paintview.shareImage(Your_Current_Activity.this);
于 2013-02-02T07:45:39.703 に答える
0

として試してみてくださいstartActivityForResult(Intent.createChooser(share, "Share drawing"));

于 2013-02-02T07:32:00.050 に答える