2015-10-25 22 views
1

您好我試圖打印一個文本文件與雙數據,逐行,從我的Android設備上的文件讀取但它似乎並沒有工作什麼是錯的?我沒有錯誤,但沒有正確打印出來?文件讀取禮物沒有文件實際數據

在我bodyTempInfo查看我得到讀出:

Your Body Temperature Readings are:[] 

我temp2.​​txt文件,總共有1440號的是,不打算在此一一粘貼

37.06750839 
36.89390613 
36.88484785 
36.81941363 
36.81815453 
36.97372599 
37.0510889 
36.90421803 
36.90452867 
36.81245808 
36.91739551 
37.05989195 
36.80757783 
36.85141347 
37.04927826 
36.80384926 
36.94000689 
37.04948705 
37.07007973 
36.95211596 
37.05161886 
36.97029703 
37.03218835 
36.89381629 

我bodyTempInfo。 Java的

package com.teamfara.circadianrhythmmonitor4; 

import android.os.Bundle; 
import android.os.Environment; 
import android.support.v7.app.AppCompatActivity; 
import android.support.v7.widget.Toolbar; 
import android.view.View; 
import android.widget.TextView; 

import java.io.BufferedReader; 
import java.io.File; 
import java.io.FileNotFoundException; 
import java.io.FileReader; 
import java.io.IOException; 
import java.util.ArrayList; 

public class bodyTempInfo extends AppCompatActivity { 


    ArrayList<Double> currentBodyTemp; 

    public void displayCurrentBodyTempArray(View view) throws FileNotFoundException { 

     ArrayList<Double> list = new ArrayList<>(); 

     File sdcard = Environment.getExternalStorageDirectory(); 

//Get the text file 


//Read text from file 

     File file = new File(sdcard, "temp2.txt"); 

     StringBuilder text = new StringBuilder(); 

     try { 

      BufferedReader br = new BufferedReader(new FileReader(file)); 
      String line; 

      while ((line = br.readLine()) != null) { 
       text.append(line); 
       list.add(Double.parseDouble(line)); 
       text.append('\n'); 
      } 
      br.close(); 
     } 
     catch(IOException e) { 

     } 


/* 
     Scanner file2 = new Scanner(new File(sdcard, "temp.txt")); 

     while (file2.hasNextLine()) { 
      String line = file2.nextLine(); 

      Scanner scanner = new Scanner(line); 
      scanner.useDelimiter(","); 
      while (scanner.hasNextDouble()) { 
       list.add(scanner.nextDouble()); 
      } 
      scanner.close(); 
     } 
     */ 
     currentBodyTemp = list; 
     displayBodyTemp("Your Body Temperature Readings are:" + list); 

    } 
    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_body_temp_info); 
     Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar); 
     setSupportActionBar(toolbar); 

     try { 
      displayCurrentBodyTempArray(findViewById(R.id.bodyTempInfoArray)); 
     } catch (FileNotFoundException e) { 
      e.printStackTrace(); 
     } 

     getSupportActionBar().setDisplayHomeAsUpEnabled(true); 
    } 
    private void displayBodyTemp(String bodyTemp) { 
     TextView textView = (TextView) findViewById(R.id.bodyTempInfoArray); 
     textView.setText(bodyTemp); 
    } 


} 

logcat的

