我在使用Android Wear的GridViewPager时遇到问题。将其添加到布局中时,Android Studio表示没有此类(空指针异常)。我究竟做错了什么?我正在使用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
}
});
}
}
最佳答案
首先,NullPointerException
不适用于无法找到类的情况-存在java.lang.ClassNotFoundException
。当对空值的变量调用方法时,发生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时,我设法重现了相同的stacktrace。我还检查了设置适配器的情况:
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的Stacktrace: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
奖励故事:这只是可穿戴UI支持库的另一种怪异情况。另一件事是
WearableListView
,当您未设置NullPointerException
时会抛出setClickListener
(单击顶部空白区域时)):\此处有更多信息:https://plus.google.com/115773092852757402019/posts/KBZUJwA6CPT