Android:自定义视图在从延迟后的runnable进行调用时不呈现

时间:2013-11-18 09:28:03

标签: android android-layout android-view onresume postdelayed

您好我想创建一个使用Multi-touch支持的应用程序。

多点触控的

我正在使用这个

https://code.google.com/p/android-multitouch-controller/

现在,您可以看到该活动中有org.metalev.multitouch.photosortr.PhotoSortrActivity活动我做了一些更改,创建了一个布局文件并使用了它工作正常

public class PhotoSortrActivity extends Activity {
    PhotoSortrView photoSorter;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        this.setTitle(R.string.instructions);
        setContentView(R.layout.layout);
        photoSorter = (PhotoSortrView)findViewById(R.id.view);

    }

     @Override
     protected void onResume() {
     super.onResume();
     photoSorter.loadImages(this);
     }

布局文件layout.xml

  <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >
 <org.metalev.multitouch.photosortr.PhotoSortrView 
     android:layout_height="fill_parent"
     android:layout_width="fill_parent"
     android:id="@+id/view" />   

</LinearLayout>

问题 但是当我在onCreate方法中调用photoSorter.loadImages(getApplicationContext)后延迟runnable时就像这样

        public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        this.setTitle(R.string.instructions);
        setContentView(R.layout.layout);
        photoSorter = (PhotoSortrView)findViewById(R.id.view);
        photoSorter.postDelayed(new Runnable() {
            @Override
            public void run() {
                // TODO Auto-generated method stub
                photoSorter.loadImages(getApplicationContext());

            }
        },500);

    }

我收到错误,下面是日志猫

    e 11-18 09:20:27.987: E/AndroidRuntime(21395): FATAL EXCEPTION: main
11-18 09:20:27.987: E/AndroidRuntime(21395): java.lang.NullPointerException
11-18 09:20:27.987: E/AndroidRuntime(21395):    at org.metalev.multitouch.photosortr.PhotoSortrView$Img.draw(PhotoSortrView.java:313)
11-18 09:20:27.987: E/AndroidRuntime(21395):    at org.metalev.multitouch.photosortr.PhotoSortrView.onDraw(PhotoSortrView.java:122)
11-18 09:20:27.987: E/AndroidRuntime(21395):    at android.view.View.draw(View.java:13944)
11-18 09:20:27.987: E/AndroidRuntime(21395):    at android.view.View.draw(View.java:13825)
11-18 09:20:27.987: E/AndroidRuntime(21395):    at android.view.ViewGroup.drawChild(ViewGroup.java:3083)
11-18 09:20:27.987: E/AndroidRuntime(21395):    at android.view.ViewGroup.dispatchDraw(ViewGroup.java:2920)
11-18 09:20:27.987: E/AndroidRuntime(21395):    at android.view.View.draw(View.java:13823)
在PhotoSortrView.java文件中的

代码如下所示,我收到此错误

   public void draw(Canvas canvas) {
    canvas.save();
    float dx = (maxX + minX) / 2;
    float dy = (maxY + minY) / 2;
    ---->drawable.setBounds((int) minX, (int) minY, (int) maxX, (int) maxY); // HERE getting drawable == NULL
    canvas.translate(dx, dy);
    canvas.rotate(angle * 180.0f / (float) Math.PI);
    canvas.translate(-dx, -dy);
    drawable.draw(canvas);
    canvas.restore();
}

0 个答案:

没有答案