gpt4 book ai didi

android - 在 Android Wear 上使用 GridViewPager 类时出现 NullPointerException

转载 作者:行者123 更新时间:2023-11-29 16:00:47 25 4
gpt4 key购买 nike

我在为 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.ClassNotFoundExceptionNullPointerException 在对 null 变量调用方法时发生。

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 代码( Activity 、适配器等)以获得解决您的问题的一些帮助。


编辑

我已经测试了这个问题,并且当我只添加一个没有任何适配器的 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;
}
}
}

现在一切正常。
getColumnCountgetRowCount 返回 0 时,GridViewPager 会抛出异常……这是否意味着我们不能拥有有效的“空”适配器? :\getColumnCountgetRowCount 中 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

奖金故事:这只是可穿戴 UI 支持库的另一种奇怪情况。另一件事是 WearableListView 在您未设置 setClickListener 时抛出 NullPointerException(单击顶部空白区域时):\
更多信息在这里:https://plus.google.com/115773092852757402019/posts/KBZUJwA6CPT

关于android - 在 Android Wear 上使用 GridViewPager 类时出现 NullPointerException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24752716/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com