두 클래스 DashboardActivity.class 및 ProfileActivity.class가 있습니다.버튼을 통해 값을 안드로이드 코드의 다른 활동으로 전달하는 방법은 무엇입니까?
Dashboard 클래스에서 Button을 통해 ProfileActivity 클래스에 값을 전달하고 싶습니다. 하지만 그것은 'JSON이'필수 입력란이 누락되었습니다 '라는 오류와 응답을 계속받습니다. 내가 체크했을 때 대시 보드의 값이 ProfileActivity로 전달되지 않았으므로 응답에서 '필수 필드가 누락되었습니다'라는 메시지가 계속 표시됩니다.
내 질문은, 단추를 통해 값을 다른 활동으로 전달하는 방법입니다. 이미 다음 코드를 사용합니다 :
btnLinkToProfile = (Button) findViewById(R.id.btnProfile);
btnLinkToProfile.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View view) {
// TODO Auto-generated method stub
String name = ((TextView) view.findViewById(R.id.name)).getText().toString();
//userFunctions.userProfil(INPUT_METHOD_SERVICE);
Intent i = new Intent(getApplicationContext(),ProfileActivity.class);
i.putExtra(KEY_NAME, name);
startActivityForResult(i,0);
}
하지만 실행하려고하면 강제로 닫힙니다. 다음은 대시 보드 활동의 전체 코드입니다 :
public class DashboardActivity extends Activity {
UserFunctions userFunctions;
Button btnLogout;
Button btnLinkToProfile;
private static final String KEY_NAME = "name";
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
/**
* Dashboard Screen for the application
* */
// Check login status di database
userFunctions = new UserFunctions();
if(userFunctions.isUserLoggedIn(getApplicationContext())){
// user already logged in show dashboard
setContentView(R.layout.dashboard);
//JSONObject json = jParserr.getJSONFromUrl(dashboardURL, "GET", params);
btnLinkToProfile = (Button) findViewById(R.id.btnProfile);
btnLinkToProfile.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View view) {
// TODO Auto-generated method stub
String name = ((TextView) view.findViewById(R.id.name)).getText().toString();
//userFunctions.userProfil(INPUT_METHOD_SERVICE);
Intent i = new Intent(getApplicationContext(),ProfileActivity.class);
i.putExtra(KEY_NAME, name);
startActivityForResult(i,0);
//startActivity(i);
//finish();
}
});
btnLogout = (Button) findViewById(R.id.btnLogout);
btnLogout.setOnClickListener(new View.OnClickListener() {
public void onClick(View arg0) {
// TODO Auto-generated method stub
userFunctions.logoutUser(getApplicationContext());
Intent login = new Intent(getApplicationContext(), MainActivity.class);
login.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(login);
// Keluar dari dashboard screen
finish();
}
});
}else{
// user is not logged in show login screen
Intent login = new Intent(getApplicationContext(), MainActivity.class);
login.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(login);
// Closing dashboard screen
finish();
}
}
}
그리고 여기 ProfileActivity 클래스입니다 :
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.profile);
// Loading user Profile in Background Thread
new showUserProfile().execute();
}
///...
/**
* Background Async Task to Load user profile by making HTTP Request
* */
private class showUserProfile extends AsyncTask<String, String, String> {
/**
* Before starting background thread Show Progress Dialog
* */
@Override
protected void onPreExecute() {
super.onPreExecute();
pDialog = new ProgressDialog(ProfileActivity.this);
pDialog.setMessage("Loading User Profile. Please wait...");
pDialog.setIndeterminate(false);
pDialog.setCancelable(false);
pDialog.show();
}
/**
* getting user profile from url
* */
protected String doInBackground(String... args) {
//String name = name.getText().toString();
// Building Parameters
List<NameValuePair> params = new ArrayList<NameValuePair>();
// getting JSON string from URL
JSONObject json = jParser.getJSONFromUrl(profileURL, "GET", params);
/* UserFunctions userFunction = new UserFunctions();
if (params.length != 0)
return null;
JSONObject json = userFunction.userProfil(params[0]);
return json; */
// Check your log cat for JSON reponse
Log.d("User Profile: ", json.toString());
...
return null;
}
}
}
당신의'DashboardActivity' 당신이'문자열 이름 = ((텍스트 뷰) findViewById를 (R.id.name)). gettext에(). toString을 시도했다 (다음
사용
Bundle var = getIntent().getExtras()
);''Button' 클릭 안쪽? 대신 String name = ((TextView) view.findViewById (R.id.name)) .getText(). toString();'? –
@Shobhit Puri 예, 이미 문자열 이름 = ((TextView) findViewById (R.id.name)). getText(). toString(); 버튼의 clickListener에 있습니다. – user2476947
이 코드를 컴파일해도됩니까? 어떻게 TextView를 문자열로 변환 할 수 있습니까? 또한 불필요한 코드를 백만 줄 게시하는 이유는 무엇입니까? –