App crashes when adding View to layout when using int id Android SDK
Question
Basically, the point of the code is to interpret a String in a way that the View being created will be placed in the prior View mentioned.
% $- background:#000000 %
Hello, World.
% $- background:#ffffff %
$+ id:test type:container %
Test
Here is the code for it.
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();
When the app crashes, I get the following logcat error:
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)
This log shows that the error is occurring at ((LinearLayout) findViewById(relative)).addView(tv); at line 116.
What is the problem with my code?
Solution
Because of following code,
int relative2 = 0;
final int relative = relative2;
the value of relative is 0 which is final and can't be changed.
So when you perform ((LinearLayout) findViewById(relative))
, it becomes like ((LinearLayout) findViewById(0))
. As there is no view associated with id 0, you don't get the reference to that view, hence the value is null. And when you try to add a view to it, you get a nullpointer exception.
Read findViewById() from docs.It says that
Finds a view that was identified by the id attribute from the XML that was processed in onCreate(Bundle).
Returns The view if found or null otherwise.
So basically in your case make sure when you try to find a view by id, the view is present in xml layout file with proper id assigned to it. So that when the R.java
file is generated, this id will be mapped to an int value.