문제

My app force closes on the onClick (goForIt). I'm just trying to set a content View but it forces closes eatch time I press my button. And also is it possible to make an app with only 1 activity ? Could somebody tell me why please:

package com.XanderApps.techquizz;

import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.widget.CheckBox;

public class MainActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

    }

     public void goForIt(View view) {
     setContentView(R.layout.question_one);

     }


    public void onCheckboxClicked(View view) {

        boolean checked = ((CheckBox) view).isChecked();

        switch(view.getId()) {
            case R.id.checkbox_meat:
                if (checked)
                    setContentView(R.layout.choice_done);






                break;
            case R.id.checkbox_cheese:
                if (checked)
                    System.exit(0);


                break;

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

}
도움이 되었습니까?

해결책

Without looking at your Logcat, my guess is that you are getting a 'NullPointerException' when you try to access your button.

Make sure you are 'instantiating' your button object using the findViewById syntax, before you are trying to use onClick().

다른 팁

As you haven't instantiated your goForIt inside onCreateView() of Activity. Thus it throws NPE(Null Pointer Exception).

Get your id of goForIt from your XML layout file.

Add

public void onCreate(Bundle savedInstanceState)
{ 
 super.onCreate(savedInstanceState);
 setContentView(R.layout.activity_main);
 goForit=(Button)findViewById(R.id.goForItId);
 }

And then add onClickListener on goForIt.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top