Toast 不显示 第一次在单击事件上启动活动



我在吐司上没有什么问题。我有四个活动,其中三个活动不应该打开,直到第四个活动设置未完成。

在主要活动中,我点击这些有四个文本,我应该得到那个活动但在此之前,我正在检查设置表。这是第四个活动..首先完成设置,然后只有您转到其他活动。这就是为什么我要展示吐司首先完成设置。下面的代码正在检查设置表是否为空。但是在第一次单击文本1消息时不显示。但是当我去第四个活动并回到主要活动时,它会显示吐司。如果有人有什么问题,请告诉我

text1.setOnClickListener(new OnClickListener()
        {
            @Override
            public void onClick(View arg0)
            { // TODO Auto-generated method stub
                   try
                   {
                    NewNewDataHelper db=new NewNewDataHelper(this);
                    List<String> list=db.CheckSettingData();
                    if(db.CheckSettingData().isEmpty())
                    {                       
                        showCustomSelectState();
                    }else
                    {
                        Intent i =new Intent(MainActivity.this,Log_Practice_timeActivity.class);
                        i.putExtra("s11","Log");
                        startActivity(i);
                     }              
                   }catch(Exception e)
                   {
                   }
            }
        });


text4.setOnClickListener(new OnClickListener()
        {
            @Override
            public void onClick(View arg0) 
            {
                // TODO Auto-generated method stub
                Intent i =new Intent(MainActivity.this,SettingActivity.class);
                startActivity(i);   
            }
        });

吐司留言方式:

 public void showCustomSelectState() 
{ 
   msg="Please Complete Setting First"; 
   LayoutInflater inflater = getLayoutInflater(); 
  //Inflate the Layout 
   View layout = inflater.inflate(R.layout.my_custom_toast1 (ViewGroup)findViewById(R.id.custom_toast_layout)); 
  TextView text = (TextView) layout.findViewById(R.id.textToShow);
 // Set the Text to show in
 TextView text.setText(msg); 
 Toast toast = new Toast(MainActivity.this); 
 toast.setGravity(Gravity.CENTER_VERTICAL, 0, 0);
 toast.setDuration(Toast.LENGTH_LONG); 
 toast.setView(layout);
 toast.show(); 
}
  My DataHelper Class And In this CheckSettingData Method is available
   >        public class NewNewDataHelper
         {         
              private Context context;
          private SQLiteDatabase db;
          NewNewOpenHelper myDbHelper;
    // Constructor to accept class instance in on click method.
       public NewNewDataHelper(OnClickListener onClickListener)
      {
    // TODO Auto-generated constructor stub
      this.context=context;
       myDbHelper = new NewNewOpenHelper(this.context);
    try 
    {
        myDbHelper.createDataBase();
    } 
    catch (IOException ioe) 
    {
        ioe.printStackTrace();}  
      }
  }      
 public List<String>CheckSettingData()
{
    this.db=myDbHelper.getDatabase();
    String strValue=null;
    String strValue1=null;
    String strValue2=null;
    String strValue3=null;
    Cursor c=null;
    ArrayList<String> ar=new ArrayList<String>();
    //Log.w("NewNewDataHelper", "param "+param);
    c=db.rawQuery("SELECT * from tb_settings",null);
    //Log.w("NewNewDataHelper", "c= "+c);
    if(c!=null)
    {
        //Log.w("NewNewDataHelper", "Inside IF");
        if(c.moveToFirst())
        {
            do
            {
    strValue=c.getString(c.getColumnIndex("state"));
    strValue1=c.getString(c.getColumnIndex  ("email_id"));    
    strValue2=c.getString(c.getColumnIndex("active_flag"));
    strValue3=c.getString(c.getColumnIndex("create_date")); 
    String str=strValue+"~"+strValue1+"~"+strValue3+"~"+strValue3;
                //Log.w("strValue=", strValue);
            ar.add(str);
            }
            while(c.moveToNext());
        }
    }
    c.close();
    db.close();
    myDbHelper.close();
    return ar;
}  

}

// try this way 
public void showCustomSelectState()
{
   String msg="Please Complete Setting First";
   View layout = LayoutInflater.from(this).inflate(R.layout.my_custom_toast1,null,false);
   TextView text = (TextView) layout.findViewById(R.id.textToShow);
   text.setText(msg);
   Toast toast = new Toast(this);
   toast.setGravity(Gravity.CENTER_VERTICAL, 0, 0);
   toast.setDuration(Toast.LENGTH_LONG);
   toast.setView(layout);
   toast.show();
}

有没有可能,在你移动到第四个活动之前,此代码中的某些值导致异常。(应用程序不会崩溃,因为您已经抓住了它)

NewNewDataHelper db=new NewNewDataHelper(this);
                    List<String> list=db.CheckSettingData();
                    if(db.CheckSettingData().isEmpty())

尝试在 catch 块中烘烤错误消息

也许您的CheckSettingData()在至少一次实例化第 4 个活动之前返回 null。 您可以使用代码更新问题CheckSettingData()

试试这个,

public void showCustomSelectState() 
    { 
        String msg="Please Complete Setting First"; 
        //Inflate the Layout 
        LayoutInflater layoutInflater = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View layout = layoutInflater.inflate(R.layout.my_custom_toast1, null);
        TextView text = (TextView) layout.findViewById(R.id.textToShow);
        // Set the Text to show in
        TextView text.setText(msg); 
        Toast toast = new Toast(MainActivity.this); 
        toast.setGravity(Gravity.CENTER_VERTICAL, 0, 0);
        toast.setDuration(Toast.LENGTH_LONG); 
        toast.setView(layout);
        toast.show(); 
    }

最新更新