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

How to render a file with Blender called from a PHP script?

php permissions executable blender

I'm writing a basic web service to render colour swatches with nice lighting and shadows, using blender in its 'command line' mode, and called from a php script ( that also handles cataloging swatc...

Best practice to handle locales with currencies and language switches

php zend-framework frameworks locale currency

I'm working on a multilanguage project with Zend Framework (German and English) where I have a small form to order a product. The user should be able to:

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.