I have little problem in diplaying toast. I have four activity in which three activity should not open untill fourth activity setting not completed.

In main activty I have four text on click of these I am suppose to got that activity But before that I am checking in Setting table..which is in forth activity..first complete setting then and then only u go to other activity.Thats why I am Displaying Toast Please Complete Setting First.Below code is checking is setting table is empty.But on first click on text1 message not display.But When I am go to fourth activity and come back to main then it display toast.If any one whats the problem then please let me know

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);   

            }
        });

Toast Message method :

 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;
}  

}

有帮助吗?

解决方案 2

Is it possible that , before you move to the fourth activity once, some value in this code is causeing an exception. (app won't crash as you have caught it)

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

try to toast a error message in catch block

maybe your CheckSettingData() is returning null before you instantiate the 4th activity atleast one time. you might update the question with code for CheckSettingData()

其他提示

// 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();
}

Try this,

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(); 
    }
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top