2014-01-22 63 views
0

嗨,我在我做的小任務,如創建活動,按鈕android的初學者,複選框etc.The代碼將正常工作,我會得到所需的output.If我嘗試添加新的這可能包含錯誤我會不幸的應用程序停止然後再次如果我撤消所有的變化我做了什麼,再次運行代碼將得到相同的錯誤。錯誤:不幸的是應用程序停止

這是什麼問題,我該怎麼辦? 這是撤消更改後的代碼是否有任何問題?這是主要活動。請任何人幫助我。

CheckBox rememberMe = (CheckBox)findViewById(R.id.checkBox1); 



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

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

       public void onClick(View v) { 
        EditText text= (EditText) findViewById (R.id.editText1); 
        EditText text1= (EditText) findViewById (R.id.editText2); 
        SharedPreferences sp=getSharedPreferences(MY_FILE,Context.MODE_PRIVATE); 
        String textview1= text.getText().toString(); 
        String password1= text1.getText().toString(); 

        String oldname=sp.getString("name",name); 
        String oldpassword=sp.getString("password",""); 
        if(!textview1.equals(null) && !password1.equals(null)) 
        { 

         if(textview1.equals(oldname)&& password1.equals(oldpassword)) 

         { 

          Intent i=new Intent(MainActivity.this,MenuScreen.class); 
         startActivity(i); 

         } 



        else{ 
        AlertDialog alertDialog = new AlertDialog.Builder(MainActivity.this).create(); 
        alertDialog.setTitle("error msg"); 
        alertDialog.setMessage("You should register before"); 
        alertDialog.show(); 
        } 

        } 

       } 


      });  
      button2= (Button)findViewById (R.id.button2); 
      button2.setOnClickListener(new OnClickListener(){ 
       public void onClick(View v){ 
        Intent j=new Intent(MainActivity.this,Registration.class); 
        startActivity(j); 
        } 
      }); 

    } 



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

} 
+0

發佈堆棧跟蹤 – Raghunandan

+0

檢查堆棧跟蹤並在此處張貼 –

+0

在此處發佈代碼時是否有錯別字或者提供的解決方案是否解決了您的問題? – csmckelvey

回答

2

此行導致你錯誤..

CheckBox rememberMe = (CheckBox)findViewById(R.id.checkBox1); 

上面一行不出來側onCreate() method..place這裏面onCreate()因爲後setContentView()只有你會看到對象。

+0

謝謝你我得到了輸出 – user3205928

1
CheckBox rememberMe = (CheckBox)findViewById(R.id.checkBox1); 

投入的onCreate()

0

調用的setContentView()後,只有您的佈局將是以後的setContentView把loaded.So下面的代碼

CheckBox rememberMe = (CheckBox)findViewById(R.id.checkBox1); 
中的onCreate

()()。

相關問題