2013-01-01 4 views
1

나는 새로운 활동 (forresult) 에 국가를 쓸 때 집에서 할 일이 생겼습니다. 주 활동과 쓰기 활동에 글을 쓸 것이고 새로운 활동은 글의 색깔을 나는 썼다. 하지만 확인 및 취소 버튼 못해 성공하지 솔루션에 대한 검색 정보를 전송안드로이드 | StartActivityForResult 아무 것도하지 않습니다

MainActivity -

public class MainActivity extends Activity implements OnClickListener { 
TextView tvDisplayCountry; 
Random crazy; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    requestWindowFeature(Window.FEATURE_NO_TITLE); 
    getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, 
      WindowManager.LayoutParams.FLAG_FULLSCREEN); 
    setContentView(R.layout.activity_main); 

    initsialize(); 

} 

private void initsialize() { 
    Button bAddCountry = (Button) findViewById(R.id.bAddCountry); 
    bAddCountry.setOnClickListener(this); 
    tvDisplayCountry = (TextView) findViewById(R.id.tvDisplayCountry); 
    // ImageView ivCountryPhoto = 
    // (ImageView)findViewById(r.id.ivCountryPhoto); 
} 

@Override 
public void onClick(View v) { 

    switch (v.getId()) { 

    case R.id.bAddCountry: 
     Intent countryactivityIntent = new Intent(MainActivity.this, 
       CountryActivity.class); 
     startActivityForResult(countryactivityIntent, 12); 

     break; 
    } 

} 

@Override 
protected void onActivityResult(int requestCode, int resultCode, Intent data) { 


    switch(resultCode){ 
    case 12: 
     if (resultCode == Activity.RESULT_OK) { 
      String Country = data.getStringExtra("country"); 
      String Color = data.getStringExtra("color"); 
      tvDisplayCountry.setText(Country); 
      if (Color.equalsIgnoreCase("blue")) { 
       tvDisplayCountry.setTextColor(android.graphics.Color.BLUE); 
      } else if (Color.equalsIgnoreCase("yellow")) { 
       tvDisplayCountry 
       .setTextColor(android.graphics.Color.YELLOW); 
      } else if (Color.equalsIgnoreCase("gray")) { 
       tvDisplayCountry.setTextColor(android.graphics.Color.GRAY); 
      } else if (Color.equalsIgnoreCase("red")) { 
       tvDisplayCountry.setTextColor(android.graphics.Color.RED); 
      } 
      else{ 
       Toast.makeText(MainActivity.this, "canceld", Toast.LENGTH_SHORT).show(); 
      } 
      break; 
     } 
    } 

    super.onActivityResult(requestCode, resultCode, data); 
} 

}

CountryActivity -에 오류가

public class CountryActivity extends Activity implements OnClickListener { 
EditText etCountry, etColor; 
String getCountry, getColor; 
Button bOk,bCancel; 
@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    requestWindowFeature(Window.FEATURE_NO_TITLE); 
    getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN); 
    setContentView(R.layout.activity_country); 



    init(); 


} 

private void init() { 
    etColor = (EditText)findViewById(R.id.etColor); 
    etCountry = (EditText)findViewById(R.id.etCountry); 
    bOk = (Button)findViewById(R.id.bOk); 
    bCancel = (Button)findViewById(R.id.bCancel); 
    bOk.setOnClickListener(this); 
    bCancel.setOnClickListener(this); 


} 

@Override 
public void onClick(View v) { 

    switch (v.getId()) { 
    case R.id.bOk: 
     Intent myIntet = new Intent(); 
     getCountry = etCountry.getText().toString(); 
     myIntet.putExtra("country",getCountry); 
     getColor = etColor.getText().toString(); 
     myIntet.putExtra("color", getColor); 

     setResult(Activity.RESULT_OK, myIntet); 
     finish(); 

     break; 

    case R.id.bCancel: 
     Intent myIntent = new Intent(); 

     setResult(Activity.RESULT_CANCELED, myIntent); 
     finish(); 
     break; 
    } 

} 
+0

LogCat에서 오류가 발생 했습니까? –

+0

코드에 Log.i ("", "Something usefull")를 추가하고 문제가있는 곳을 확인하십시오. 우리는 전체 코드를 테스트 할 수 없습니다! –

+0

로그에 아무런 문제가 없으며 취소하거나 주 활동이 변경되지 않고 열립니다. –

답변

2

이 당신의 onActivityResult - resultCode을 사용합니다 (RESULT_OK 또는).이 아니라 12).

switch(resultCode)switch(requestCode)으로 변경하십시오.

+0

내 관심을 가져 주셔서 감사합니다! 문제 해결됨 –

관련 문제