프로그래밍/안드로이드

에러 종류별 해결 방법

do121 2022. 11. 15. 09:45
  • Cannot resolve method 'onBackPressed' in 'Fragment'

     프래그먼트안에서는 backpress를 받을 수 없으므로 webview에서 직접 받게한다.

     wv = root.findViewById(R.id.webview_home);

     wv.setOnKeyListener(new View.OnKeyListener(){
            @Override
            public boolean onKey(View view, int keyCode, KeyEvent keyEvent) {
                if ((keyCode == KeyEvent.KEYCODE_BACK) && wv.canGoBack()) {
                    wv.goBack();
                    return true;
                }
                return false;
            }


        });

    

     다른 방법

https://soo0100.tistory.com/1252

 

안드로이드의 기술 #Fragment 백키 처리하기.

Fragment 에서는 H/W 백키 이벤트가 불릴 시 onBackPress() 함수가 실행되지 않는다. 즉, 오버라이딩 할 수 없는 기본 구조이다. 그럼, 어떻게 할 것 인가? 1. Fragment 에 onBackPressed() 라는 함수를 만든다. 필

soo0100.tistory.com

 

  • Cannot resolve symbol 'FragmentManager' 

androidx로 변경되면서 기존의 import android.support.v4.app로 import한 것들이 모두 에러 발생함

아래와 같이 androidx로 import하면 됨

 

//import android.support.v4.app.FragmentManager;
import androidx.fragment.app.Fragment;


//import android.support.v7.app.ActionBar;
import androidx.appcompat.app.ActionBar;


//import android.support.v7.app.AppCompatActivity;
import androidx.appcompat.app.AppCompatActivity;


//import android.support.v4.app.DialogFragment;
import androidx.fragment.app.DialogFragment;

 

 

https://hi5lab.tistory.com/1112

 

Androidx 정리표

기존에 안드로이드 라이브러리 28.0.0부터 새로운 androidx.* 패키지 명으로 교체되었습니다. Android Studio Refactor > Refactor to AndroidX 메뉴로 자동전환이 가능은 하나 아직 잘 지원되지는 않는듯 합니다.

hi5lab.tistory.com

 

  • Cannot resolve symbol 'NotificationCompat '

//import android.support.v4.app.NotificationCompat;

androidx.core.app.NotificationCompat

 

  • Not allowed to start service Intent { cmp=com.example.keeprunning/.UndeadService }: app is in background uid UidRecord{a095d6 u0a101 TPSL idle procs:1 seq(0,0,0)} 에러

오레오 이상부터는 startForegroundService로만 호출가능

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            final String strId = getString(R.string.noti_channel_id);
            final String strTitle = getString(R.string.app_name);
            NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
            NotificationChannel channel = notificationManager.getNotificationChannel(strId);
            if (channel == null) {
                channel = new NotificationChannel(strId, strTitle, NotificationManager.IMPORTANCE_HIGH);
                notificationManager.createNotificationChannel(channel);
            }
 
            Notification notification = new NotificationCompat.Builder(this, strId).build();
            startForeground(1, notification);
        } else{
        	startService(1)
        }

 

  • This view is not constrained horizontally: at runtime it will jump to the left unless you add a horizontal constraint

아래와 같이 수직과 수평에 기준이 되는 위치를 하나씩은 입력되어야 위치를 특정할 수 있으므로 누락되면 에러 발생

  • org.eclipse.paho.client.mqttv3.persist.MqttDefaultFilePersistence.open(MqttDefaultFilePersistence.java:86)

mqtt 클라이언트 구현 관련하여 발생함

client = new MqttClient(CONNECTION_URL,clientid);를 아래와 같이 변경

client = new MqttClient(CONNECTION_URL,clientid, new MemoryPersistence()

 

  • MqttException (0) - java.net.SocketException: socket failed: EPERM (Operation not permitted)

아래 퍼미션 추가

<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />

 

  • jsoup에서  do.select 후 get으로 뽑아올때 try catch 안잡힐 때

try{

}catch(IOException e){}

try{

}catch(error e){} 로는 잡히지 않고

 

try{

}catch(IndexOutOfBoundsException e){} 로 해야 잡힘

 

 

  • scrollview안의 textview가 스크롤 안될때

scrollview안에 있을때는 기본적으로 textview가 스코롤이 안되는 것으로 보임 아래와 같이 터치 이벤트 처리를 해줘야 함

https://stackoverflow.com/questions/22422771/android-scrollable-textview-inside-scrollview

 

android scrollable textview inside scrollview

i need to create like scrolling TextView inside the ScrollView.. like 1 main ScrollView inside that scrollable TextView i have put this code in RelativeLayout <ScrollView android:id="...

stackoverflow.com

public  void enableScroll(View view) {
    if (view instanceof TextView) {
        TextView textView = (TextView) view;
        textView.setMovementMethod(new ScrollingMovementMethod());
    }

    view.setOnTouchListener(new View.OnTouchListener() {
        @Override
        public boolean onTouch(View v, MotionEvent event) {
            v.getParent().requestDisallowInterceptTouchEvent(true);
            switch (event.getAction() & MotionEvent.ACTION_MASK) {
                case MotionEvent.ACTION_UP:
                    v.getParent().requestDisallowInterceptTouchEvent(false);
                    break;
            }
            return false;
        }
    });
}

사용법 :

TextView textView = findViewById(R.id.textView);

enableScroll(textview);