10-25 14:15:11.041 11084-11084/? E/Zygote: MountEmulatedStorage() 
10-25 14:15:11.041 11084-11084/? E/Zygote: v2 
10-25 14:15:11.041 11084-11084/? I/libpersona: KNOX_SDCARD checking this for 10385 
10-25 14:15:11.041 11084-11084/? I/libpersona: KNOX_SDCARD not a persona 
10-25 14:15:11.051 11084-11084/? I/SELinux: Function: selinux_compare_spd_ram , priority [2] , priority version is VE=SEPF_SM-G920V_5.1.1_0033 
10-25 14:15:11.051 11084-11084/? E/SELinux: [DEBUG] get_category: variable seinfo: default sensitivity: NULL, cateogry: NULL 
10-25 14:15:11.051 11084-11084/? I/art: Late-enabling -Xcheck:jni 
10-25 14:15:11.111 11084-11084/? D/TimaKeyStoreProvider: TimaSignature is unavailable 
10-25 14:15:11.111 11084-11084/? D/ActivityThread: Added TimaKeyStore provider 
10-25 14:15:11.211 11084-11084/com.teamfara.circadianrhythmmonitor4 I/InjectionManager: Inside getClassLibPath + mLibMap{0=, 1=} 
10-25 14:15:11.231 11084-11084/com.teamfara.circadianrhythmmonitor4 I/InjectionManager: Inside getClassLibPath caller 
10-25 14:15:11.241 11084-11084/com.teamfara.circadianrhythmmonitor4 D/InjectionManager: InjectionManager 
10-25 14:15:11.241 11084-11084/com.teamfara.circadianrhythmmonitor4 D/InjectionManager: fillFeatureStoreMap com.teamfara.circadianrhythmmonitor4 
10-25 14:15:11.241 11084-11084/com.teamfara.circadianrhythmmonitor4 I/InjectionManager: Constructor com.teamfara.circadianrhythmmonitor4, Feature store :{} 
10-25 14:15:11.241 11084-11084/com.teamfara.circadianrhythmmonitor4 I/InjectionManager: featureStore :{} 
10-25 14:15:11.301 11084-11084/com.teamfara.circadianrhythmmonitor4 D/SecWifiDisplayUtil: Metadata value : SecSettings2 
10-25 14:15:11.491 11084-11084/com.teamfara.circadianrhythmmonitor4 D/PhoneWindow: *FMB* installDecor mIsFloating : false 
10-25 14:15:11.491 11084-11084/com.teamfara.circadianrhythmmonitor4 D/PhoneWindow: *FMB* installDecor flags : -2139029248 
10-25 14:15:11.761 11084-11084/com.teamfara.circadianrhythmmonitor4 D/Activity: performCreate Call Injection manager 
10-25 14:15:11.771 11084-11084/com.teamfara.circadianrhythmmonitor4 I/InjectionManager: dispatchOnViewCreated > Target : com.teamfara.circadianrhythmmonitor4.MainActivity isFragment :false 
10-25 14:15:11.791 11084-11175/com.teamfara.circadianrhythmmonitor4 D/OpenGLRenderer: Use EGL_SWAP_BEHAVIOR_PRESERVED: true 
10-25 14:15:11.821 11084-11084/com.teamfara.circadianrhythmmonitor4 D/PhoneWindow: *FMB* isFloatingMenuEnabled mFloatingMenuBtn : null 
10-25 14:15:11.821 11084-11084/com.teamfara.circadianrhythmmonitor4 D/PhoneWindow: *FMB* isFloatingMenuEnabled return false 
10-25 14:15:11.891 11084-11084/com.teamfara.circadianrhythmmonitor4 D/SRIB_DCS: log_dcs ThreadedRenderer::initialize entered! 
10-25 14:15:11.951 11084-11175/com.teamfara.circadianrhythmmonitor4 D/libEGL: loaded /vendor/lib64/egl/libGLES_mali.so 
10-25 14:15:11.991 11084-11175/com.teamfara.circadianrhythmmonitor4 I/OpenGLRenderer: Initialized EGL, version 1.4 
10-25 14:15:12.001 11084-11175/com.teamfara.circadianrhythmmonitor4 I/OpenGLRenderer: HWUI protection enabled for context , &this =0x7f79c8abc0 ,&mEglDisplay = 1 , &mEglConfig = 1912140464 
10-25 14:15:12.011 11084-11175/com.teamfara.circadianrhythmmonitor4 D/OpenGLRenderer: Get maximum texture size. GL_MAX_TEXTURE_SIZE is 8192 
10-25 14:15:12.011 11084-11175/com.teamfara.circadianrhythmmonitor4 D/OpenGLRenderer: Enabling debug mode 0 
10-25 14:15:12.011 11084-11175/com.teamfara.circadianrhythmmonitor4 D/mali_winsys: new_window_surface returns 0x3000, [1440x2560]-format:1 
10-25 14:15:12.391 11084-11084/com.teamfara.circadianrhythmmonitor4 I/InjectionManager: dispatchCreateOptionsMenu :com.teamfara.circadianrhythmmonitor4.MainActivity 
10-25 14:15:12.391 11084-11084/com.teamfara.circadianrhythmmonitor4 I/InjectionManager: dispatchPrepareOptionsMenu :com.teamfara.circadianrhythmmonitor4.MainActivity 
10-25 14:15:12.461 11084-11084/com.teamfara.circadianrhythmmonitor4 I/Timeline: Timeline: Activity_idle id: [email protected] time:4028871 
10-25 14:15:12.801 11084-11084/com.teamfara.circadianrhythmmonitor4 V/ActivityThread: updateVisibility : ActivityRecord{81a0e76 [email protected] {com.teamfara.circadianrhythmmonitor4/com.teamfara.circadianrhythmmonitor4.MainActivity}} show : true 
10-25 14:15:14.701 11084-11084/com.teamfara.circadianrhythmmonitor4 I/Timeline: Timeline: Activity_idle id: [email protected] time:4031112 
10-25 14:15:14.921 11084-11084/com.teamfara.circadianrhythmmonitor4 D/ViewRootImpl: ViewPostImeInputStage ACTION_DOWN 
10-25 14:15:14.971 11084-11084/com.teamfara.circadianrhythmmonitor4 I/Timeline: Timeline: Activity_launch_request id:com.teamfara.circadianrhythmmonitor4 time:4031384 
10-25 14:15:15.051 11084-11084/com.teamfara.circadianrhythmmonitor4 D/PhoneWindow: *FMB* installDecor mIsFloating : false 
10-25 14:15:15.051 11084-11084/com.teamfara.circadianrhythmmonitor4 D/PhoneWindow: *FMB* installDecor flags : -2139029248 
10-25 14:15:15.071 11084-11084/com.teamfara.circadianrhythmmonitor4 D/Activity: performCreate Call Injection manager 
10-25 14:15:15.121 11084-11084/com.teamfara.circadianrhythmmonitor4 I/InjectionManager: dispatchOnViewCreated > Target : com.teamfara.circadianrhythmmonitor4.bodyTempInfo isFragment :false 
10-25 14:15:15.121 11084-11084/com.teamfara.circadianrhythmmonitor4 D/SecWifiDisplayUtil: Metadata value : SecSettings2 
10-25 14:15:15.141 11084-11084/com.teamfara.circadianrhythmmonitor4 D/PhoneWindow: *FMB* isFloatingMenuEnabled mFloatingMenuBtn : null 
10-25 14:15:15.141 11084-11084/com.teamfara.circadianrhythmmonitor4 D/PhoneWindow: *FMB* isFloatingMenuEnabled return false 
10-25 14:15:15.181 11084-11084/com.teamfara.circadianrhythmmonitor4 D/SRIB_DCS: log_dcs ThreadedRenderer::initialize entered! 
10-25 14:15:15.181 11084-11175/com.teamfara.circadianrhythmmonitor4 D/mali_winsys: new_window_surface returns 0x3000, [1440x2560]-format:1 
10-25 14:15:15.201 11084-11084/com.teamfara.circadianrhythmmonitor4 I/InjectionManager: dispatchCreateOptionsMenu :com.teamfara.circadianrhythmmonitor4.bodyTempInfo 
10-25 14:15:15.201 11084-11084/com.teamfara.circadianrhythmmonitor4 I/InjectionManager: dispatchPrepareOptionsMenu :com.teamfara.circadianrhythmmonitor4.bodyTempInfo 
10-25 14:15:15.271 11084-11084/com.teamfara.circadianrhythmmonitor4 I/Timeline: Timeline: Activity_idle id: [email protected] time:4031688 
10-25 14:15:15.311 11084-11084/com.teamfara.circadianrhythmmonitor4 V/ActivityThread: updateVisibility : ActivityRecord{81a0e76 [email protected] {com.teamfara.circadianrhythmmonitor4/com.teamfara.circadianrhythmmonitor4.MainActivity}} show : false 

