我在使用GridViewPager for Android Wear时遇到问题。将它添加到我的布局时,Android Studio会说没有这样的类(Null Pointer Exception)。我究竟做错了什么?我正在使用Android Studio 0.8.2,我已经通过向导选择了Wear应用程序。 这是我的代码:
布局:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MyActivity"
tools:deviceIds="wear_square">
<android.support.wearable.view.GridViewPager
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/pager"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:keepScreenOn="true" />
</LinearLayout>
和我的错误:
FATAL EXCEPTION: main
Process: eu.nalewczynski.weargsdgs, PID: 10653
java.lang.NullPointerException
at android.support.wearable.view.GridViewPager.populate(GridViewPager.java:900)
at android.support.wearable.view.GridViewPager.populate(GridViewPager.java:848)
at android.support.wearable.view.GridViewPager.onMeasure(GridViewPager.java:1141)
at android.view.View.measure(View.java:16648)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5125)
at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1404)
at android.widget.LinearLayout.measureVertical(LinearLayout.java:695)
at android.widget.LinearLayout.onMeasure(LinearLayout.java:588)
at android.view.View.measure(View.java:16648)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5125)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
at android.support.wearable.view.WatchViewStub.onMeasure(WatchViewStub.java:143)
at android.view.View.measure(View.java:16648)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5125)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
at android.view.View.measure(View.java:16648)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5125)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2321)
at android.view.View.measure(View.java:16648)
at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:1959)
at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1145)
at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1340)
at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1032)
at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:5657)
at android.view.Choreographer$CallbackRecord.run(Choreographer.java:761)
at android.view.Choreographer.doCallbacks(Choreographer.java:574)
at android.view.Choreographer.doFrame(Choreographer.java:544)
at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:747)
at android.os.Handler.handleCallback(Handler.java:733)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5026)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:777)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:602)
at dalvik.system.NativeStart.main(Native Method)
我的完整java代码几乎没有动过:
import android.app.Activity;
import android.os.Bundle;
import android.support.wearable.view.GridViewPager;
import android.support.wearable.view.WatchViewStub;
public class MyActivity extends Activity {
GridViewPager gvp;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_my);
final WatchViewStub stub = (WatchViewStub) findViewById(R.id.watch_view_stub);
stub.setOnLayoutInflatedListener(new WatchViewStub.OnLayoutInflatedListener() {
@Override
public void onLayoutInflated(WatchViewStub stub) {
//blahblahblah
}
});
}
}
答案 0 :(得分:4)
首先,NullPointerException
不适用于无法找到课程的情况 - 有java.lang.ClassNotFoundException
。在对null为null的变量调用方法时会发生NullPointerException
。
Object nullString = null;
nullString.toString() // <- will throw NullPointerException;
第二件事:你需要学习如何阅读堆栈跟踪:
您可以在GridViewPager
类上看到一些方法被调用。这显然意味着这个课程存在且可以找到。
at android.support.wearable.view.GridViewPager.populate(GridViewPager.java:900)
at android.support.wearable.view.GridViewPager.populate(GridViewPager.java:848)
at android.support.wearable.view.GridViewPager.onMeasure(GridViewPager.java:1141)
您的真正问题不在于您的xml布局,而是在Java代码中。请发布您的完整Java代码(活动,适配器等),以便获得有关您的问题的一些帮助。
我已经测试了这个问题,当我添加了一个没有任何适配器的GridViewPager时,我已经设法重现了相同的堆栈跟踪。我还检查了设置适配器时的情况:
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final GridViewPager gridViewPager = (GridViewPager) findViewById(R.id.gridViewPager);
gridViewPager.setAdapter(new MyGridViewPagerAdapter());
}
private class MyGridViewPagerAdapter extends GridPagerAdapter {
@Override
protected void destroyItem(ViewGroup arg0, int arg1, int arg2, Object arg3) {
}
@Override
public int getColumnCount(int arg0) {
return 1;
}
@Override
public int getRowCount() {
return 1;
}
@Override
protected Object instantiateItem(ViewGroup arg0, int arg1, int arg2) {
return null;
}
@Override
public boolean isViewFromObject(View arg0, Object arg1) {
return false;
}
}
}
现在一切正常。
当GridViewPager
或getColumnCount
返回0时,令人惊讶的是getRowCount
抛出异常...那么这是否意味着我们不能拥有有效的“空”适配器? :\
在getColumnCount
或getRowCount
中的堆栈跟踪为0:
Caused by: java.lang.NullPointerException
at android.support.wearable.view.BackgroundController.getBackground(BackgroundController.java:204)
at android.support.wearable.view.BackgroundController.onVerticalScroll(BackgroundController.java:240)
at android.support.wearable.view.BackgroundController.setPage(BackgroundController.java:91)
at android.support.wearable.view.BackgroundController.setAdapter(BackgroundController.java:51)
at android.support.wearable.view.GridViewPager.setAdapter(GridViewPager.java:445)
at com.example.compressiontest.MainActivity.onCreate(MainActivity.java:18)
at android.app.Activity.performCreate(Activity.java:5242)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2161)
... 11 more
Bonus story :这只是可穿戴UI支持lib的另一个奇怪情况。另一件事WearableListView
会在您未设置NullPointerException
时抛出setClickListener
(点击顶部空白区域时):\\更多信息:https://plus.google.com/115773092852757402019/posts/KBZUJwA6CPT