从设备加载图像并应用视差效果时出现RuntimeException

时间:2015-06-03 07:33:06

标签: android android-layout android-intent android-activity android-xml

在我的应用程序中,我想从图库加载图像或使用相机拍摄图像,我想在该图像上应用视差滚动视图效果。我有一个名为header的xml文件,它包含一个图像视图和两个按钮,一个用于加载图像的另一个用于使用camera拍摄图像。我尝试了这段代码,但是当我运行应用程序时应用程序崩溃,它在logcat中显示运行时异常,有人可以帮我找出问题吗?

logcat的

06-03 12:39:02.683  27327-27327/? E/AndroidRuntime﹕ FATAL EXCEPTION: main
java.lang.RuntimeException: Unable to start activity ComponentInfo{in.zoid.parallaxtutorial/in.zoid.parallaxtutorial.MainActivity}: java.lang.NullPointerException
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2110)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2135)
        at android.app.ActivityThread.access$700(ActivityThread.java:140)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1237)
        at android.os.Handler.dispatchMessage(Handler.java:99)
        at android.os.Looper.loop(Looper.java:137)
        at android.app.ActivityThread.main(ActivityThread.java:4921)
        at java.lang.reflect.Method.invokeNative(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:511)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1027)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:794)
        at dalvik.system.NativeStart.main(Native Method)
 Caused by: java.lang.NullPointerException
        at in.zoid.parallaxtutorial.MainActivity.onCreate(MainActivity.java:71)
        at android.app.Activity.performCreate(Activity.java:5206)
        at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1094)
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2074)

MainActivity.java


public class MainActivity extends Activity {

private int lastTop = 0;
//ImageView image;
ListView listView;
private static int RESULT_LOAD_IMAGE = 1;
private static final int CAMERA_REQUEST = 1888;
private ImageView imageView;
ArrayAdapter adapter;
ArrayList<String> items = new ArrayList<>();

public void parallax(final View v) {
    final Rect r = new Rect();
    v.getLocalVisibleRect(r);

    if (lastTop != r.top) {
        lastTop = r.top;
        v.post(new Runnable() {
            @Override
            public void run() {
                v.setY((float) (r.top / 2.0));
            }
        });
    }
}

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    listView = (ListView) findViewById(R.id.listView);

    items.add("List Item 1");
    items.add("List Item 2");
    items.add("List Item 3");
    items.add("List Item 4");
    items.add("List Item 5");
    items.add("List Item 6");
    items.add("List Item 7");
    items.add("List Item 8");
    items.add("List Item 9");
    items.add("List Item 10");
    items.add("List Item 11");
    items.add("List Item 12");
    items.add("List Item 13");
    items.add("List Item 14");
    items.add("List Item 15");
    items.add("List Item 16");
    items.add("List Item 17");
    items.add("List Item 18");
    items.add("List Item 19");
    items.add("List Item 20");

    View view = getLayoutInflater().inflate(R.layout.header, null, false);

    Button buttonLoadImage = (Button) findViewById(R.id.buttonLoadPicture);
    buttonLoadImage.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View arg0) {


            Intent i = new Intent(
                    Intent.ACTION_PICK,
                    android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);

            startActivityForResult(i, RESULT_LOAD_IMAGE);
        }
    });
    this.imageView = (ImageView)this.findViewById(R.id.imgView);
   /* Button photoButton = (Button) this.findViewById(R.id.button2);
    photoButton.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
            startActivityForResult(cameraIntent, CAMERA_REQUEST);
        }
    });*/

    //imageview block end


    // image = (ImageView) view.findViewById(R.id.image);
    listView.addHeaderView(view);

    adapter = new ArrayAdapter(this, android.R.layout.simple_list_item_1, items);
    listView.setAdapter(adapter);
    adapter.notifyDataSetChanged();
    listView.setOnScrollListener(new AbsListView.OnScrollListener() {
        @Override
        public void onScrollStateChanged(AbsListView view, int scrollState) {
            parallax(imageView);
        }

        @Override
        public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
            parallax(imageView);
        }
    });
}
}

header.xml

<!--?xml version="1.0" encoding="utf-8"?-->
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<ImageView android:id="@+id/imgView"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_weight="1"></ImageView>
<RelativeLayout
    android:id="@+id/relativeLayout1"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:background="@android:color/black" >
    <Button android:id="@+id/buttonLoadPicture"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="0"
        android:text="Load Picture"
        android:layout_gravity="center"></Button>
    <Button
        android:id="@+id/button2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Take Picture"
        android:layout_alignParentRight="true"/>
</RelativeLayout>
</LinearLayout>

1 个答案:

答案 0 :(得分:0)

我认为问题在于:

@api.onchange('product')
def product_changed(self)
    self.price = self.product.price

你应该使用这个

Button buttonLoadImage = (Button) findViewById(R.id.buttonLoadPicture);

因为标题尚未添加到活动布局中,所以当找到buttonLoadPicture时,当前代码将获得空指针。