Reputation: 103
I'm pulling my hair out over this for a few hours now.
I've an Android Wear device, where I have tracked sensor data. I've sent this data successfully to the paired smartphone, I can see the messages displayed in logcat on the phone.
However, I'm trying to append a textview on the phone to display the sensor data, but when I call the textview.append() it crashes with a null pointer exception.
Error:
java.lang.NullPointerException: Attempt to invoke virtual method 'android.view.View android.view.Window.findViewById(int)' on a null object reference
The listenerService that is listening for changes, on the phone. This does work, I'm getting the correct info in logcat:
public class ListenerService extends WearableListenerService {
@Override
public void onMessageReceived(MessageEvent messageEvent) {
retrieveMessage(messageEvent.getPath());
}
private void retrieveMessage(String message) {
MainActivity sensorData = new MainActivity();
sensorData.appendData(message);
}
And the receiving method in my MainActivity, even getting the correct 'message' here in logcat:
public void appendData(String message) {
Log.d("MESSAGE", message);
sensorList.append("\n" + " DISPLAY PLS : " + message);
}
I've correctly inflated the the layout from within the onCreate() method in my MainActivity and I can append to this TextView from within this method:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
...
sensorList = (TextView) findViewById(R.id.sensorListView);
sensorList.setText("\n" + "On create");
Any idea as to what I am doing wrong?
<android.support.v4.widget.DrawerLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/DrawerLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:elevation="7dp">
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<include
android:id="@+id/tool_bar"
layout="@layout/tool_bar">
</include>
<TextView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="@+id/sensorListView"
android:layout_gravity="center_horizontal|top"
android:layout_marginTop="100dp"
android:text="Sensors" />
</FrameLayout>
<android.support.v7.widget.RecyclerView
android:id="@+id/RecyclerView"
android:layout_width="320dp"
android:layout_height="match_parent"
android:layout_gravity="left"
android:background="#ffffff"
android:scrollbars="vertical">
</android.support.v7.widget.RecyclerView>
Upvotes: 2
Views: 2275
Reputation: 303
You can achieve your goal with BroadcastReceiver, here is the sample code:
Phone Side
public MainActivity extends Activity {
...
public static final String ACTION_TEXT_CHANGED = "changed";
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
...
sensorList = (TextView) findViewById(R.id.sensorListView);
sensorList.setText("\n" + "On create");
...
LocalBroadcastManager.getInstance(this).registerReceiver(broadcastReceiver, new IntentFilter(ACTION_TEXT_CHANGED));
}
private BroadcastReceiver broadcastReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
String content = intent.getStringExtra("content");
sensorList.setText("");
}
};
}
public class ListenerService extends WearableListenerService {
...
public static final String ACTION_TEXT_CHANGED = "changed";
...
@Override
public void onMessageReceived(MessageEvent messageEvent) {
...
retrieveMessage(message);
...
}
private void retrieveMessage(String message) {
Intent intent = new Intent();
intent.setAction(ACTION_TEXT_CHANGED);
intent.putExtra("content", message);
LocalBroadcastManager.getInstance(this).sendBroadcast(intent);
}
}
Upvotes: 1