2016-05-23 5 views
0

2つのアクティビティ間でデータを送信する際に問題が発生しました。プログラミングに慣れておらず、コードの位置付けと何かが関係していると思いますが、単純なもの。2つのアクティビティ間でデータを送信する

私はこの数字をmyText3に保存された2番目のアクティビティに送信しようとしています。最初のアクティビティでは表示されており、そのプリミティブが現在カウントされています。

私はmyText3に値を設定する場合は、第2の活動で、その値が表示されますが、それは文句を言わない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の両方の値を確認するために2回目のアクティビティを開始する直前にログを作成します –

答えて

0

saveButton onClickListenerでは、新しいString mytext3を作成してから、空の初期化されていない文字列をインテントに渡します。代わりに、何を行う可能性は、TextViewの中に実際のmytext3を取得するためにfindViewByIdを使用し、その後にそれを渡すことで、このようなあなたのsaveButtononClick()内側に入れ

0

String mytext3 = Float.toString(stepCount);:。

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

1つの質問tho、私はすでに '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); 
       } 
      }); 

後あなたはonClickでそれにアクセスするにはfinal String mytext3にコメントする必要があります。

関連する問題