2017-01-18 12 views
1

私は、1つのアクティビティ(LoginActivity)から別のアクティビティ(SettingsActivity)に文字列を渡すのに多くの時間をかけました。もちろん、私は多くの研究を行なったし、それぞれ提供される解決策を試してみましたが、何も今まで私を助けることができなかった他のアクティビティに文字列を渡すことができません

LoginActivity.class:

package com.naderk.pds; 

import android.content.Intent; 
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.util.Log; 
import android.view.View; 
import android.view.Menu; 
import android.view.MenuItem; 
import android.widget.Button; 
import android.widget.EditText; 
import android.widget.Toast; 
import com.kosalgeek.genasync12.AsyncResponse; 
import com.kosalgeek.genasync12.PostResponseAsyncTask; 
import com.naderk.pds.contexts.ContextServerActivity; 

import java.util.HashMap; 

public class LoginActivity extends AppCompatActivity implements View.OnClickListener { 

    final String LOG = "LoginActivity"; 

    Button btnLogin, btnRegister; 
    EditText etUsername, etPassword; 

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

     etUsername = (EditText) findViewById(R.id.etUsername); 
     etPassword = (EditText) findViewById(R.id.etPassword); 

     btnLogin = (Button) findViewById(R.id.btnLogin); 
     btnRegister = (Button) findViewById(R.id.bRegister); 

     btnLogin.setOnClickListener(this); 

     btnRegister.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View v) { 
       Intent seeTasksIntent= new Intent(LoginActivity.this, RegisterActivity.class); 
       LoginActivity.this.startActivity(seeTasksIntent); 
      } 
     }); 

    } 

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

    @Override 
    public void onClick(View view) { 
     HashMap postData = new HashMap(); 

     String username = etUsername.getText().toString(); 
     String password = etPassword.getText().toString(); 

     postData.put("txtUsername", username); 

     postData.put("txtPassword", password); 

     PostResponseAsyncTask task1 = new PostResponseAsyncTask(LoginActivity.this, postData, new AsyncResponse() { 
      @Override 
      public void processFinish(String s) { 
       Log.d(LOG, s); 
       if(s.contains("success")){ 
        Toast.makeText(LoginActivity.this, "Login successful", Toast.LENGTH_LONG).show(); 
        Intent intent = new Intent(LoginActivity.this, MainActivity.class); 
        startActivity(intent); 
       } else { 
        Toast.makeText(LoginActivity.this, "Wrong username or password. Please register and try again.", Toast.LENGTH_LONG).show(); 
       } 
      } 
     }); 
     task1.execute("http://192.168.0.11/customer/"); 

     visitactivity1(); 

    } 

    public void visitactivity1() { 
     Intent i = new Intent(LoginActivity.this, SettingsActivity.class); 
     Bundle bundle = new Bundle(); 
     bundle.putString("key", etUsername.getText().toString()); 
     i.putExtras(bundle); 
     startActivity(i); 
     finish(); 
    } 
} 

SettingsActivity.class:

package com.naderk.pds; 

import android.support.v7.app.AppCompatActivity; 
import android.os.Bundle; 
import android.widget.TextView; 
import android.content.Intent; 

public class SettingsActivity extends AppCompatActivity { 


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

     TextView textView = (TextView) findViewById(R.id.tvUserName); 

     Bundle bundle = getIntent().getExtras(); 

     String stuff = bundle.getString("key"); 

     textView.setText(stuff); 

    } 
} 

ログcat:

Exceeds 30.000 characters lol 

誰かが私を助けてくれることを本当に願っています。

乾杯!

+0

logcatからスタックトレースを共有できますか? – Supreethks

答えて

1

この

public void visitactivity1() { 
     Intent i = new Intent(LoginActivity.this, SettingsActivity.class); 
     i.putExtra("key", etUsername.getText().toString()); 
     startActivity(i); 
     finish(); 
    } 

を試してみて、第二の活動に

public class SettingsActivity extends AppCompatActivity { 


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

      TextView textView = (TextView) findViewById(R.id.tvUserName); 


      String stuff = getIntent().getStringExtra("key"); 

      textView.setText(stuff); 

     } 
    } 

はそれがお役に立てば幸いです(:

0

あなたはあなたの第二のアクティビティにputExtras

if(s.contains("success")){ 
       Toast.makeText(LoginActivity.this, "Login successful", Toast.LENGTH_LONG).show(); 
       Intent intent = new Intent(LoginActivity.this, MainActivity.class); 
       intent.putExtra("passwordText", password.getText().toString()); 
       intent.putExtra("usernameText",username.getText().toString()); 
       startActivity(intent); 

を追加することができますあなたが意図についての詳細は、このブログを読むことができますあなたのonCreate()メソッド

Intent intent = getIntent(); 
    String password = intent.getStringExtra("passwordText"); 
    String username = intent.getSringExtra("usernameText"); 

以内に次のように実装します。要するにWhat are intents はインテントを実装する方法についてのチュートリアルです。構造を変更したい場合に役立ちます。

最後に、これがデータを渡す方法であるかどうかはわかりませんが、代わりに私が推奨するように使用してください。私はそれが助けて欲しい!

0

バンドルメソッドを使用すると、アクティビティクラス全体でストリングを送受信できます。

関連する問題