ARN
ARN

Reputation: 175

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;
    }
}

Upvotes: 1

Views: 163

Answers (2)

Rahul Gupta
Rahul Gupta

Reputation: 5295

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

Upvotes: 0

Raghunandan
Raghunandan

Reputation: 133560

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

Upvotes: 3

Related Questions