2016-05-23 78 views
0

我在兩項活動之間發送數據時遇到問題 - 我是編程新手,認爲它與代碼的定位有關,但我不確定,可能更多簡單一些。在兩項活動之間發送數據Android Studio

我試圖將stepCount中的數字發送到第二個活動,它存儲在myText3中,並且在第一個活動中可見,並且計數正確(它的原始數據但現在完成此工作)。

如果我的值設置爲myText3它會顯示在第二個活動是價值,但它不會顯示的stepCount當其在myText3

在此先感謝..

第一個活動

import android.content.Intent; 
import android.os.CountDownTimer; 
import android.support.v7.app.AppCompatActivity; 
import android.view.View.OnClickListener; 



     import android.hardware.Sensor; 
     import android.hardware.SensorEvent; 
     import android.hardware.SensorEventListener; 
     import android.hardware.SensorManager; 
     import android.os.Bundle; 
     //import android.support.design.widget.FloatingActionButton; 
     //import android.support.design.widget.Snackbar; 
     import android.support.v7.app.AppCompatActivity; 
     import android.support.v7.widget.Toolbar; 
     import android.view.View; 
     import android.view.Menu; 
     import android.view.MenuItem; 
import android.widget.Button; 
import android.widget.EditText; 
import android.widget.TextView; 

public class MainActivity extends AppCompatActivity implements SensorEventListener { 
    TextView tvx, tvy, tvz, display, display2, display3; 
    float total; 
    float stepCount; 
    float x, y, z; 
    float xPrev,yPrev, zPrev; 
    float totalPrev = 0; 
    Button saveButton; 



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

     /*FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab); 
     fab.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View view) { 
       Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG) 
         .setAction("Action", null).show(); 
      } 
     });*/ 



     tvx = (TextView) findViewById(R.id.textView); 
     tvy = (TextView) findViewById(R.id.textView2); 
     tvz = (TextView) findViewById(R.id.textView3); 
     display = (TextView) findViewById(R.id.display); 
     display2 = (TextView) findViewById(R.id.display2); 
     display3 = (TextView) findViewById(R.id.display3); 
     saveButton = (Button) findViewById(R.id.saveButton); 

     SensorManager mgr = (SensorManager) getSystemService(SENSOR_SERVICE); 
     Sensor GSensor = mgr.getDefaultSensor(Sensor.TYPE_ACCELEROMETER); 
     mgr.registerListener(this, GSensor, SensorManager.SENSOR_DELAY_NORMAL); 



    } 

    @Override 
    public void onSensorChanged(SensorEvent event) { 
     // called byt the system every 10 ms 



     x = event.values[0]; 
     y = event.values[1]; 
     z = event.values[2]; 
     total = x + y + z; 

     new CountDownTimer(3000, 1000) { 
      public void onFinish() { 
       // When timer is finished 
       // Execute your code here 
       xPrev = x; 
       yPrev = y; 
       zPrev = z; 
       totalPrev = xPrev + yPrev + zPrev; 
      } 

      public void onTick(long millisUntilFinished) { 
       // millisUntilFinished The amount of time until finished. 
      } 
     }.start(); 



     if (Math.abs(total - totalPrev) > 20) { 
      stepCount++; 
     } 


     tvx.setText(String.valueOf(event.values[0])); 
     tvy.setText(String.valueOf(event.values[1])); 
     tvz.setText(String.valueOf(event.values[2])); 

     String mytext = Float.toString(total); 
     display.setText(mytext); 

     String mytext2 = Float.toString(totalPrev); 
     display2.setText(mytext2); 

     String mytext3 = Float.toString(stepCount); 
     display3.setText(mytext3); 

     saveButton.setOnClickListener(new View.OnClickListener() { 
      String mytext3; 
      @Override 
      public void onClick(View v) { 
       Intent A2Intent = new Intent (v.getContext(), Main2Activity.class); 
       //EditText et = (EditText) findViewById(R.id.editText); 
       // pass the entered phone number to the next activity 
       //A2Intent.putExtra("phoneNumber", tv2.getText()); 
       //String ph = et.getText().toString(); 
       A2Intent.putExtra("steps", mytext3); 
       // start the next activity/page 
       startActivity(A2Intent); 
      } 
     }); 








    } 

    @Override 
    public void onAccuracyChanged(Sensor sensor, int accuracy) { 

    } 

    /*@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); 
    }*/ 
    /*saveButton.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View v) { 
      Intent A2Intent = new Intent (v.getContext(), Main2Activity.class); 
      EditText et = (EditText) findViewById(R.id.editText); 
      // pass the entered phone number to the next activity 
      //A2Intent.putExtra("phoneNumber", tv2.getText()); 
      String ph = et.getText().toString(); 
      A2Intent.putExtra("phoneNumber", ph); 
      // start the next activity/page 
      startActivity(A2Intent); 
     } 
    })*/ 

} 

次活動

import android.support.v7.app.AppCompatActivity; 
import android.os.Bundle; 
import android.widget.Button; 
import android.widget.EditText; 
import android.widget.TextView; 

public class Main2Activity extends AppCompatActivity { 

    TextView textView1; 
    Button newButton; 
    String displayCount; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main2); 

     textView1 = (TextView) findViewById(R.id.editText); 
     newButton = (Button) findViewById(R.id.button); 

     displayCount = getIntent().getStringExtra("steps"); 
     textView1.setText(displayCount); 

    } 
} 
+0

在開始第二個活動之前做一個日誌,以查看stepCount和mtText3的值 –

回答

0

在saveButton onClickListener中,您正在創建一個新的String mytext3,然後將空的未初始化的字符串傳遞給您的意圖。相反,你可以做的是使用findViewById來檢索TextView的實際mytext3,然後通過在

0

String mytext3 = Float.toString(stepCount);saveButtononClick()裏面是這樣的:

saveButton.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View v) { 
      Intent A2Intent = new Intent (v.getContext(), Main2Activity.class); 
      String mytext3 = Float.toString(stepCount); 
      A2Intent.putExtra("steps", mytext3); 
      startActivity(A2Intent); 
     } 
    }); 
+0

可愛的,這解決了它..謝謝。 – iamliam

+0

有一個問題,我已經把'stepCount'放在'mytext3'這裏..'String mytext3 = Float.toString(stepCount); display3.setText(mytext3);'爲什麼我必須重做它才能工作? – iamliam

+0

,因爲您傳遞的是您在onclicklistener方法中創建的字符串mytext3。 –

0
saveButton.setOnClickListener(new View.OnClickListener() { 
       // String mytext3; 
       //you are giving intent a empty string here comment above line 
       @Override 
       public void onClick(View v) { 
        Intent A2Intent = new Intent (v.getContext(), Main2Activity.class); 
        //EditText et = (EditText) findViewById(R.id.editText); 
        // pass the entered phone number to the next activity 
        //A2Intent.putExtra("phoneNumber", tv2.getText()); 
        //String ph = et.getText().toString(); 
        A2Intent.putExtra("steps", mytext3); 
        // start the next activity/page 
        startActivity(A2Intent); 
       } 
      }); 

後評論你必須final String mytext3訪問onClick。

相關問題