Java Android Wear NullPointerException可以';t在TextView上设置文本

Java Android Wear NullPointerException可以';t在TextView上设置文本,java,android,android-layout,wear-os,Java,Android,Android Layout,Wear Os,我正在尝试编写一个Android Wear应用程序。现在,我有一个文本视图,希望以编程方式设置文本 private TextView mTextView; private TextView text; public void onCreate(Bundle savedInstance) { super.onCreate(savedInstance); setContentView(R.layout.activity_my); final WatchViewStub stub = (

我正在尝试编写一个Android Wear应用程序。现在,我有一个文本视图,希望以编程方式设置文本

private TextView mTextView;
private TextView text;

public void onCreate(Bundle savedInstance) {
  super.onCreate(savedInstance);
  setContentView(R.layout.activity_my);
  final WatchViewStub stub = (WatchViewStub)findViewById(R.id.watch_view_stub);
  text = (TextView)findViewById(R.id.text);
  stub.setOnLayoutInflatedListener((stub -> {
    mTextView = (TextView)stub.findViewById(R.id.text);
  }};

  text.setText("test");
}
当尝试这样运行应用程序时,我在

 text.setText("test");
这让我假设找不到TextView“text”。通常在安卓手机应用程序上,拨打

text = (TextView)findViewById(R.id.text);
紧接着

setContentView();
但是现在有了WatchViewStub和SetOnlayOutingFlatedListener之类的东西。也许有什么我监督过的

但是,如何在Android Wear平台上正确指定TextView

编辑

活动_my.xml:

<?xml version="1.0" encoding="utf-8"?>

<android.support.wearable.view.WatchViewStub
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/watch_view_stub"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:rectLayout="@layout/rect_activity_my"
app:roundLayout="@layout/round_activity_my"
tools:context=".MyActivity"
tools:deviceIds="wear">
</android.support.wearable.view.WatchViewStub>

rect_activity.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"
android:orientation="vertical"
tools:context=".MyActivity"
tools:deviceIds="wear_square">

<TextView
    android:id="@+id/text"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/text"
    android:textSize="30sp"
    android:layout_centerInParent="true"/>

</RelativeLayout>
<?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=".MyActivity"
tools:deviceIds="wear_round">

<TextView
    android:id="@+id/text"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/text"
    android:textSize="30sp"
    android:layout_centerInParent="true"/>

</RelativeLayout>

round_activity.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"
android:orientation="vertical"
tools:context=".MyActivity"
tools:deviceIds="wear_square">

<TextView
    android:id="@+id/text"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/text"
    android:textSize="30sp"
    android:layout_centerInParent="true"/>

</RelativeLayout>
<?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=".MyActivity"
tools:deviceIds="wear_round">

<TextView
    android:id="@+id/text"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/text"
    android:textSize="30sp"
    android:layout_centerInParent="true"/>

</RelativeLayout>

活动\u my.xml


text=(TextView)findViewById(R.id.text)

如果rect_activity.xml和round_activity.xml用于不同的活动,则需要创建新活动并为这些xml调用setContentView
只有在这之后,您才能使用代码中的视图。

根据
活动中的代码:

private TextView mTextView;
private TextView text;

public void onCreate(Bundle savedInstance) {
    super.onCreate(savedInstance);
    setContentView(R.layout.activity_my);
    final WatchViewStub stub = (WatchViewStub)findViewById(R.id.watch_view_stub);
    text = (TextView)findViewById(R.id.text);
    stub.setOnLayoutInflatedListener((stub -> {
        mTextView = (TextView)stub.findViewById(R.id.text);
    }};

    text.setText("test");
}
为什么有两个
TextView
s?您的
rect\u活动:
round\u活动:
版面中只有一个

您必须了解
WatchViewStub
的工作原理。您不能在
setContentView
之后立即从矩形/圆形布局访问视图,因为它们尚未膨胀。和您一样-您只能在
OnlayOutlineFlatedListener
listener回调中访问视图。
您已尝试在此回调中查找您的
mTextView
,这是您应该使用的方法

怎么了? 您应该删除以下行:

  text = (TextView)findViewById(R.id.text);
这一个:(它也会导致您的
NullPointerException

并仅保留
mTextView
字段

你该怎么办? 在
mTextView=(TextView)stub.findviewbyd(R.id.text)之后添加如下内容

它必须包含在
OnlayOutinedFlatedListener

您的最终代码应该如下所示:

private TextView mTextView;

public void onCreate(Bundle savedInstance) {
    super.onCreate(savedInstance);
    setContentView(R.layout.activity_my);
    final WatchViewStub stub = (WatchViewStub)findViewById(R.id.watch_view_stub);
    stub.setOnLayoutInflatedListener((stub -> {
        mTextView = (TextView)stub.findViewById(R.id.text);
        mTextView.setText("test");
    }};
}

您可以扩展
插入式
并在
插入式中设置内容视图,而不是添加
OnlayOutlineFlatedListener
。onReadyForContent()

导入android.support.wearable.activity.insetivity;
公共类main活动扩展了插入性{
...
@凌驾
public void onReadyForContent(){
setContentView(R.layout.activity_main);
someTextField=(TextView)findViewById(R.id.some_字段);

你能发布
活动\u my.xml
@吗?我刚刚编辑了我的帖子并添加了xml文件,尝试从这里学习:那么我如何在round\u活动或rect\u活动中访问TextView?