我content_body_temp_info.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" 
    xmlns:app="http://schemas.android.com/apk/res-auto" android:layout_width="match_parent" 
    android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin" 
    android:paddingRight="@dimen/activity_horizontal_margin" 
    android:paddingTop="@dimen/activity_vertical_margin" 
    android:paddingBottom="@dimen/activity_vertical_margin" 
    app:layout_behavior="@string/appbar_scrolling_view_behavior" 
    tools:showIn="@layout/activity_body_temp_info" 
    tools:context="com.teamfara.circadianrhythmmonitor4.bodyTempInfo"> 

    <TextView 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text = "Body Temperature Measurements will go here" 
     android:id = "@+id/bodyTempInfoArray"/> 
</RelativeLayout> 

我MainActivity.java

package com.teamfara.circadianrhythmmonitor4; 

import android.bluetooth.BluetoothAdapter; 
import android.bluetooth.BluetoothDevice; 
import android.bluetooth.BluetoothServerSocket; 
import android.bluetooth.BluetoothSocket; 
import android.content.BroadcastReceiver; 
import android.content.Context; 
import android.content.Intent; 
import android.content.IntentFilter; 
import android.os.Bundle; 
import android.support.v7.app.AppCompatActivity; 
import android.support.v7.widget.Toolbar; 
import android.view.Menu; 
import android.view.MenuItem; 
import android.view.View; 
import android.widget.ArrayAdapter; 
import android.widget.TextView; 

