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

안드로이드에서 view 객체에 대한 질문입니다. [closed]

0 추천

안드로이드 스튜디오에 view 클래스를 상속 받은 DrawFlowPlanview라는클래스를 만들고 

해당 클래스를 xml상에 화면 전체 사이즈로 설정하여 보여주려고 하고 있습니다.

이때 DrawFlowPlanview에 포함된 멤버들에 접근하려하면 

아래와 같은 에러가 발생합니다.. 도와주세요

에러코드

2020-05-21 11:24:34.910 16021-16021/? E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.minew.beaconset, PID: 16021
    java.lang.RuntimeException: Unable to start activity ComponentInfo{com.minew.beaconset/com.minew.beaconset.demo.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'void com.minew.beaconset.demo.DrawFlowPlanView.setFlowPlan(int)' on a null object reference
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3114)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3257)
        at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:78)
        at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:108)
        at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:68)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1948)
        at android.os.Handler.dispatchMessage(Handler.java:106)
        at android.os.Looper.loop(Looper.java:214)
        at android.app.ActivityThread.main(ActivityThread.java:7050)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:494)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:965)
     Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void com.minew.beaconset.demo.DrawFlowPlanView.setFlowPlan(int)' on a null object reference
        at com.minew.beaconset.demo.MainActivity.onCreate(MainActivity.java:52)
        at android.app.Activity.performCreate(Activity.java:7327)
        at android.app.Activity.performCreate(Activity.java:7318)
        at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1271)
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3094)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3257) 
        at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:78) 
        at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:108) 
        at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:68) 
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1948) 
        at android.os.Handler.dispatchMessage(Handler.java:106) 
        at android.os.Looper.loop(Looper.java:214) 
        at android.app.ActivityThread.main(ActivityThread.java:7050) 
        at java.lang.reflect.Method.invoke(Native Method) 
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:494) 
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:965) 

DrawFlowPlanView

public class DrawFlowPlanView extends View {
    private static final String TAG = "MainActivity";

    Canvas canvas;
    Bitmap picture;  //도면이미지

    int picture_ori_H; //원본 도면의 크기
    int picture_ori_W;

    int can_H; //캔버스의 크기
    int can_W;

    double scale_H; //원본이미지를 캔버스로 맞출경우의 비율율
    double scale_W;

    int resize_H; //비율로 변환한 크기
    int resize_W;

    int bitX;  //도면 시작점
    int bitY;

    int my_x, my_y;

    int id;
    public DrawFlowPlanView(Context context, AttributeSet att, int re) { super(context, att, re);}
    public DrawFlowPlanView(Context context, AttributeSet att)         { super(context, att);}
    public DrawFlowPlanView(Context context) {
        super(context);
    }

    public void setFlowPlan(int id){
        picture = BitmapFactory.decodeResource(getResources(),id);
        this.id=id;
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        this.canvas = canvas;
//        picture = BitmapFactory.decodeResource(getResources(),R.drawable.sample);
//        picture = BitmapFactory.decodeResource(getResources(),id);

        picture_ori_H = picture.getHeight();
        picture_ori_W = picture.getWidth();
        can_H = canvas.getHeight();
        can_W = canvas.getWidth();
        scale_H = (double)can_H/picture_ori_H; //원본 이미지를 캔바스의 맞출 경우 H의 비율
        scale_W = (double)can_W/picture_ori_W;
        resize_H = (int)(picture_ori_H*scale_W);
        resize_W = (int)(picture_ori_W*scale_H);

        if(picture_ori_H>picture_ori_W){
            picture = Bitmap.createScaledBitmap(picture,resize_W,canvas.getHeight(),true);
        }else{
            picture = Bitmap.createScaledBitmap(picture,canvas.getWidth(),resize_H,true);
        }
        bitX = (this.getWidth()-picture.getWidth())/2; //이미지 시작점
        bitY = (this.getHeight()-picture.getHeight())/2; //이미지

        double x = Math.random()*picture.getHeight();
        double y = Math.random()*picture.getWidth();
        Log.d("호출",String.valueOf(x)+","+String.valueOf(y));

        if(my_x!=0&&my_y!=0){
            canvas.drawCircle((float) (bitX+(my_x)),(float) (bitY+(my_y)),20,new Paint(Color.RED));
            Log.d("에러","값이 없다."+my_x+","+my_y);
        }
        picture.recycle();
        Log.d("호출","제발2");
    }
    public void redraw(){
        invalidate();
        Log.d("안녕","안녕");
    }
    public void setMyposition(int x, int y){
        my_x=x;
        my_y=y;
    }
}

MainActivity

public class MainActivity extends AppCompatActivity {

    private RecyclerView       mRecycle;
    private MinewBeaconManager mMinewBeaconManager;
    private BeaconListAdapter  mAdapter;
    UserRssi comp = new UserRssi();
    private       ProgressDialog mpDialog;
    public static MinewBeacon    clickBeacon;
    private static final int REQUEST_ENABLE_BT = 2;

    String[][] res;  //3개의 비콘 이름과 거리
    boolean beaconEnable = false; //비콘이 3개이상 인식 되었는가?  false=3개 미만, true=3개 이상
    ReadBeacon readBeacon;
    DrawFlowPlanView drawFlowPlanView ;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        drawFlowPlanView = (DrawFlowPlanView)findViewById(R.id.DFPV);
        drawFlowPlanView.setFlowPlan(getID("sample"));<---이곳에서 에러가 발생

        readBeacon = new ReadBeacon(getResources().openRawResource(R.raw.beaconinfo));
        readBeacon.readBeaconData();//비콘정보를 담으 csv 읽기

        setContentView(R.layout.activity_main2);

//        Log.d(" 비콘",drawBeacon.beacon[0].name);
//        initView();  //비콘스캐너의 목록을 initView하는 메서드 목록을 볼때 사용(activity_mian과 함께 사용), 미사용시 mAdapter = new BeaconListAdapter()를 통해 객체생성해주어야함

        mAdapter = new BeaconListAdapter();  //initView() 미사용시 사용
        initManager();
        checkBluetooth();
        mMinewBeaconManager.startService();
    }

    public int getID(String name){
        int id = getResources().getIdentifier(this.getPackageName()+":drawable/"+name,null,null);
        Log.d("객체 번호",String.valueOf(id));
        return  id;
    }
}
질문을 종료한 이유: 문제를 해결하였습니다.
chlwldud (160 포인트) 님이 5월 21일 질문
chlwldud님이 5월 21일 closed
...