Intent android studio

时间:2017-06-25 11:38:25

标签: java android android-intent

我是Android编程新手。我在按钮点击上打开另一个活动时遇到问题。另外,我附上了我写的代码。

这是我的 MainActivity 类:

public class MainActivity extends AppCompatActivity{
    TextView ed1;
    Button btn;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ed1=(TextView) findViewById(R.id.textView1);
        btn=(Button)findViewById(R.id.button);

        btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent i = new Intent(getApplicationContext(),LoginScreen.class);
                startActivity(i);
            }
        });
    }
}

我的 LoginScreen 类如下:

public class LoginScreen extends AppCompatActivity {
    TextView tv;
    EditText ed1,ed2;
    Button btn1;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.login_screen);
        tv=(TextView)findViewById(R.id.textView1);
        ed1=(EditText)findViewById(R.id.editText1);
        ed2=(EditText)findViewById(R.id.editText2);

        btn1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String name= ed1.getText().toString();
                String pass=ed2.getText().toString();
                if(name.equals(" ")|| pass.equals(" ")){
                    Toast.makeText(getApplicationContext(),"Required Field",Toast.LENGTH_SHORT).show();
                }
                else{
                    Toast.makeText(getApplicationContext(),"Login Successfull",Toast.LENGTH_LONG).show();
                }
            }
        });
    }
}

运行此代码后,只运行第一个类MainActivity,然后该设备显示该程序已停止工作。

1 个答案:

答案 0 :(得分:1)

您的LoginScreen课程出现问题。在设置btn1之前,您忘记初始化OnClickListener

public class LoginScreen extends AppCompatActivity {
    TextView tv;
    EditText ed1,ed2;
    Button btn1;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.login_screen);
        tv=(TextView)findViewById(R.id.textView1);
        ed1=(EditText)findViewById(R.id.editText1);
        ed2=(EditText)findViewById(R.id.editText2);
        btn1=(Button)findViewById(R.id.button1);

        btn1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String name= ed1.getText().toString();
                String pass=ed2.getText().toString();
                if(name.equals(" ")|| pass.equals(" ")){
                    Toast.makeText(getApplicationContext(),"Required Field",Toast.LENGTH_SHORT).show();
                }
                else{
                    Toast.makeText(getApplicationContext(),"Login Successfull",Toast.LENGTH_LONG).show();
                }
            }
        });
    }    
}