按下设置按钮时崩溃

时间:2011-07-17 12:34:15

标签: java android

这是我的代码......

import java.util.ArrayList;

import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;
import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.telephony.TelephonyManager;
import android.view.KeyEvent;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class SettingsActivity extends Activity {
    EditText vname,vphone,vemail,vaddress;
    Button vbutton;
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        vname=(EditText)findViewById(R.id.get_name);
        vphone=(EditText)findViewById(R.id.get_phone);
        vemail=(EditText)findViewById(R.id.get_email);
        vaddress=(EditText)findViewById(R.id.get_address);
        vbutton=(Button)findViewById(R.id.settings_submit);
        TelephonyManager TelephonyMgr = (TelephonyManager)getSystemService(TELEPHONY_SERVICE);
        final String szid = TelephonyMgr.getDeviceId(); // Requires READ_PHONE_STATE
        super.onCreate(savedInstanceState);
        setContentView(R.layout.settingmenu);

    vbutton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub

            ArrayList<NameValuePair> postParameters = new ArrayList<NameValuePair>();
            postParameters.add(new BasicNameValuePair("pidcode", szid));
            postParameters.add(new BasicNameValuePair("pname", vname.getText().toString()));
            postParameters.add(new BasicNameValuePair("pphone", vphone.getText().toString()));
            postParameters.add(new BasicNameValuePair("pemail", vemail.getText().toString()));
            postParameters.add(new BasicNameValuePair("paddress", vaddress.getText().toString()));
            //String valid = "1";
            String response = null;
            try {/mysqlversion.php", postParameters);
                String res=response.toString();
               // res = res.trim();
                res= res.replaceAll("\\s+","");                              
                //error.setText(res);

               if(res.equals("1"))
                Toast.makeText(getBaseContext(), "Setting Set.", Toast.LENGTH_LONG).show();
                SavePreferences("MEM1", vname.getText().toString());
                SavePreferences("MEM2", vphone.getText().toString());
                SavePreferences("MEM3", vemail.getText().toString());
                SavePreferences("MEM4", vaddress.getText().toString());
                LoadPreferences();
            } catch (Exception e) {
            }
    }
    }
);


    }

    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            Intent intent = new Intent(SettingsActivity.this, MainMenuActivity.class);
            intent.setFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);
            startActivity(intent);
            return super.onKeyDown(keyCode, event);
        }
        return super.onKeyDown(keyCode, event);
    }

    private void SavePreferences(String key, String value) {
        SharedPreferences sharedPreferences = getPreferences(MODE_PRIVATE);
        SharedPreferences.Editor editor = sharedPreferences.edit();
        editor.putString(key, value);
        editor.commit();
    }

    private void LoadPreferences() {
        SharedPreferences sharedPreferences = getPreferences(MODE_PRIVATE);
        String strSavedMem1 = sharedPreferences.getString("MEM1", "");
        String strSavedMem2 = sharedPreferences.getString("MEM2", "");
        String strSavedMem3 = sharedPreferences.getString("MEM3", "");
        String strSavedMem4 = sharedPreferences.getString("MEM4", "");
        vname.setText(strSavedMem1);
        vphone.setText(strSavedMem2);
        vemail.setText(strSavedMem3);
        vaddress.setText(strSavedMem4); // textfield
    }
}

这是我的设置代码。我有这个在android manifest.xml中命名的类

1 个答案:

答案 0 :(得分:2)

您的解决方案也非常简单。在初始化视图之前调用setContentView()