import java.io.FileOutputStream; 
import java.io.IOException; 
import java.io.InputStream; 
import java.io.OutputStream; 
import java.util.ArrayList; 
import java.util.Set; 
import java.util.UUID; 


public class MainActivity extends AppCompatActivity { 


    private final static int REQUEST_ENABLE_BT = 1; 
    UUID myUUID = UUID.randomUUID(); 
    private static final int DISCOVER_DURATION = 300; 
    private static final int REQUEST_BLU = 1; 
    ArrayAdapter<String> mArrayAdapter; 
    ArrayList<Double> currentBodyTemp; 
    BluetoothAdapter mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter(); 


    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 
     Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar); 
     setSupportActionBar(toolbar); 

     BluetoothAdapter mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter(); 
     if (!mBluetoothAdapter.isEnabled()) { 
      Intent enableBIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE); 
      startActivityForResult(enableBIntent, REQUEST_ENABLE_BT); 
     } 

     //If there are paired devices 
     /*Set<BluetoothDevice> pairedDevices = mBluetoothAdapter.getBondedDevices(); 
     if (pairedDevices.size() > 0) { 
      //Loop through the paired devices 
      for (BluetoothDevice device : pairedDevices) { 
       //add the name and address to an array adapter to show in a ListView 
       mArrayAdapter.add(device.getName() + "\n" + device.getAddress()); 
      } 
     } 
     */ 

     IntentFilter filter = new IntentFilter(BluetoothDevice.ACTION_FOUND); 
     registerReceiver(mReceiver, filter); // Don't forget to unregister during onDestroy 

     Intent discoverableIntent = new 
       Intent(BluetoothAdapter.ACTION_REQUEST_DISCOVERABLE); 
     discoverableIntent.putExtra(BluetoothAdapter.EXTRA_DISCOVERABLE_DURATION, 300); 
     startActivity(discoverableIntent); 




    } 

    ArrayList<Double> lightInfo; 

    public void sendLightFile() { 

     String filename = "lightInfo"; 
     String string = "Here is light info" + lightInfo; 
     FileOutputStream outputStream; 

     try { 
      outputStream = openFileOutput(filename, Context.MODE_PRIVATE); 
      outputStream.write(string.getBytes()); 
      outputStream.close(); 
     } catch (Exception e) { 
      e.printStackTrace(); 
     } 

     // Send information over bluetooth? 

    } 



    public void goToBodyInfo(View view) { 
     Intent intent = new Intent(this, bodyTempInfo.class); 
     startActivity(intent); 

    } 

    @Override 
    public boolean onCreateOptionsMenu(Menu menu) { 
     // Inflate the menu; this adds items to the action bar if it is present. 
     getMenuInflater().inflate(R.menu.menu_main, menu); 
     return true; 
    } 

    @Override 
    public boolean onOptionsItemSelected(MenuItem item) { 
     // Handle action bar item clicks here. The action bar will 
     // automatically handle clicks on the Home/Up button, so long 
     // as you specify a parent activity in AndroidManifest.xml. 
     int id = item.getItemId(); 

     //noinspection SimplifiableIfStatement 
     if (id == R.id.action_settings) { 
      return true; 
     } 

     return super.onOptionsItemSelected(item); 
    } 

    public void sendLight(View view) { 

     Intent intent = new Intent(this, ConnectTest.class); 
     startActivity(intent); 

    } 


    // Create a BroadcastReceiver for ACTION_FOUND 
    private final BroadcastReceiver mReceiver = new BroadcastReceiver() { 
     public void onReceive(Context context, Intent intent) { 
      String action = intent.getAction(); 
      // When discovery finds a device 
      if (BluetoothDevice.ACTION_FOUND.equals(action)) { 
       // Get the BluetoothDevice object from the Intent 
       BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE); 
       // Add the name and address to an array adapter to show in a ListView 
       mArrayAdapter.add(device.getName() + "\n" + device.getAddress()); 
      } 
     } 
    }; 


    private class AcceptThread extends Thread { 
     private final BluetoothServerSocket mmServerSocket; 
     BluetoothAdapter mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter(); 
     Set<BluetoothDevice> pairedDevices = mBluetoothAdapter.getBondedDevices(); 


     public AcceptThread() { 
      // Use a temporary object that is later assigned to mmServerSocket, 
      // because mmServerSocket is final 
      BluetoothServerSocket tmp = null; 
      try { 
       // MY_UUID is the app's UUID string, also used by the client code 
       tmp = mBluetoothAdapter.listenUsingRfcommWithServiceRecord("CircadianRhythmMonitor", myUUID); 
      } catch (IOException e) { 
      } 
      mmServerSocket = tmp; 
     } 

     public void run() { 
      BluetoothSocket socket = null; 
      // Keep listening until exception occurs or a socket is returned 
      while (true) { 
       try { 
        socket = mmServerSocket.accept(); 
       } catch (IOException e) { 
        break; 
       } 
       // If a connection was accepted 
       if (socket != null) { 
        // Do work to manage the connection (in a separate thread) 
        //manageConnectedSocket(socket); 
        //mmServerSocket.close(); 
        break; 
       } 
      } 
     } 

     /** 
     * Will cancel the listening socket, and cause the thread to finish 
     */ 
     public void cancel() { 
      try { 
       mmServerSocket.close(); 
      } catch (IOException e) { 
      } 
     } 
    } 

    private class ConnectedThread extends Thread { 
     private final BluetoothSocket mmSocket; 
     private final InputStream mmInStream; 
     private final OutputStream mmOutStream; 

     public ConnectedThread(BluetoothSocket socket) { 
      mmSocket = socket; 
      InputStream tmpIn = null; 
      OutputStream tmpOut = null; 

      // Get the input and output streams, using temp objects because 
      // member streams are final 
      try { 
       tmpIn = socket.getInputStream(); 
       tmpOut = socket.getOutputStream(); 
      } catch (IOException e) { } 

      mmInStream = tmpIn; 
      mmOutStream = tmpOut; 
     } 

     public void run() { 
      byte[] buffer = new byte[1024]; // buffer store for the stream 
      int bytes; // bytes returned from read() 

      // Keep listening to the InputStream until an exception occurs 
      while (true) { 
       try { 
        // Read from the InputStream 
        bytes = mmInStream.read(buffer); 
        // Send the obtained bytes to the UI activity 
        // mHandler.obtainMessage(MESSAGE_READ, bytes, -1, buffer) 
          //.sendToTarget(); 
       } catch (IOException e) { 
        break; 
       } 
      } 
     } 

     /* Call this from the main activity to send data to the remote device */ 
     public void write(byte[] bytes) { 
      try { 
       mmOutStream.write(bytes); 
      } catch (IOException e) { } 
     } 

     /* Call this from the main activity to shutdown the connection */ 
     public void cancel() { 
      try { 
       mmSocket.close(); 
      } catch (IOException e) { } 
     } 
    } 


    private void displayBodyTemp(String bodyTemp) { 
     TextView textView = (TextView) findViewById(R.id.bodyTempInfoArray); 
     textView.setText(bodyTemp); 
    } 



} 

