我在Activity中首先private定义了几个String变量..然后在OnCreate中有对这些String进行赋值..但是之后在onPrepareOptionsMenu中引用到的这个String时候他的值并不是我想要的那个..这是为什么..还有怎么解决..谢谢..

解决方案 »

  1.   

    按照你的逻辑描述,不可能不现上述现象,请仔细检查代码,String是否还被其他地方赋值, 或者把代码贴出来。
      

  2.   

    嗯..我去贴代码..嗯..是这样的..这个String sbstr的值如果登录正确应该是mobile..但是正确登录之后我的group1的菜单项还是没显示..
    顺便能否问下关于过一段时间就获取一下服务器上内容的功能怎么实现..我之前用Thread.sleep然后发现这个会导致程序未响应..抱歉我是新手很多东西都不懂= =麻烦大家了
    package sim.activity;import java.io.BufferedReader;
    import java.io.IOException;
    import java.io.InputStream;
    import java.io.InputStreamReader;
    import java.net.HttpURLConnection;
    import java.net.MalformedURLException;
    import java.net.URL;
    import android.app.Activity;
    import android.app.AlarmManager;
    import android.app.Notification;
    import android.app.NotificationManager;
    import android.app.PendingIntent;
    import android.app.Service;
    import android.content.BroadcastReceiver;
    import android.content.ContentValues;
    import android.content.Context;
    import android.content.Intent;
    import android.database.sqlite.SQLiteDatabase; 
    import android.database.sqlite.SQLiteDatabase.CursorFactory;
    import android.database.sqlite.SQLiteOpenHelper;
    import android.os.Bundle;
    import android.os.Handler;
    import android.os.IBinder;
    import android.os.Message;
    import android.telephony.TelephonyManager;
    import android.util.Log;
    import android.view.Menu;
    import android.view.MenuItem;
    import android.view.SubMenu;
    import android.view.ViewGroup;
    import android.view.ViewGroup.LayoutParams;
    import android.webkit.WebChromeClient;
    import android.webkit.WebView;
    import android.webkit.WebViewClient;
    import android.widget.TextView;
    import android.widget.Toast;public class SIMActivity extends Activity {
        /** Called when the activity is first created. */
         private WebView webView;
        private int group1 = 1;
        private int group2 = 2;
            private String sbstr = null;
            private String popup = null;
            private String popup1 = null;
            private TextView tv = (TextView)findViewById(R.id.tv1);
        private Context mContext;   
        private Notification mNotification;  
        private NotificationManager mNotificationManager;  
        private final static int NOTIFICATION_ID = 0x0001;     @Override    
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.main);
            webView = new WebView(this);
            setupViews();
            
            TelephonyManager tm = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);   
            
            String mdn = tm.getLine1Number();
            String imei1 = tm.getDeviceId();
            String imsi = tm.getSubscriberId();
            
            String imei2 ="0";
            String url1 = "";
            String Format1 = getResources().getString(R.string.imeis);
            String imei = "";
            int len = imei1.length();
            if(len == 14){
            imei = String.format(Format1,imei2,imei1);
            }
            else {imei = imei1;}
            
            String urll = String.format(Format1,url1,imei);
            
            //浏览器界面
            webView.loadUrl(urll);
            webView.setWebChromeClient(new WebChromeClient() {              public void onProgressChanged(WebView view, int progress) {  
                    setTitle("页面加载中,请稍候..." + progress + "%");  
                    setProgress(progress * 100);  
                    if (progress == 100) {  
                        setTitle(R.string.app_name);  
                    }  
                }  
            });  
            webView.setWebViewClient(new WebViewClient(){       
                         public boolean shouldOverrideUrlLoading(WebView view, String url) {       
                             view.loadUrl(url);       
                             return true;       
                         }       
             });
            
             setContentView(webView,new LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,
             ViewGroup.LayoutParams.WRAP_CONTENT));
             
             String httpUrl = "http://202.118.40.16/news/session.aspx";
             StringBuffer sb = new StringBuffer();
             URL urlx = null;
        try{
         urlx = new URL(httpUrl);
        }catch(MalformedURLException e){
         e.printStackTrace();
        }
        if (urlx != null){
         try{
         HttpURLConnection urlConn = 
         (HttpURLConnection) urlx.openConnection();
             urlConn.connect();
             InputStream input = urlConn.getInputStream();
             InputStreamReader inputReader = new InputStreamReader(input);
             BufferedReader reader = new BufferedReader(inputReader);
             String inputLine = null;
             while((inputLine = reader.readLine()) != null){
              sb.append(inputLine).append("\n");
             }
             reader.close();
             inputReader.close();
             input.close();
             urlConn.disconnect();
             }catch(IOException e){
              e.printStackTrace();
                 }
                }else{
              Log.i("TAG","url is null");
              }          if (sb != null){
                 
                  String sbtr = sb.toString();
                  int sblen = sbtr.length();
                  int index=sbtr.lastIndexOf("|");
                  
                  sbstr=sbtr.substring(index+1,sblen);
                  
                  tv.setText(sbstr);
              }else{
               tv.setText("cannot get page");
              }
              
              }
    }
    public boolean onCreateOptionsMenu(Menu menu) {
    menu.add(group2,1,1,"后退");
    menu.add(group2,2,2,"前进");
    SubMenu subMenu = menu.addSubMenu(group1,100,100,"通知");
    subMenu.add(group1,101,101,"显示通知");
    subMenu.add(group1,102,102,"不显示通知");
            return true;    }

    public boolean onPrepareOptionsMenu(Menu menu){  
    super.onPrepareOptionsMenu(menu); 
            if (sbstr != null){
             if (sbstr == "mobile"){
             menu.setGroupEnabled(group1, true);
             menu.setGroupVisible(group1, true);
             menu.setGroupVisible(group2, true);
             }else{
             menu.setGroupEnabled(group1, false);
             menu.setGroupVisible(group1, false);
             menu.setGroupVisible(group2, true);
             }
            }else{
         Log.i("TAG","null");
         }   
    return super.onPrepareOptionsMenu(menu); } 

        public boolean onOptionsItemSelected(MenuItem item){
         switch (item.getItemId()){
         case 1:
         if(webView.canGoBack()){
         webView.goBack();
         return true;
         }
         break;
         case 2:
         if(webView.canGoForward()){
         webView.goForward();
         return true;
         }
         break;
         case 101:
              Intent mIntent = new Intent(mContext,SIMActivity.class);  
           mIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);      
              PendingIntent mContentIntent =PendingIntent.getActivity(mContext,0, mIntent, 0);             
              mNotification.setLatestEventInfo(mContext, "通知", "有新的通知", mContentIntent);  
              mNotificationManager.notify(NOTIFICATION_ID, mNotification);           
         break;
         case 102:
         mNotificationManager.cancel(NOTIFICATION_ID);
         break;
             default:
              System.out.println("");
              return super.onOptionsItemSelected(item);
         }
         return true;
        }
        public void setupViews(){             mContext = SIMActivity.this;  
               mNotification = new Notification(R.drawable.ic_launcher,popup1,System.currentTimeMillis());
               mNotification.defaults = Notification.DEFAULT_SOUND;  
               mNotificationManager = (NotificationManager)this.getSystemService(NOTIFICATION_SERVICE);  
     }}
      

  3.   

    喔..还有就是隔一段时间获得到的字段是储存在popup里面的然后得用他与本地的txt或者数据库进行比对=  =如果已存在则不处理如果不存在则弹出通知并将popup添加到txt或者数据库中..我看SQLite好像没有查询具体内容的功能..是不是可以用txt.IndexOf来查找..然后如果返回-1就通知并添加如果不是-1就不处理呢..
      

  4.   

    难道上面那个贴多了=  =主要问题在这段..
    public class SIMActivity extends Activity {
    ...
            private String sbstr = null;
    ...
              if (sb != null){
                    
                  String sbtr = sb.toString();
                  int sblen = sbtr.length();
                  int index=sbtr.lastIndexOf("|");
                  
                  sbstr=sbtr.substring(index+1,sblen);
                  
                  tv.setText(sbstr);
              }else{
                  tv.setText("cannot get page");
              }
              
              } public boolean onPrepareOptionsMenu(Menu menu){  
            super.onPrepareOptionsMenu(menu); 
            if (sbstr != null){
                if (sbstr == "mobile"){
                    menu.setGroupEnabled(group1, true);
                    menu.setGroupVisible(group1, true);
                    menu.setGroupVisible(group2, true);
                }else{
                    menu.setGroupEnabled(group1, false);
                    menu.setGroupVisible(group1, false);
                    menu.setGroupVisible(group2, true);
                }
            }else{
                Log.i("TAG","null");
                }   
        return super.onPrepareOptionsMenu(menu); } 
    =  =啊.不小心把URL写出来了..求管理员编辑掉....
      

  5.   

    亲sbstr == "mobile" change to  TextUtils.equals(sbstr, "mobile");== 是地址比较, equals 是值比较,网上很多,你必须要了解这一点。
      

  6.   

    嗯..原来如此..谢谢楼上的0.0话说我的程序又开始has stopped unexpectedly了 = =..好奇怪..昨天还能运行..今天改的应该已经全删了啊...