检查活动条件

时间:2013-08-11 08:31:55

标签: android button android-intent

我有一个包含3个按钮创建,显示和设置的布局。 在点击创建和显示按钮时,它们正常工作并转移到下一个活动,无论它是什么。 但我需要检查设置按钮。如果密码表存在则应该转到password.class,如果不存在则转到Setting.class。

尝试此代码时,else语句显示死代码而没有响应。

import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.app.Activity;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;


public class MainActivity extends Activity {

Button create, show, setting;
String pass1,pass2;

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


    create = (Button)findViewById(R.id.button1);
    create.setOnClickListener(new View.OnClickListener() {
        public void onClick(View view) {
            Intent myIntent1 = new Intent(view.getContext(), Create.class);
            startActivityForResult(myIntent1, 0);
        }

    });


    show = (Button)findViewById(R.id.button2);
    show.setOnClickListener(new View.OnClickListener() {

        public void onClick(View view) {
            Intent myIntent2 = new Intent(view.getContext(), Password.class);
            startActivityForResult(myIntent2, 0);
        }

    }); 


     SQLiteDatabase db = openOrCreateDatabase("passmanage", MODE_PRIVATE, null);
     Cursor c = db.rawQuery("select * from password", null);
     c.moveToFirst();
     if(c!=null)
     {
         setting = (Button)findViewById(R.id.button3);
         setting.setOnClickListener(new View.OnClickListener() {
            public void onClick(View view) {
                Intent myIntent3 = new Intent(view.getContext(), Password.class);
                startActivityForResult(myIntent3, 0);
            }

         }); 
     }   

     else
     {
         setting = (Button)findViewById(R.id.button3);
         setting.setOnClickListener(new View.OnClickListener() {
             public void onClick(View view) {
                 Intent myIntent3 = new Intent(view.getContext(), Setting.class);
                 startActivityForResult(myIntent3, 0);
             }

         }); 
     }   


}

}

我也试过这种方式 -

setting = (Button)findViewById(R.id.button3);
    setting.setOnClickListener(new View.OnClickListener() {
        public void onClick(View view) {

        SQLiteDatabase db = openOrCreateDatabase("passmanage", MODE_PRIVATE, null);
     Cursor c = db.rawQuery("select * from password", null);
     c.moveToFirst();
     if(c!=null)
     {
         Intent myIntent3 = new Intent(view.getContext(), Password.class);
         startActivityForResult(myIntent3, 0);
     }   

     else
     {
         Intent myIntent3 = new Intent(view.getContext(), Setting.class);
         startActivityForResult(myIntent3, 0);
     }

        }}); 

但是我在这里也得到错误作为其他条件的死代码。

我也试过这种方式

 setting = (Button)findViewById(R.id.button3);
    setting.setOnClickListener(new View.OnClickListener() {
        public void onClick(View view) {

    SQLiteDatabase db = openOrCreateDatabase("passmanage", MODE_PRIVATE, null);
     Cursor c = db.rawQuery("select * from password", null);
     c.moveToFirst();
     if(c!=null)
     {
         do
        {
            pass1 = c.getString(c.getColumnIndex("pass"));
            pass2 = c.getString(1);

        } while(c.moveToNext());

         if(pass1.compareTo("")==0 && pass2.compareTo("")==0)
         {
             Intent myIntent3 = new Intent(view.getContext(), Password.class);
             startActivityForResult(myIntent3, 0);
         }
         else
         {
             Intent myIntent3 = new Intent(view.getContext(), Setting.class);
             startActivityForResult(myIntent3, 0);
         }

     }

        }}); 

但它意外关闭了!

我还有一个小疑问。我是否需要为同一活动中的不同按钮创建具有不同名称的意图?

1 个答案:

答案 0 :(得分:0)

您的光标c不会为空。如果是,你会在c.moveToFirst()上获得nullpointerexception。您需要检查它是否有数据。