Domanda

I looked it up on the internet and as far as I can tell it SHOULD work, but then again, the internet always reminds me I am wrong 95% of the time. Anyway, can anyone tell why this ISN'T working? I've gathered it's adding layoutchild to layout but I don't know why, if it's clearly there? Please and thanks in advance. Sometimes the simplest things give me the most trouble.

Line 24 would be layout.addView(layoutchild);

Code:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    RelativeLayout layout = (RelativeLayout) findViewById(R.layout.activity_main);
    LinearLayout layoutchild = new LinearLayout(this);
    TextView test = new TextView(this);
    test.setText("pooppoopoppopopo");
    layoutchild.addView(test);
    layout.addView(layoutchild);
}

LogKitty:

java.lang.NullPointerException
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2282)
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2340)
    at android.app.ActivityThread.access$800(ActivityThread.java:157)
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1247)
    at android.os.Handler.dispatchMessage(Handler.java:102)
    at android.os.Looper.loop(Looper.java:157)
    at android.app.ActivityThread.main(ActivityThread.java:5293)
    at java.lang.reflect.Method.invokeNative(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:515)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1265)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1081)
    at dalvik.system.NativeStart.main(Native Method)

Caused by: java.lang.NullPointerException
    at com.eai.util.counter.MainActivity.onCreate(MainActivity.java:24)
    at android.app.Activity.performCreate(Activity.java:5389)
    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1105)
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2246)
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2340)
    at android.app.ActivityThread.access$800(ActivityThread.java:157)
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1247)
    at android.os.Handler.dispatchMessage(Handler.java:102)
    at android.os.Looper.loop(Looper.java:157)
    at android.app.ActivityThread.main(ActivityThread.java:5293)
    at java.lang.reflect.Method.invokeNative(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:515)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1265)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1081)
    at dalvik.system.NativeStart.main(Native Method)
È stato utile?

Soluzione 2

Sure enough, the answer was staring me in the face. I never created an android:id and was referencing a layout instead of an ID. In fact, looking back, I used R.layout. Should have been blatantly obvious I wasn't gonna get an ID returned. Assigning an ID in XML, REFERENCING that ID with the correct R.id, and setting the content prior to declaring, fixed and execute perfectly, after a clean.

CODE:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    RelativeLayout layout = (RelativeLayout) findViewById(R.id.pooper);
    LinearLayout layoutchild = new LinearLayout(this);
    TextView test = new TextView(this);
    test.setText("DebuggedTheHeckOuttaThis");
    layoutchild.addView(test);
    layout.addView(layoutchild);
}

Altri suggerimenti

Your exception log shows the exception originated in line 24 of your MainActivity.java file on line 24, which is inside your onCreate method. You need to tell us which is line 24 of your code.

...but I'm guessing it's one of the lines that's dereferencing whatever R or R.layout is, or findViewById is returning null too.

Add some null checks/assertions in your code or use a step-through debugger to find out what is null and to handle those situations or fix other bugs in your code that cause them to be null.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top