我AndroidManfiest.xml

package="com.teamfara.circadianrhythmmonitor4" > 

    <!-- Allows bluetooth to be used from this app --> 
    <uses-permission android:name="android.permission.BLUETOOTH" /> 
    <uses-permission android:name="android.permission.BLUETOOTH_ADMIN" /> 
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> 
    <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" /> 
    <uses-permission android:name="android.permission.NFC" /> 

    <application 
     android:allowBackup="true" 
     android:icon="@mipmap/ic_launcher" 
     android:label="@string/app_name" 
     android:supportsRtl="true" 
     android:theme="@style/AppTheme" > 
     <activity 
      android:name=".MainActivity" 
      android:label="@string/app_name" 
      android:theme="@style/AppTheme.NoActionBar" > 
      <intent-filter> 
       <action android:name="android.intent.action.MAIN" /> 

       <category android:name="android.intent.category.LAUNCHER" /> 
      </intent-filter> 
     </activity> 
     <activity 
      android:name=".bodyTempInfo" 
      android:label="@string/title_activity_body_temp_info" 
      android:parentActivityName=".MainActivity" 
      android:theme="@style/AppTheme.NoActionBar" > 
      <meta-data 
       android:name="android.support.PARENT_ACTIVITY" 
       android:value="com.teamfara.circadianrhythmmonitor4.MainActivity" /> 
     </activity> 
     <activity 
      android:name=".ConnectTest" 
      android:label="@string/title_activity_body_temp_info" 
      android:parentActivityName=".MainActivity" 
      android:theme="@style/AppTheme.NoActionBar" > 
      <meta-data 
       android:name="android.support.PARENT_ACTIVITY" 
       android:value="com.teamfara.circadianrhythmmonitor4.MainActivity" /> 
     </activity> 
    </application> 
    <uses-feature 
     android:name="android.hardware.nfc" 
     android:required="true" /> 

</manifest> 
+0

你爲什麼要張貼的logcat的?關於你的問題的是什麼?你爲什麼發佈一個XML文件?請將您的代碼逐行減少到閱讀該文件的相關部分。不要只是轉儲代碼。 – greenapps

+0

道歉@greenapps我認爲這可能是相關的身體tempinfo文件被引用在日誌cathe作爲閱讀null,我認爲我的issuemail是 –

回答

0

在你displayBodyTemp()功能,你傳遞一個字符串並使用+添加數組列表。這是故事。你只需要傳遞一個字符串。或覆蓋的Arraylist

toString()法,試試這個:

displayBodyTemp("Your Body Temperature Readings are:" + text);