용자를 추가하 뷰 레이아웃을 사용하는 경우 int id Android SDK
문제
기본적으로의 포인트는 코드는 해석 문자열하는 방식으로 보는 만들어지는 배치에서 보기 이전에 언급된다.
% $- background:#000000 %
Hello, World.
% $- background:#ffffff %
$+ id:test type:container %
Test
는 코드는 다음과 같습니다습니다.
new Thread() {
StringBuilder text = new StringBuilder();
@Override
public void run() {
try
{
String str = "";
URL url = new URL("http://example.com/" + mes + "/" + mes + ".meb");
BufferedReader in = new BufferedReader(new InputStreamReader(url.openStream()));
while ((str = in.readLine()) != null) {
text.append(str);
}
in.close();
} catch (MalformedURLException e1)
{
}
catch (IOException e)
{
}
if(message.contains(".meb")) {
String strn = text.toString().replace("\n", "").replace("\r", "");
String str[] = strn.toString().split("%");
int relative2 = 0;
final int relative = relative2;
for (final String l : str) {
String code[] = l.split(" ");
if (l.toString().contains("$-")) {
for(String p : code)
{
if(p.toString().contains("background"))
{
final String set[] = p.toString().split(":");
runOnUiThread(new Runnable() {
@Override
public void run() {
(findViewById(R.id.body)).setBackgroundColor(Color.parseColor(set[1]));
}
});
}
}
}
else if(l.toString().contains("$+"))
{
String[] g = code[2].split(":");
String[] c = code[3].split(":");
final String[] test = {g[1], "100"};
globvar.add(test);
if(c[1].toString().equals("container")) {
runOnUiThread(new Runnable() {
@Override
public void run() {
final LinearLayout element = new LinearLayout(getApplicationContext());
element.setBackgroundColor(Color.parseColor("#FFFFFF"));
element.setMinimumHeight(50);
element.setId(Integer.parseInt(test[1]));
((LinearLayout) findViewById(relative)).addView(element);
}
});
relative2 = Integer.parseInt(test[1]);
}
}
else {
if(!l.toString().isEmpty()) {
runOnUiThread(new Runnable() {
@Override
public void run() {
final TextView tv = new TextView(getApplicationContext());
tv.setTextColor(Color.parseColor("#000000"));
tv.setTextSize(18);
tv.setText(l);
((LinearLayout) findViewById(relative)).addView(tv);
}
});
}
}
}
}
else {
if(message == null) {
runOnUiThread(new Runnable() {
@Override
public void run() {
}
});
}
}
}
}.start();
할 때 응용 프로그램이 충돌하면 다음과 같은 콘텐츠는 오류:
08-01 19:44:27.149 29893 29893 E AndroidRuntime: FATAL EXCEPTION: main
08-01 19:44:27.149 29893 29893 E AndroidRuntime: java.lang.NullPointerException
08-01 19:44:27.149 29893 29893 E AndroidRuntime: at example.yemeb.List$1$3.run(List.java:116)
08-01 19:44:27.149 29893 29893 E AndroidRuntime: at android.os.Handler.handleCallback(Handler.java:615)
08-01 19:44:27.149 29893 29893 E AndroidRuntime: at android.os.Handler.dispatchMessage(Handler.java:92)
08-01 19:44:27.149 29893 29893 E AndroidRuntime: at android.os.Looper.loop(Looper.java:137)
08-01 19:44:27.149 29893 29893 E AndroidRuntime: at android.app.ActivityThread.main(ActivityThread.java:4931)
08-01 19:44:27.149 29893 29893 E AndroidRuntime: at java.lang.reflect.Method.invokeNative(Native Method)
08-01 19:44:27.149 29893 29893 E AndroidRuntime: at java.lang.reflect.Method.invoke(Method.java:511)
08-01 19:44:27.149 29893 29893 E AndroidRuntime: at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:791)
08-01 19:44:27.149 29893 29893 E AndroidRuntime: at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:558)
08-01 19:44:27.149 29893 29893 E AndroidRuntime: at dalvik.system.NativeStart.main(Native Method)
이 로그를 보여주는 오류가 발생하는((LinearLayout)findViewById(상대)).addView(tv);에서 선 116.
무엇이 문제 내 코드는?
해결책
기 때문에 다음 코드
int relative2 = 0;
final int relative = relative2;
의 가치에 상대적이 0 이는 최종적이며 변경할 수 없습니다.그래서 수행할 때 ((LinearLayout) findViewById(relative))
,그것은 ((LinearLayout) findViewById(0))
.이 없으므로보기와 관련된 id0,당신을 얻을 하지 않을 보고,따라서 이 값은 null 입니다.고 하려고 하면 뷰를 추가하려면 다음을 수행합니다,당신은 그것을 얻을 nullpointer 예외는 아니다.
읽기 findViewById() 에서 도움을 줍니다.말
을 찾는 뷰가에 의해 확인되었 id 특성에서는 XML 처리에 onCreate(Bundle).
을 반환합 보기 발견하는 경우 또는 null 지 않습니다.
그래서 기본적으로 귀하의 경우에는지 확인 하려고 하면 찾을 보여 id,뷰에 존재하는 xml 레이아웃 파일이 적절한 id 를 할당되어 있습니다.그래서 그 때 R.java
파일을 생성,이 id 에 매핑됩니다 int 값입니다.
제휴하지 않습니다 StackOverflow