[英]Android Wear NullPointerException can't set text on TextView
I am trying to write an Android Wear application. 我正在尝试编写Android Wear应用程序。 For now I've got a TextView and would like to set the text programmatically. 现在我有一个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");
}
When trying to run the application like that I get a nullpointerexception at 当我尝试运行应用程序时,我得到了nullpointerexception
text.setText("test");
That led me to assume that the TextView "text" was not found. 这导致我假设找不到TextView“文本”。 Normally on Android phone applications it is just fine to call the 通常在Android手机应用程序上,可以调用它
text = (TextView)findViewById(R.id.text);
Directly after 直接之后
setContentView();
But now there are things like the WatchViewStub and the setOnLayoutInflatedListener. 但是现在有像WatchViewStub和setOnLayoutInflatedListener这样的东西。 Maybe there's something I've overseen? 也许有一些我监督过的事情?
However, how do I specify my TextView correctly on the Android Wear platform? 但是,如何在Android Wear平台上正确指定TextView?
Edit : 编辑 :
activity_my.xml: 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: 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: 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
: 根据您的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? 为什么你有两个TextView
? You have only one either in your rect_activity:
or round_activity:
layouts. 在rect_activity:
或round_activity:
layouts中只有一个。
You have to understand how WatchViewStub
works. 您必须了解WatchViewStub
工作原理。 You cannot access views from your rect/round layouts right after setContentView
because they are not inflated yet. 您无法在setContentView
之后访问rect / round布局中的视图,因为它们还没有膨胀。 Like you did - you can access your views only in OnLayoutInflatedListener
listener callback. 就像你一样 - 你只能在OnLayoutInflatedListener
监听器回调中访问你的视图。
You have tried to find your mTextView
inside this callback - which is the way you should do it. 您试图在此回调中找到您的mTextView
- 这是您应该这样做的方式。
You should remove following lines: 您应该删除以下行:
text = (TextView)findViewById(R.id.text);
and this one: (it also causes your NullPointerException
) 而这一个:(它也会导致你的NullPointerException
)
text.setText("test");
and keep only the mTextView
field. 并仅保留mTextView
字段。
Add following like right after mTextView = (TextView)stub.findViewById(R.id.text);
在mTextView = (TextView)stub.findViewById(R.id.text);
之后添加以下内容mTextView = (TextView)stub.findViewById(R.id.text);
: :
mTextView.setText("test");
It must be incide the OnLayoutInflatedListener
. 它必须包含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);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.