strings.xml
파일에 문자열 값을 가져 오는 데 getResources()
을 사용하고 있습니다. 이 같은 유사한 내 수업에서 변수 생성 :android에서 getResources를 사용하여 null 객체에 가상 메소드를 호출하려고 시도했습니다.
public class Example extends Activity {
String okhttp_tp = getResources().getString(R.string.okhttp_tp);
// The rest of my script.
}
을 그리고 나는이 .addHeader("X-Client-Type", okhttp_tp)
같은 OKhttp 요청과 함께 사용 할 때 나는 내 응용 프로그램 실행 할 때 직접 나에게 오류를 제공합니다
java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.res.Resources android.content.Context.getResources()' on a null object reference
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.res.Resources android.content.Context.getResources()' on a null object reference
을
나는 다음과 같은 규칙을 시도 :
:1. String okhttp_tp = getResources().getString(R.string.okhttp_tp);
2. String okhttp_tp = (String) getResources().getString(R.string.okhttp_tp);
3. String okhttp_tp = getResources().getString(R.string.okhttp_tp).toString();
4. String okhttp_tp = (String) getResources().getString(R.string.okhttp_tp).toString();
5. String okhttp_tp = Example.this.getResources().getString(R.string.okhttp_tp);
나는 또한이 작업을 수행하려
String doPostRequest(String url, String json[]) throws IOException {
Request request = new Request.Builder()
.addHeader("X-Client-Type", getResources().getString(R.string.okhttp_tp).toString())
.url(okhttp_login_url)
.url(url)
.post(body)
.build();
}
하지만 이러한 작업과 앱 실행 후 충돌이 발생하지 않습니다. 내가 놓친 게 있니? 내가 잊었던 게 있니?
도움 주셔서 감사합니다.
이 –