2013-08-26 2 views
0

그래서 App을 시작하고 MainActivity의 onCreate 메소드에서 변수 값을 변경합니다. 그러나 다른 Activity로 이동하게하는 버튼을 클릭하고이 Activity에서 변경된 변수를 가져 오려고하면 변수의 원래 값만 가져옵니다.다른 클래스에서 변경된 변수를 가져 오시겠습니까?

내 문제는 여기에서 첫 번째 클래스에서 변경된 변수를 가져 오는 방법을 모르겠다는 것입니다. 어떻게해야합니까?

주요 활동 :

package com.example.getandset; 

import android.app.Activity; 
import android.content.Intent; 
import android.os.Bundle; 
import android.view.View; 

public class MainActivity extends Activity { 

    //Initialised variable 
    String tag; 

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

     //The text that I want to show in the next class 
     setTag("Show this text"); 
    } 

    public void setTag(String t){ 
     tag =t; 
    } 

    public String getTag(){ 
     return tag; 
    } 

    public void onClick_Start(View v){ 
//start of the next Class 
     startActivity(new Intent(this, CalledActivity.class)); 
    } 

} 

및 호출 활동 : 여기

코드입니다

package com.example.getandset; 

import android.os.Bundle; 
import android.app.Activity; 
import android.widget.TextView; 

public class CalledActivity extends Activity { 

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

     TextView tv = (TextView)findViewById(R.id.textView); 
//trying to get the changed variable 
     tv.setText("Got text: "+ new MainActivity().getTag()); 
    } 


} 
+0

http://developer.android.com/training/basics/firstapp/index.html 주제 : 다른 활동 시작 – Selvin

+0

태그를 의도에 추가하거나 공유 환경 설정에 추가 할 수 있습니다. – Sambuca

+0

Activity에서 전달 된 값을 가져오고 싶습니다. –

답변

0
public void onClick_Start(View v){ 
//start of the next Inten 
    Intent intent = new Intent(this, CalledActivity.class) 
    intent.putExta("key",getTag()) ; 
    startActivity(intent); 
} 

는에서 onCreate 방법으로 CalledActivity에서 태그 텍스트를 전달받을

String value = getIntent().getStringExtra("key"); 
0

tagnull으로 초기화 된 MainActivity의 새 인스턴스를 만듭니다. 일반적으로 Activity을 이와 같이 인스턴스화해서는 안되며, 항상 startActivity을 사용하여 시스템에서 시작하도록하십시오.

  • 가 다른 하나 개의 활동에서 태그 값을 전송하는 CalledActivity
  • 사용 영구 저장소를 시작하는 의도에 태그를 전달합니다

    원하는 효과를 달성하는 방법은 여러 가지가 있습니다.

0

주 활동의 데이터 호출이 있어서는 안됩니다. 그것은 객체 클래스와 같지 않으므로 동작이 다릅니다. (문제는 객체의 초기화 된 인스턴스가 아니라 클래스를 호출한다는 사실에 있습니다)

데이터 묶음이 포함 된 INTENT를 사용하여 활동을 시작해야합니다.

public void sendMessage(View view) { 
    Intent intent = new Intent(this, DisplayMessageActivity.class); 
    EditText editText = (EditText) findViewById(R.id.edit_message); 
    String message = editText.getText().toString(); 
    intent.putExtra(EXTRA_MESSAGE, message); 
    startActivity(intent); 
} 

(이 안드로이드 디바이스 가이드에서 직선)

다음 두 번째 활동에, 당신은 당신이 변수의 변수/유형의 번호로이 작업을 수행 할 수 있습니다

Intent intent = getIntent(); 
String message = intent.getStringExtra(MainActivity.EXTRA_MESSAGE); 

를 사용합니다.

다른 옵션은 변수를 "보유"할 객체 클래스를 만드는 것입니다. 위와 같은 방법으로 객체를 전달한 다음 .getVariable()을 사용하여 클래스의 변수를 정상적으로 호출합니다.

관련 문제