마스터Q&A 안드로이드는 안드로이드 개발자들의 질문과 답변을 위한 지식 커뮤니티 사이트입니다. 안드로이드펍에서 운영하고 있습니다. [사용법, 운영진]

안드로이드 킷캣으로 업그레이드 되면서 getIntent() 질문?

0 추천
package com.example.sampleflagintentsjh;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v7.app.ActionBarActivity;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;

public class MainActivity extends ActionBarActivity {

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

  if (savedInstanceState == null) {
   getSupportFragmentManager().beginTransaction()
     .add(R.id.container, new PlaceholderFragment()).commit();
  }
 }

 @Override
 public boolean onCreateOptionsMenu(Menu menu) {

  // Inflate the menu; this adds items to the action bar if it is present.
  getMenuInflater().inflate(R.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();
  if (id == R.id.action_settings) {
   return true;
  }
  return super.onOptionsItemSelected(item);
 }

 /**
  * A placeholder fragment containing a simple view.
  */
 public static class PlaceholderFragment extends Fragment {

  public PlaceholderFragment() {
  }
  Button showBtn;
  TextView txtMsg;
  String msg;
  
  /** 요청 코드 정의 **/
  public static final int REQUEST_CODE_ANOTHER = 1001;
  
  /** 시작 횟수 **/
  public static int startCount = 0;


  @Override
  public View onCreateView(LayoutInflater inflater, ViewGroup container,
    Bundle savedInstanceState) {
   View rootView = inflater.inflate(R.layout.fragment_main, container,
     false);
   
   showBtn = (Button)rootView.findViewById(R.id.showBtn);
   txtMsg = (TextView)rootView.findViewById(R.id.txtMsg);
   
   showBtn.setOnClickListener(new OnClickListener(){
    public void onClick(View v) {
     
     Intent receivedIntent = getIntent();
        startCount = receivedIntent.getIntExtra("startCount", 0);
    
        // 텍스트뷰에 startCount 값을 보여줍니다.
        msg = "전달된 startCount : " + startCount;
        txtMsg.setText(msg);
    }
    
   });
   
   // 전달받은 인텐트를 처리합니다.
   Intent receivedIntent = getIntent();
      startCount = receivedIntent.getIntExtra("startCount", 0);
  
      // 텍스트뷰에 startCount 값을 보여줍니다.
      msg = "전달된 startCount : " + startCount;
      txtMsg.setText(msg);
   
   return rootView;
  } 
  protected void onNewIntent(Intent intent) {
      // 전달받은 인텐트를 처리합니다.
      Intent receivedIntent = getIntent();
      startCount = receivedIntent.getIntExtra("startCount", 0);
  
      // 텍스트뷰에 startCount 값을 보여줍니다.
      msg = "전달된 startCount : " + startCount;
      txtMsg.setText(msg);
   
   super.onNewIntent(intent);
  }
 } 
}
현재 독학으로 공부를 하고있는데 안드로이드가 킷캣으로 업그레이드 되면서 
빨강글씨로 된 getIntent()부분과 onNewIntent 이 부분에서 에러가 뜨는데 어떻게 해야 하나요?
갑자기 업그레이드 되면서 바뀌니까 난감하네요-.-
 
꼭 답변 부탁드립니다.
 
앱신 (140 포인트) 님이 2014년 4월 2일 질문

1개의 답변

+1 추천
킷캣에서만 문제가 있으신지 모르겠지만 처음에는 getIntent()가 null이 아니였다가 화면전환 등으로 재시작되면 null이 될수있습니다. 그리고 onNewIntent에서 getIntent()를 쓰셨는데 저렇게하시면 처음 실행됐을때 intent가 반환됩니다. onNewIntent로 실행되었을때 파라미터에 있는 intent를 쓰셔야 됩니다.
congbab (5,140 포인트) 님이 2014년 4월 2일 답변
...