Scrollview(findviewby id)返回null

时间:2015-05-26 10:11:33

标签: android android-layout wear-os android-scrollview

在滚动视图中有一个带有超长图像的图像视图,该图像当前是可滚动的。我试图让这个图像自己滚动,但是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>

1 个答案:

答案 0 :(得分:1)

查看你发布问题的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);

            }
        });

    }
});