2014-01-22 64 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

()()。

相关问题