gpt4 book ai didi

android - scrollview(findviewby id)返回null

转载 作者:行者123 更新时间:2023-11-30 02:07:38 24 4
gpt4 key购买 nike

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

编辑:这是在 android wear 上

@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();
}

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

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 来初始化 View ,并且必须使用 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);

}
});

}
});

关于android - scrollview(findviewby id)返回null,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30455562/

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