如何使用if else选择布局?

时间:2013-12-16 09:49:31

标签: android layout if-statement

我试图根据if else语句在两个布局之间切换。我不知道我哪里出错了。有人请帮忙。

public class MainActivity extends Activity 
{
    TextView head, tname, tid, tphone;
    EditText ename, eid, ephone;
    Button submit;
    List label = new ArrayList<String>();
    List labels = new ArrayList<String>();
    String name, id, phone;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        DataBaseHelper db = new DataBaseHelper(getApplicationContext());
        labels = db.getAllLabels();

        if (labels.isEmpty()) {
            setContentView(R.layout.activity_main);
            System.out.println("in if loop");
        } else {
            System.out.println("in else loop");     
            Intent i = new Intent(this, activity2.class);
            startActivity(i);
            MainActivity.this.finish();
        }

        System.out.println("Coming into onCreate");                    
        Calendar cal = Calendar.getInstance();
        cal.setTimeInMillis(System.currentTimeMillis());

        cal.add(Calendar.DAY_OF_MONTH, 1);
        cal.set(Calendar.HOUR_OF_DAY, 10);
        cal.set(Calendar.MINUTE, 00);
        cal.set(Calendar.SECOND, 10);
        System.out.println(cal.toString() + "         " + cal.getTime());

        Intent intent = new Intent(MainActivity.this, TestService.class);

        PendingIntent pintent = PendingIntent.getService(this, 0, intent, 0);

        AlarmManager alarm = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
        // int i;
        // i=24*60*60*1000;
        alarm.setRepeating(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(),
                    86400000, pintent);

        head = (TextView) findViewById(R.id.textView1);
        tname = (TextView) findViewById(R.id.textView2);
        tid = (TextView) findViewById(R.id.textView3);
        tphone = (TextView) findViewById(R.id.textView4);
        ename = (EditText) findViewById(R.id.editText1);
        eid = (EditText) findViewById(R.id.editText2);
        ephone = (EditText) findViewById(R.id.editText3);
        submit = (Button) findViewById(R.id.button1);

        // head.setText("REGISTER");
        // tname.setText("NAME:");
        // tid.setText("ID:");
        // tphone.setText("PHONE NUMBER");

        submit.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub

                name = ename.getText().toString();
                id = eid.getText().toString();
                phone = ephone.getText().toString();

                label.add(name);
                label.add(id);
                label.add(phone);

                DataBaseHelper db = new DataBaseHelper(getApplicationContext());
                // labels=db.getAllLabels();
                db.insertLabel(label);
                Toast.makeText(getApplicationContext(), "Registered",
                            Toast.LENGTH_SHORT).show();
                MainActivity.this.finish();
            }
        });

    }

}
Logcat中的

错误:

12-16 15:09:18.452: E/AndroidRuntime(9117): FATAL EXCEPTION: main
12-16 15:09:18.452: E/AndroidRuntime(9117): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.alarmservice/com.example.alarmservice.MainActivity}: java.lang.NullPointerException
12-16 15:09:18.452: E/AndroidRuntime(9117):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2114)
12-16 15:09:18.452: E/AndroidRuntime(9117):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2139)
12-16 15:09:18.452: E/AndroidRuntime(9117):     at android.app.ActivityThread.access$700(ActivityThread.java:143)
12-16 15:09:18.452: E/AndroidRuntime(9117):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1241)
12-16 15:09:18.452: E/AndroidRuntime(9117):     at android.os.Handler.dispatchMessage(Handler.java:99)
12-16 15:09:18.452: E/AndroidRuntime(9117):     at android.os.Looper.loop(Looper.java:137)
12-16 15:09:18.452: E/AndroidRuntime(9117):     at android.app.ActivityThread.main(ActivityThread.java:4960)
12-16 15:09:18.452: E/AndroidRuntime(9117):     at java.lang.reflect.Method.invokeNative(Native Method)
12-16 15:09:18.452: E/AndroidRuntime(9117):     at java.lang.reflect.Method.invoke(Method.java:511)
12-16 15:09:18.452: E/AndroidRuntime(9117):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1038)
12-16 15:09:18.452: E/AndroidRuntime(9117):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:805)
12-16 15:09:18.452: E/AndroidRuntime(9117):     at dalvik.system.NativeStart.main(Native Method)
12-16 15:09:18.452: E/AndroidRuntime(9117): Caused by: java.lang.NullPointerException
12-16 15:09:18.452: E/AndroidRuntime(9117):     at com.example.alarmservice.MainActivity.onCreate(MainActivity.java:110)
12-16 15:09:18.452: E/AndroidRuntime(9117):     at android.app.Activity.performCreate(Activity.java:5203)
12-16 15:09:18.452: E/AndroidRuntime(9117):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1094)
12-16 15:09:18.452: E/AndroidRuntime(9117):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2078)
12-16 15:09:18.452: E/AndroidRuntime(9117):     ... 11 more

2 个答案:

答案 0 :(得分:2)

return;

之后添加MainActivity.this.finish();语句

答案 1 :(得分:0)

反转你的if语句并将其余代码放在整个块中:

if (!labels.isEmpty()) {
    System.out.println("in else loop");     
    Intent i = new Intent(this, activity2.class);
    startActivity(i);
    MainActivity.this.finish();
}
else {
    setContentView(R.layout.activity_main);
    System.out.println("in if loop");
    System.out.println("Coming into onCreate");                    
    Calendar cal = Calendar.getInstance();
    cal.setTimeInMillis(System.currentTimeMillis());

    cal.add(Calendar.DAY_OF_MONTH, 1);
    cal.set(Calendar.HOUR_OF_DAY, 10);
    cal.set(Calendar.MINUTE, 00);
    cal.set(Calendar.SECOND, 10);

    //and the rest of the code
}