dialogbox没有打开android

时间:2014-01-06 06:20:23

标签: android android-layout dialog

嘿,我正在使用一个应用程序,我通过单击按钮调用Activity。报告。类活动必须在它的按钮单击事件中打开一个对话框。 但不幸的是它没有显示对话框。

以下是代码:

public class Report extends Activity{

     int t_id;
     int list1=2,list2=3;
     Button btn_day,btn_category,btn_partner,btn_finish;
     Context context=this;

     SQLiteDatabase db;
        SimpleCursorAdapter cursor;
        sqliteHelper helper = new sqliteHelper(this);
        Cursor c;

     @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);


            setContentView(R.layout.report);


        Intent i = getIntent();

        t_id=i.getIntExtra("t_id",0);

        btn_day=(Button)findViewById(R.id.btn_day);
        btn_category=(Button)findViewById(R.id.btn_exp);
        btn_partner=(Button)findViewById(R.id.btn_partner);

        btn_day.setOnClickListener(new OnClickListener(){

            @Override
            public void onClick(View arg0) {
                // TODO Auto-generated method stub
                db = helper.getReadableDatabase();
                c=db.rawQuery("Select date,SUM(ammount) from tbl_expense where c_trip_id=? GROUP BY date",new String[] {Integer.toString(t_id)});
                System.out.println("total columns"+c.getColumnCount());
                if(c.getCount()!=0)
                {
                     fetch();


                }   
            }

        });


            });
        btn_partner.setOnClickListener(new OnClickListener(){

            @Override
            public void onClick(View arg0) {

                db = helper.getReadableDatabase();
                final String MY_QUERY = "SELECT a.user_name, SUM(b.ammount) FROM tbl_usermain a INNER JOIN tbl_expense b ON a.user_id=b.c_u_id WHERE b.c_trip_id=? GROUP BY a.user_name ";

                c=db.rawQuery(MY_QUERY, new String[]{Integer.toString(t_id)});
                //c=db.rawQuery("Select date,SUM(ammount) from tbl_expense where c_trip_id=? GROUP BY date",new String[] {Integer.toString(t_id)});
                System.out.println("total columns"+c.getColumnCount());
                if(c.getCount()!=0)

                     fetch();


            }

            });

     }

    private void fetch() {
        // TODO Auto-generated method stub
        final Dialog dialog = new Dialog(Report.this);
        dialog.setContentView(R.layout.reportlist);
        LinearLayout reportlistview=(LinearLayout)dialog.findViewById(R.id.reportlayout);
        dialog.setTitle("Report");
        dialog.show();
        ArrayList<HashMap> expense_by_user = new ArrayList<HashMap>();
        c.moveToFirst();

        for(int j=1;j<=c.getCount();j++)
        {
            HashMap temp = new HashMap();
              temp.put(FIRST_COLUMN,j);
              temp.put(SECOND_COLUMN, c.getString(0));
              temp.put(THIRD_COLUMN, c.getInt(1));

          expense_by_user.add(temp);
            c.moveToNext();
        }


        c.close();
        db.close();

            /*SET THE ADAPTER TO LISTVIEW*/


          ListView list=new ListView(Report.this);
          list.setId(list2);
          listviewAdapter adapter = new listviewAdapter(Report.this, expense_by_user);
            list.setAdapter(adapter);

            reportlistview.addView(list);


    }
    @Override
    protected void onSaveInstanceState(Bundle outState) {       
        int viewId = this.getCurrentFocus().getId();
        outState.putInt("hasFocus", viewId);

        super.onSaveInstanceState(outState);
    }

    @Override
    protected void onRestoreInstanceState(Bundle savedInstanceState) {      
        super.onRestoreInstanceState(savedInstanceState);

        int viewId = savedInstanceState.getInt("hasFocus");
        View view = findViewById(viewId);
        view.requestFocus();        
    }

}

logcat视图:

01-06 12:03:48.603: INFO/ActivityManager(61): Starting: Intent { cmp=com.NRP.MainPackage/.Report (has extras) } from pid 918
01-06 12:03:49.873: INFO/ActivityManager(61): Displayed com.NRP.MainPackage/.Report: +1s160ms
01-06 12:03:50.154: DEBUG/dalvikvm(61): GC_CONCURRENT freed 968K, 46% free 4369K/8071K, external 2269K/3469K, paused 24ms+13ms
01-06 12:03:52.294: INFO/System.out(918): total columns2
01-06 12:04:05.676: INFO/System.out(918): total columns2
01-06 12:05:17.223: VERBOSE/BackupManagerService(61): Backup requested but nothing pending
 01-06 12:06:02.463: DEBUG/SntpClient(61): request time failed: java.net.SocketException: Address family not supported by protocol

解决了它:

c.moveToFirst();
            if(c.getColumnCount()!=0)

                 fetch();


        }

2 个答案:

答案 0 :(得分:3)

你无法显示对话,因为你得到了c.getCount()= 0,这就是为什么下面如果不满足条件并且从不调用fetch()方法,检查c.getCount()的值,你的对话框代码是对的。

  if(c.getCount()!=0)

                     fetch();


            }

答案 1 :(得分:1)

您的对话框代码没问题。 检查光标值

c.getCount()= 0就是问题。