繁体   English   中英

Android Wear NullPointerException无法在TextView上设置文本

[英]Android Wear NullPointerException can't set text on TextView

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

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");
}

当我尝试运行应用程序时,我得到了nullpointerexception

 text.setText("test");

这导致我假设找不到TextView“文本”。 通常在Android手机应用程序上,可以调用它

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

直接之后

setContentView();

但是现在有像WatchViewStub和setOnLayoutInflatedListener这样的东西。 也许有一些我监督过的事情?

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

编辑

activity_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>

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"
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>

根据您的Activity代码:

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 rect_activity:round_activity: layouts中只有一个。

您必须了解WatchViewStub工作原理。 您无法在setContentView之后访问rect / round布局中的视图,因为它们还没有膨胀。 就像你一样 - 你只能在OnLayoutInflatedListener监听器回调中访问你的视图。
您试图在此回调中找到您的mTextView - 这是您应该这样做的方式。

怎么了?

您应该删除以下行:

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

而这一个:(它也会导致你的NullPointerException

  text.setText("test");

并仅保留mTextView字段。

你该怎么办?

mTextView = (TextView)stub.findViewById(R.id.text);之后添加以下内容mTextView = (TextView)stub.findViewById(R.id.text);

  mTextView.setText("test");

它必须包含OnLayoutInflatedListener

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

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");
    }};
}

您在activity_my.xml没有TextView

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

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

您可以在InsetActivity.onReadyForContent()扩展InsetActivity并设置内容视图,而不是添加OnLayoutInflatedListener

import android.support.wearable.activity.InsetActivity; public class MainActivity extends InsetActivity { ... @Override public void onReadyForContent() { setContentView(R.layout.activity_main); someTextField = (TextView) findViewById(R.id.some_field);

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM