Scrollview (findviewby id) returns null

Scrollview (findviewby id) returns null

滚动视图中有一个图像视图,其中包含当前可滚动的超长图像。我试图让这张图片自行滚动,但是 findViewById 在滚动视图上一直返回 null。

编辑:这是 android 穿

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    final ScrollView sv = (ScrollView) MainActivity.this.findViewById(R.id.mtr_scrollView);
    //LinearLayout layout = (LinearLayout)sv.findViewById(R.id.something)
    //sv.scrollTo(0, sv.getBottom());

    sv.post(new Runnable() {
        public void run() {
            sv.fullScroll(View.FOCUS_DOWN);
            //sv.scrollTo(0, sv.getBottom());
        }
    });

    final WatchViewStub stub = (WatchViewStub) findViewById(R.id.watch_view_stub);
    stub.setOnLayoutInflatedListener(new WatchViewStub.OnLayoutInflatedListener() {
        @Override
        public void onLayoutInflated(WatchViewStub stub) {
            mTextView = (TextView) stub.findViewById(R.id.text);

        }
    });

    handler = new Handler();

    nodeListener = new NodeApi.NodeListener() {
        @Override
        public void onPeerConnected(Node node) {
            remoteNodeId = node.getId();
            handler.post(new Runnable() {
                @Override
                public void run() {

                }
            });
            Intent intent = new Intent(getApplicationContext(), ConfirmationActivity.class);
            intent.putExtra(ConfirmationActivity.EXTRA_ANIMATION_TYPE, ConfirmationActivity.SUCCESS_ANIMATION);
            intent.putExtra(ConfirmationActivity.EXTRA_MESSAGE, getString(R.string.peer_connected));
            startActivity(intent);
        }

        @Override
        public void onPeerDisconnected(Node node) {
            handler.post(new Runnable() {
                @Override
                public void run() {

                }
            });
            Intent intent = new Intent(getApplicationContext(), ConfirmationActivity.class);
            intent.putExtra(ConfirmationActivity.EXTRA_ANIMATION_TYPE, ConfirmationActivity.FAILURE_ANIMATION);
            intent.putExtra(ConfirmationActivity.EXTRA_MESSAGE, getString(R.string.peer_disconnected));
            startActivity(intent);
        }
    };

    messageListener = new MessageApi.MessageListener() {
        @Override
        public void onMessageReceived(MessageEvent messageEvent) {
            byte[]bytes = messageEvent.getData();
            final String temp = new String(bytes);
            if (messageEvent.getPath().equals(MESSAGE_PATH)) {
                handler.post(new Runnable() {
                    @Override
                    public void run() {

                    }
                });
            }
        }

    };

    apiClient = new GoogleApiClient.Builder(getApplicationContext()).addConnectionCallbacks(new GoogleApiClient.ConnectionCallbacks() {
        @Override
        public void onConnected(Bundle bundle) {

            Wearable.NodeApi.addListener(apiClient, nodeListener);
            Wearable.MessageApi.addListener(apiClient, messageListener);

            Wearable.NodeApi.getConnectedNodes(apiClient).setResultCallback(new ResultCallback<NodeApi.GetConnectedNodesResult>() {
                @Override
                public void onResult(NodeApi.GetConnectedNodesResult getConnectedNodesResult) {
                    if (getConnectedNodesResult.getStatus().isSuccess() && getConnectedNodesResult.getNodes().size() > 0) {
                        remoteNodeId = getConnectedNodesResult.getNodes().get(0).getId();

                    }
                }
            });
        }

        @Override
        public void onConnectionSuspended(int i) {

        }
    }).addApi(Wearable.API).build();
}

我正在尝试滚动让滚动视图中的图像在应用程序启动后自动滚动。

XML如下

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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" tools:context=".MainActivity" tools:deviceIds="wear_round"
android:background="@color/black">

<ScrollView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/mtr_scrollView"
    android:layout_alignParentBottom="true"
    android:layout_alignParentEnd="true"
    android:layout_alignParentTop="true"
    android:layout_alignParentStart="true" >

    <LinearLayout
        android:orientation="vertical"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/something">
        <ImageView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@drawable/sample"
            android:id="@+id/imageView"
            android:scaleType="fitStart"
            android:layout_gravity="center_horizontal"
            android:paddingTop="20dp"
            android:paddingBottom="20dp" />
    </LinearLayout>
  </ScrollView>

查看 xml 您发布的问题不是 ScrollView,而是 WatchViewStub。干掉

final WatchViewStub stub = (WatchViewStub) findViewById(R.id.watch_view_stub);
stub.setOnLayoutInflatedListener(new WatchViewStub.OnLayoutInflatedListener() {
    @Override
    public void onLayoutInflated(WatchViewStub stub) {
        mTextView = (TextView) stub.findViewById(R.id.text);

    }
});

编辑

如果您使用 WatchViewStub,则必须等待 onLayoutInflated 来初始化视图,并且必须使用 stub 参数:

final WatchViewStub stub = (WatchViewStub)       findViewById(R.id.watch_view_stub);
stub.setOnLayoutInflatedListener(new WatchViewStub.OnLayoutInflatedListener() {
    @Override
    public void onLayoutInflated(WatchViewStub stub) {
        final ScrollView sv = (ScrollView)  stub.findViewById(R.id.mtr_scrollView);

        sv.post(new Runnable() {
            public void run() {
               sv.fullScroll(View.FOCUS_DOWN);

            }
        });

    }
});