简体   繁体   中英

Android Fatal Exception NullPointer

Application crashes and I'm getting this error :

02-19 13:48:34.168: E/AndroidRuntime(1070): FATAL EXCEPTION: main
02-19 13:48:34.168: E/AndroidRuntime(1070): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.prva/com.example.prva.MainMenu}: java.lang.NullPointerException

02-19 13:48:34.168: E/AndroidRuntime(1070): Caused by: java.lang.NullPointerException
02-19 13:48:34.168: E/AndroidRuntime(1070):     at com.example.prva.MainMenu.onCreate(MainMenu.java:49)

When I try to runs this activity :

public class MainMenu extends Activity{ 
    Calendar c = Calendar.getInstance();    
    int currentsecond = c.get(Calendar.SECOND); 
    Handler h=new Handler();
    DatabaseManager db;



    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);             
        h.post(new Runnable(){
            @Override
            public void run() {             
                String currentDateTimeString = new SimpleDateFormat("dd.MM.yyyy HH:mm").format(new Date());
                TextView text = (TextView) findViewById(R.id.txtDate);
                text.setText(currentDateTimeString);
                h.postDelayed(this, 60000);
            }
        });

        db = new DatabaseManager(this);             


        Button btnalarm = (Button) findViewById(R.id.btnSetAlarm);
        btnalarm.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub      
                startActivity(new Intent(MainMenu.this, SetAlarm.class));               
            }           
        });

        Button btnmelody = (Button)findViewById(R.id.btnSetMelody);
        btnmelody.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                startActivity(new Intent(MainMenu.this, SetMelody.class));
            }
        });     

        Button btnlist = (Button) findViewById(R.id.btnAlarmList);
        btnlist.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View arg0) {
                // TODO Auto-generated method stub
                Intent intent1 = new Intent(MainMenu.this, AlarmList.class);
                startActivity(intent1);
            }
        });

        Button btndata = (Button) findViewById(R.id.btnDatabase);
        btndata.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View arg0) {
                // TODO Auto-generated method stub
                Intent intent2 = new Intent(MainMenu.this, ManageDatabase.class);
                startActivity(intent2);
            }
        }); 
    }
}

Line 49 is :

btnalarm.setOnClickListener(new View.OnClickListener() {

When you wrote this:

    Button btnalarm = (Button) findViewById(R.id.btnSetAlarm);
    btnalarm.setOnClickListener(new View.OnClickListener() {

you assumed that finding that view would return a non-null button, but you were wrong.

See why the search failed and you'll have your answer.

你的问题应该是,当你尝试findViewById你的Button ,看看如果这个ID( R.id.btnSetAlarm )是在布局,或者如果这个按钮是有在同一setContentView(R.layout.activity_main);

Try this:

    btnalarm.setOnClickListener(new OnClickListener() {
        public void onClick(View v) {
        // TODO do your stuff here
        }
    });

and be sure that your " btnSetAlarm " is declared in you *main_activity.xml*

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM