简体   繁体   中英

Android Wear NullPointerException can't set text on TextView

I am trying to write an Android Wear application. For now I've got a TextView and would like to set the text programmatically.

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

When trying to run the application like that I get a nullpointerexception at

 text.setText("test");

That led me to assume that the TextView "text" was not found. Normally on Android phone applications it is just fine to call the

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

Directly after

setContentView();

But now there are things like the WatchViewStub and the setOnLayoutInflatedListener. Maybe there's something I've overseen?

However, how do I specify my TextView correctly on the Android Wear platform?

Edit :

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>

According to code from your 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");
}

Why do you have two TextView s? You have only one either in your rect_activity: or round_activity: layouts.

You have to understand how WatchViewStub works. You cannot access views from your rect/round layouts right after setContentView because they are not inflated yet. Like you did - you can access your views only in OnLayoutInflatedListener listener callback.
You have tried to find your mTextView inside this callback - which is the way you should do it.

What is wrong?

You should remove following lines:

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

and this one: (it also causes your NullPointerException )

  text.setText("test");

and keep only the mTextView field.

What should you do?

Add following like right after mTextView = (TextView)stub.findViewById(R.id.text); :

  mTextView.setText("test");

It must be incide the OnLayoutInflatedListener .

Your final code should look like:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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