I can't figure this out

I'm learning Android development. Running the code underneath results in this error message log. I can't figure it out, does anybody see what I am doing wrong?

03-17 23:02:59.387: E/Trace(15387): error opening trace file: No such file or directory (2)
03-17 23:02:59.387: D/ActivityThread(15387): setTargetHeapUtilization:0.25
03-17 23:02:59.397: D/ActivityThread(15387): setTargetHeapIdealFree:8388608
03-17 23:02:59.397: D/ActivityThread(15387): setTargetHeapConcurrentStart:2097152
03-17 23:02:59.497: D/AndroidRuntime(15387): Shutting down VM
03-17 23:02:59.497: W/dalvikvm(15387): threadid=1: thread exiting with uncaught exception (group=0x41bdc378)
03-17 23:02:59.507: E/AndroidRuntime(15387): FATAL EXCEPTION: main
03-17 23:02:59.507: E/AndroidRuntime(15387): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.fb_ui_in_code/com.example.fb_ui_in_code.MainActivity}: java.lang.NullPointerException
03-17 23:02:59.507: E/AndroidRuntime(15387):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2071)
03-17 23:02:59.507: E/AndroidRuntime(15387):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2096)
03-17 23:02:59.507: E/AndroidRuntime(15387):    at android.app.ActivityThread.access$600(ActivityThread.java:138)
03-17 23:02:59.507: E/AndroidRuntime(15387):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1207)
03-17 23:02:59.507: E/AndroidRuntime(15387):    at android.os.Handler.dispatchMessage(Handler.java:99)
03-17 23:02:59.507: E/AndroidRuntime(15387):    at android.os.Looper.loop(Looper.java:213)
03-17 23:02:59.507: E/AndroidRuntime(15387):    at android.app.ActivityThread.main(ActivityThread.java:4787)
03-17 23:02:59.507: E/AndroidRuntime(15387):    at java.lang.reflect.Method.invokeNative(Native Method)
03-17 23:02:59.507: E/AndroidRuntime(15387):    at java.lang.reflect.Method.invoke(Method.java:511)
03-17 23:02:59.507: E/AndroidRuntime(15387):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:789)
03-17 23:02:59.507: E/AndroidRuntime(15387):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:556)
03-17 23:02:59.507: E/AndroidRuntime(15387):    at dalvik.system.NativeStart.main(Native Method)
03-17 23:02:59.507: E/AndroidRuntime(15387): Caused by: java.lang.NullPointerException
03-17 23:02:59.507: E/AndroidRuntime(15387):    at android.view.ViewGroup.addViewInner(ViewGroup.java:3485)
03-17 23:02:59.507: E/AndroidRuntime(15387):    at android.view.ViewGroup.addView(ViewGroup.java:3357)
03-17 23:02:59.507: E/AndroidRuntime(15387):    at android.view.ViewGroup.addView(ViewGroup.java:3333)
03-17 23:02:59.507: E/AndroidRuntime(15387):    at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:281)
03-17 23:02:59.507: E/AndroidRuntime(15387):    at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:271)
03-17 23:02:59.507: E/AndroidRuntime(15387):    at android.app.Activity.setContentView(Activity.java:1887)
03-17 23:02:59.507: E/AndroidRuntime(15387):    at com.example.fb_ui_in_code.MainActivity.onCreate(MainActivity.java:21)
03-17 23:02:59.507: E/AndroidRuntime(15387):    at android.app.Activity.performCreate(Activity.java:5008)
03-17 23:02:59.507: E/AndroidRuntime(15387):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1079)
03-17 23:02:59.507: E/AndroidRuntime(15387):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2035)
03-17 23:02:59.507: E/AndroidRuntime(15387):    ... 11 more

Code

package com.example.fb_ui_in_code;

import android.os.Bundle;
import android.app.Activity;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RelativeLayout;
import android.widget.RelativeLayout.LayoutParams;
import android.widget.TextView;

public class MainActivity extends Activity {

    RelativeLayout main;
    EditText usernameInput, passwordInput;
    TextView message, username, password;
    Button login;
    LayoutParams dimensions;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(main);

        init();
        createMessageTextView();
        main.addView(message, dimensions);

    }

    private void init() {
        // TODO Auto-generated method stub
        main = new RelativeLayout(this);
        LayoutParams mainDimensions = new LayoutParams(
                LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);

        main.setLayoutParams(mainDimensions);

        usernameInput = new EditText(this);
        passwordInput = new EditText(this);
        message = new TextView(this);//please login first
        username = new TextView(this);
        password = new TextView(this);
        login = new Button(this);

    }
        private void createMessageTextView() {
            // TODO Auto-generated method stub

            LayoutParams dimensions=new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
            dimensions.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
            message.setText("Please login first");
            message.setLayoutParams(dimensions);


        }
}

Answers


Well, seems the nullPointerExecption is coming from line 21, where you have setContentView(main);

so instead of just answering you, I could google 'android developer setContentView'

get lucky on the first entry http://developer.android.com/reference/android/app/Activity.html

search page for setcontentview and see that I need to pass that an int that references my content view, and now I ask; is 'main' initialized? What am I trying to do with 'main'? See, all the views you will be using from this activity will be referencing feilds in this content view.

what we are looking at is the establishment of an interface with your xml files, in this case, a layout. so the number is R.layout.WHATEVER_YOU_NAMED_THE_LAYOUT_YOU_WANT_TO_USE

cool? =]


I believe the setContentView(main) is the problem. You need to set that to a RelativeLayout or use setContentView(R.layout.main); main is never initialized.


The argument to give setContentView() is an integer resource ID. So in your case, you'd want to use the ID 'R.layout.main' or the like. I think the code only compiles because your 'main' variable is still null and is being automatically casted to an int.


You are using very strange approach. I don't think that you really need to create UI from Java code. Normally you have to create layout in an XML file. And this is the common approach. My advice is don't waste your time on it, at least in the beginning. Why? You can read following: Why using XML to create GUI is a good practice in Android

But anyway, if you need to create UI in code you have to call setContentView(main) after you initialize main. Just put setContentView(main) at the end of onCreate() method.


It should be like :

setContentView(R.layout.main);

and also make sure that R is imported like : YOUR_PACKAGE_NAME.R


Need Your Help

Just check status process in c

c process fork wait

I want to know the status of a process. I think I can use the wait family functions but actually I don't want to wait for the process, just check the status and go on.