2015-12-12 72 views
2

現在,我正在拉我的頭髮幾個小時。Android - 如何更新另一個課程的textview

我有一臺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的正確的信息:在我的MainActivity

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

和接收方法,即使得到正確的「消息」在這裏的logcat:

public void appendData(String message) { 

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

我已經從正確充氣佈局onCreate()方法中,我MainActivity,我可以在此方法中追加到此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> 

+0

您可以添加完整的堆棧和挪用行號? – Domin

+0

您可能可以使用帶回調的界面來設置文本視圖。它類似於Android文檔中的「與片段交互」示例 –

+0

實際上,您的問題是您在'new MainActivity();中明確地創建了一個新活動?'您是否應該製作一個Intent來啓動MainActivity並傳​​遞數據通過它? –

回答

1

可以實現與廣播接收器你的目標,這裏是示例代碼:

手機端

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

謝謝,我有一個想法是與BroadcastReceiver好,但我無法弄清楚。再次感謝。 – mangledBadger

相關問題