应用程序崩溃并抛出空指针异常

时间:2014-01-09 05:52:26

标签: android

点击此活动后,我的应用就会崩溃。它正在抛出NullPointerException。任何人都可以让我知道我做了什么错误。

这是我的SettingActivity.java文件代码:

package com.homeautomation.activity;

import com.homeautomation.database.DBAdapter;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.Switch;
import android.widget.TextView;
import android.widget.Toast;

public class SettingsActivity extends BaseActivity {
    private EditText PasswordEdit;
    private EditText ConfirmPasswordEdit;
    private Button submit;
    private String Password;
    private String ConfirmPassword;
    private DBAdapter homeAuotmationDBAdapher;

    private Switch switchconfig;
    private int notifications = 0;
    private TextView switchStatus;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        initViewControls();
        if (savedInstanceState == null) {
            onlySelectItem(4);
        }
        initializeDB();
    }

    public void initViewControls() {

        LayoutInflater inflater = (LayoutInflater) SettingsActivity.this
                .getSystemService(SettingsActivity.this.LAYOUT_INFLATER_SERVICE);
        LinearLayout myView = (LinearLayout) inflater.inflate(
                R.layout.activity_settings, null);
        flContainer.addView(myView);

        PasswordEdit = (EditText) findViewById(R.id.txtPassword);
        ConfirmPasswordEdit = (EditText) findViewById(R.id.txtConfirmPass);
        submit = (Button) findViewById(R.id.btnSubmit);



        submit.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View view) {
                Password = PasswordEdit.getText().toString();
                ConfirmPassword = ConfirmPasswordEdit.getText().toString();

                if (TextUtils.isEmpty(Password)) {
                    PasswordEdit
                            .setError(getString(R.string.error_field_required));

                } else if (TextUtils.isEmpty(ConfirmPassword)) {
                    ConfirmPasswordEdit
                            .setError(getString(R.string.error_field_required));

                }

                else if (!Password.equals(ConfirmPassword)) {
                    ConfirmPasswordEdit
                            .setError(getString(R.string.error_field_required));
                }

                else {
                    SettingsActivity.this.homeAuotmationDBAdapher.updatePassword(Password);
                    SettingsActivity.this.homeAuotmationDBAdapher.updateNotification(SettingsActivity.this.notifications);
                    Toast.makeText(getApplicationContext(), "PASSWORD UPDATED SUCCESFULLY!!! =)",
                               Toast.LENGTH_LONG).show();
                }

            }

        });


          switchconfig = (Switch) findViewById(R.id.swNotification);
          if(this.homeAuotmationDBAdapher.getNotification()==1){
              switchconfig.setChecked(true);
          }else{
              switchconfig.setChecked(false);
          }
          switchconfig.setOnCheckedChangeListener(new OnCheckedChangeListener() {

           @Override
           public void onCheckedChanged(CompoundButton buttonView,
             boolean isChecked) {

            if(isChecked){
                SettingsActivity.this.notifications = 1;
            }else{
                SettingsActivity.this.notifications = 0;
            }

           }
          });


          if(switchconfig.isChecked()){
           switchStatus.setText("Switch is currently ON");
          }
          else {
           switchStatus.setText("Switch is currently OFF");
          } 

    }
    private void initializeDB() {

        this.homeAuotmationDBAdapher = new DBAdapter(SettingsActivity.this);
        this.homeAuotmationDBAdapher.openDatabase();
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // getMenuInflater().inflate(R.menu.settings, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // The action bar home/up action should open or close the drawer.
        // ActionBarDrawerToggle will take care `enter code here`of this.
        if (mDrawerToggle.onOptionsItemSelected(item)) {
            return true;
        }
        // Handle action buttons
        switch (item.getItemId()) {
        default:
            return super.onOptionsItemSelected(item);
        }
    }

}

4 个答案:

答案 0 :(得分:2)

您尚未在setContentView中的任何地方拨打onCreate()。在致电任何findViewById之前,您需要在致电super.onCreate()

后致电它

答案 1 :(得分:2)

flContainer未初始化

 PasswordEdit = (EditText) findViewById(R.id.txtPassword);

会给你NUllPointerException coz findViewById会在当前夸大的布局中查找视图。您没有setContentView(R.layout.mylayout)

此外,如果您为布局充气,则需要将其设置为活动。

答案 2 :(得分:0)

试试这个。

您的initViewControls方法

中的

    LayoutInflater inflater = (LayoutInflater) SettingsActivity.this
            .getSystemService(SettingsActivity.this.LAYOUT_INFLATER_SERVICE);
    View myView = (View) inflater.inflate(
            R.layout.activity_settings, null);
    flContainer.addView(myView);

    PasswordEdit = (EditText) myView.findViewById(R.id.txtPassword);
    ConfirmPasswordEdit = (EditText) myView.findViewById(R.id.txtConfirmPass);
    submit = (Button) myView.findViewById(R.id.btnSubmit);

答案 3 :(得分:0)

 LayoutInflater inflater = (LayoutInflater) SettingsActivity.this
            .getSystemService(SettingsActivity.this.LAYOUT_INFLATER_SERVICE);
    View myView = (View) inflater.inflate(
            R.layout.activity_settings, null);
    flContainer.addView(myView); 

PasswordEdit = (EditText)myView . findViewById(R.id.txtPassword);
        ConfirmPasswordEdit = (EditText)myView . findViewById(R.id.txtConfirmPass);
        submit = (Button) myView .findViewById(R.id.btnSubmit);

请从您的app更改您的上述代码。我希望它对您有用。