Android - 如何从其他类

时间:2015-12-12 15:21:56

标签: java android

我现在把头发拉过来几个小时。

我是Android Wear设备,我在其中跟踪传感器数据。我已成功将此数据发送到配对的智能手机,我可以看到手机上的logcat中显示的消息。

但是,我试图在手机上附加一个textview来显示传感器数据,但是当我调用textview.append()时,它会因空指针异常而崩溃。

错误:

java.lang.NullPointerException: Attempt to invoke virtual method 'android.view.View android.view.Window.findViewById(int)' on a null object reference

在手机上侦听更改的listenerService。这确实有效,我在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);
}

我的MainActivity中的接收方法,甚至可以获得正确的消息'这里是logcat:

    public void appendData(String message) {

    Log.d("MESSAGE", message);
    sensorList.append("\n" + " DISPLAY PLS :  " + message);
}

我在MainActivity中的onCreate()方法中正确地夸大了布局,我可以在此方法中附加到此TextView:

 protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
...
sensorList = (TextView) findViewById(R.id.sensorListView);
    sensorList.setText("\n" + "On create");

知道我做错了什么?

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

1 个答案:

答案 0 :(得分:1)

您可以使用BroadcastReceiver实现目标,下面是示例代码:

电话侧

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