从设备加载图像并应用视差效果时出现 RuntimeException
RuntimeException when loading image from device and applying parallax effect
在我的应用程序中,我想从图库加载图像或使用相机拍摄图像,并且我想对其应用视差滚动视图效果 image.I 有一个名为 header 的 xml 文件,它包含一个图像视图和两个按钮,一个用于加载图像另一个用于使用 camera.I 拍摄图像尝试了此代码但是当我 运行 应用程序时应用程序崩溃,它在 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.access0(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>
我认为问题出在这里:
Button buttonLoadImage = (Button) findViewById(R.id.buttonLoadPicture);
你应该使用这个
Button buttonLoadImage = (Button) view.findViewById(R.id.buttonLoadPicture);
因为header还没有添加到activity布局中,所以你当前的代码在找到buttonLoadPicture时会得到一个空指针。
在我的应用程序中,我想从图库加载图像或使用相机拍摄图像,并且我想对其应用视差滚动视图效果 image.I 有一个名为 header 的 xml 文件,它包含一个图像视图和两个按钮,一个用于加载图像另一个用于使用 camera.I 拍摄图像尝试了此代码但是当我 运行 应用程序时应用程序崩溃,它在 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.access0(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>
我认为问题出在这里:
Button buttonLoadImage = (Button) findViewById(R.id.buttonLoadPicture);
你应该使用这个
Button buttonLoadImage = (Button) view.findViewById(R.id.buttonLoadPicture);
因为header还没有添加到activity布局中,所以你当前的代码在找到buttonLoadPicture时会得到一个空指针。