2011-07-17 205 views
2

這裏是我的代碼...崩潰時,我按下設置按鈕

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中命名

+0

你需要接受你以前的答案,以防他們幫助你。 –

回答

2

你的解決方案也很簡單。在初始化視圖之前致電setContentView()

相關問題