Fatal Exception: main and Null pointer exception arise while running a simple android program

I was trying to do some simple stuff on Android. I created a Login Page and worked on. When I try to create a pop up window, Fatal exception is thrown. Help out

NewProjectActivity.java

public class NewProjectActivity extends Activity {

    PopupWindow popUp;
    LinearLayout layout;
    TextView tv;
    LayoutParams params;
    LinearLayout mainLayout;
    boolean click = true;

    @Override
    protected void onCreate(Bundle savedInstanceState)  {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_new_project);
        TextView alertdetail = (TextView) findViewById(R.id.link_to_register);
        alertdetail.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                if (click) {
                    popUp.showAtLocation(mainLayout, Gravity.BOTTOM, 10, 10);
                    popUp.update(50, 50, 300, 80);
                    click = false;
                } else {
                    popUp.dismiss();
                    click = true;
                }
            }
        });


        params = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
        layout.setOrientation(LinearLayout.VERTICAL);
        tv.setText("Hi this is a sample text for popup window");
        layout.addView(tv, params);
        popUp.setContentView(layout);
        popUp.showAtLocation(layout, Gravity.BOTTOM, 10, 10);
        mainLayout.addView(alertdetail, params);
        setContentView(mainLayout);

    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.new_project, menu);
        return true;
    }
}

Answers


TextView tv is not initialized and layout and popUp and mainLayout.

Also you have setContentView twice for the same activity which is not wrong but bad design


Your simple application is crashing because you have declared these variables :-

PopupWindow popUp;
LinearLayout layout;
TextView tv;
LayoutParams params;
LinearLayout mainLayout;

and initialised only :-

        TextView alertdetail = (TextView) findViewById(R.id.link_to_register);

This is called as passing the reference. You need to do the same for the above


Need Your Help

MBProgressHUD with Blur effect gives a Thread 1: SIGABRT

objective-c xcode5 sigabrt mbprogresshud

Im using MBProgressHUD with blur effect in one of my projects from Github , but I cannot get this work.

Sort nested comments after date in XSL

xml xslt

I have to take first 4 comments ordered descending from all comments in this xml, (ordered by date_added):