Skip to content Skip to sidebar Skip to footer

I Have Just Written My First Android App, And After 3 Hours It Still Doesn't Work

I have just started writing my first piece of code in an android app, however when I run it with the following section uncommented it crashes (anything else I tell you would be a u

Solution 1:

It seems you built your views inside fragment_main.xml and not activity_main.xml.

When you first create a new android project, you have these files which are automatically created and opened:

enter image description here

Then, when you begin, you add views (e.g: a TextView) inside fragment_main.xml file. Finally, you tried to do a basically event with this view inside your Activity, something like this:

publicclassMainActivityextendsActivity {

    @OverrideprotectedvoidonCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_main); // Using layout activity_main.xml// You try to set a simple text on the view (TextView) previously addedTextViewtext= (TextView) findViewById(R.id.textView1);
        text.setText("Simple Text");  // And you get an error here!/*
         * You do an fragment transaction to add PlaceholderFragment Fragment
         * on screen - this below snippnet is automatically created.
        */if(savedInstanceState == null) {
            getSupportFragmentManager().beginTransaction()
                    .add(R.id.container, newPlaceholderFragment()).commit();
        }
    } 

You cannot run your app or sometimes you have only a white screen, because the views that you tried to call/display are into the wrong layout..

Answer : Move all your stuff inside onCreateView method into the Fragment class.

Call views and do something in the related fragment and not the parent activity.


For example, in your case, these following lines:

pepBox = (CheckBox) findViewById(R.id.checkBox1);cheeseBox = (CheckBox) findViewById(R.id.checkBox2);textView = (TextView) findViewById(R.id.textView1);  

might be inside your fragment because you created it in fragment_main.xml layout. Then:

// start fragmentpublicstaticclassPlaceholderFragmentextendsFragment {

    @Overridepublic View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {

        // Use the layout which is displayed it's fragment_main.xmlViewrootView= inflater.inflate(R.layout.fragment_main, container, false);

        // Find your views here!// Don't forget to findViewById attached to the inflated view "rootView.findView..."
        pepBox = (CheckBox) rootView.findViewById(R.id.checkBox1);
        cheeseBox = (CheckBox) rootView.findViewById(R.id.checkBox2);
        textView = (TextView) rootView.findViewById(R.id.textView1);

        /*
         * Do something here: click listener, set a text, check a box..
        */return rootView;
    }  

    /*
     * Perform other methods still inside the fragment
    */publicvoidonButton1Click(View v){
        if(v.getId() == R.id.button1){

            StringBuilderstr=newStringBuilder("");
            if (pepBox.isChecked()) {
                str.append("Pepperoni"+" ");
            }
            if (cheeseBox.isChecked()) {
                str.append("Extra Cheese");
            }
            if (str.length() == 0) {
                str.append("Plain");
            }
            textView.setText(str);
        }
    }
}
// end fragment

Solution 2:

 if (pepBox.isChecked()) {

This line of code is failing with a NullPointerException because pepBox was declared but it's not being initialized as it should (and is therefore null at this point in the code).

Here is its declaration line:

TextView textView; CheckBox pepBox, cheeseBox;

And here is its initialization:

pepBox = (CheckBox) findViewById(R.id.checkBox1);

The problem is that (a) this line is not be called, or (b) findViewById(R.id.checkBox1) is returning null.

If findViewByID is returning null then you probably want to refer to this question, which addresses exactly this issue. Its "related questions", down the right-hand side, should also be helpful.

Post a Comment for "I Have Just Written My First Android App, And After 3 Hours It Still Doesn't Work"