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

android fragment TelephonyManager에러

0 추천
import android.telephony.TelephonyManager;

        TelephonyManager mTelephonyMgr =(TelephonyManager)getSystemService(Context.TELEPHONY_SERVICE);
        String myNumber = mTelephonyMgr.getLine1Number();

 

임포트주고 fragmentActivity 를 상속 받고 퍼미션도 잘줫는데

getSystemServiceㅇ이부분에서 에러가 발생합니다.

이유가 뭐죠?.. activity에서는 잘되는데 ,..이유를 잘모르겟습니다.
개발자응 (250 포인트) 님이 2013년 6월 24일 질문

3개의 답변

+1 추천
 
채택된 답변
TelephonyManager mTelephonyMgr =(TelephonyManager)getActivity().getSystemService(Context.TELEPHONY_SERVICE);

또는

TelephonyManager mTelephonyMgr =(TelephonyManager)getSupportActivity().getSystemService(Context.TELEPHONY_SERVICE);

이렇게 해보세요.

monal (4,210 포인트) 님이 2013년 6월 24일 답변
개발자응님이 2013년 7월 16일 채택됨
import android.content.Context;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.telephony.TelephonyManager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;


public class main_fragment extends FragmentActivity{
    private EditText name2;
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState, Context getSupportActivity) {
        View view = inflater.inflate(R.layout.activity_main, container, false);
        name2 = (EditText)view.findViewById(R.id.tel2);
       
        TelephonyManager mTel = (TelephonyManager)getActivity().getSystemService(Context.TELEPHONY_SERVICE);
        String mynumber  =  mTel.getLine1Number();
   
        name2.setText(mynumber);
       
       return view;
    }

}

이렇게도해보고 getSupportActivity() 바꿔서도 해봣는데...
도통 이유를 모르겟네요...
+2 추천
Context를 컴포넘트 등에서 전달받으세요
익명사용자 님이 2013년 6월 24일 답변
import android.content.Context;
import android.telephony.TelephonyManager;
이런식으로 말씀하시나요?
0 추천

fragment 에서

private Activity mActivity;

@Override
public void onAttach(Activity activity){
super.onAttach(activity);
this.mActivity = activity;
} 
 
로 activity 잡아오세요..mActivity.TelephonyManager mTelephonyMgr =(TelephonyManager)getSystemService(Context.TELEPHONY_SERVICE);
노예의집 (23,370 포인트) 님이 2013년 6월 26일 답변
...