【问题标题】:android startActivity undefinedandroid startActivity 未定义
【发布时间】:2013-02-02 07:19:55
【问题描述】:

我正在制作一个绘图应用程序并想分享图像。但是,我遇到了 startActivity 部分的错误,其中错误写为 The method startActivity(Intent) is undefined for the type 。这意味着什么以及如何解决?非常感谢提前!!!

编辑:

发布了代码的进一步代码设置:ActivityAPaintView 中调用shareImage()

不知道这样的Context是否可以?除了这个新增的分享功能,没有分享的代码运行起来非常流畅。

画图视图

// 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

【问题讨论】:

  • 请显示错误。我猜你还没有在 android 清单文件中记录活动
  • 是的,打印错误信息。
  • 感谢您的快速回复!该代码还不能执行,因此还没有 logcat。错误是下划线的代码编写面板
  • @pearmak : 使用Your_Current_Activity_Name.this.startActivity(Intent.createChooser(share, "Share drawing")); 而不是startActivity(Intent.createChooser(share, "Share drawing")); 来启动Activity
  • 哦!这是一个愚蠢的错误!我还没有声明共享按钮....现在它可以工作了!!!

标签: android start-activity


【解决方案1】:

我猜你正试图从一个不是你的 Activity 类的类中调用 startActivity()startActivity() 方法属于 Context 类,因此您要么需要从 Activity(扩展 Context)调用它,要么将 Context 的实例传递给此类并使用 context.startActivity() 调用.希望这会有所帮助。

【讨论】:

    【解决方案2】:

    您需要通过使用构造函数或将方法更改为参数化方法将 Activity 上下文传递给非 Activity 类:

    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);
    

    【讨论】:

      【解决方案3】:

      试试startActivityForResult(Intent.createChooser(share, "Share drawing"));

      【讨论】:

      • 我认为 startActivityForResult 需要参数第一个意图和第二个请求代码
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-11-11
      • 1970-01-01
      • 1970-01-01
      • 2019-07-15
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多