2013-03-26 68 views
-1

我正在關注ActionBar的Google API庫,但無法將其導航回家。沒有什麼被LogCat捕獲....所以沒有錯誤,但語法不允許導航。ActionBar圖標點擊時圖片沒有做任何事情?

我也承認我沒有太多的Actionbar經驗。

我不明白爲什麼這不起作用,非常感謝。

這是我的活動代碼在這裏:

@SuppressLint("NewApi") 
public class MoveContactsActivity extends ListActivity { 

    @SuppressLint("NewApi") 
    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_move_contacts); 
//  getActionBar().setHomeButtonEnabled(true); 
     ActionBar actionBar = getActionBar(); 
     actionBar.setDisplayHomeAsUpEnabled(true); 
     actionBar.setTitle("My Relocation"); 
     actionBar.setIcon(R.drawable.menu_ic_launcher); 
     Bundle bundle = getIntent().getExtras(); 
     final TextView welcomeText = (TextView) findViewById(R.id.WelcomeText); 
     final TextView relocationId = (TextView) findViewById(R.id.relocationID); 
     final TextView addressOne = (TextView) findViewById(R.id.addressOne); 
     final TextView addressTwo = (TextView) findViewById(R.id.addressTwo); 
     final String RelocationId = bundle.getString("UID1"); 
     relocationId.setText("Your Relocation: #" + RelocationId); 
     new contentCall().execute(""); 

     findViewById(R.id.WebAddressTwo).setOnClickListener(new OnClickListener(){ 

      @Override 
      public void onClick(View v) { 
       Uri uri = Uri.parse("http://www.Relocationmw.com"); 
       Intent intent = new Intent(Intent.ACTION_VIEW, uri); 
       startActivity(intent); 
      } 

     }); 
    } 

    @Override 
    public boolean onCreateOptionsMenu(Menu menu) { 
     // Inflate the menu; this adds items to the action bar if it is present. 
     getMenuInflater().inflate(R.menu.activity_move_contacts, menu); 
     return true; 
    } 

    private class contentCall extends AsyncTask<String, Void, String> { 
     ProgressDialog dialog = new ProgressDialog(MoveContactsActivity.this); 

     @Override 
     protected void onPreExecute() { 
      if (checkNullState() == false) { 
       showMyDialog(); 
      } 
     } 

     @Override 
     protected String doInBackground(String... params) { 
      String xml = null; 
      String calling = "https://demo.relocationmw.com/ws_docmgmt/mobile.asmx/GetRelocationMoveContactsTrim?UserID="; 
      Bundle bundle = getIntent().getExtras(); 
      // String UID = bundle.getString("UID"); 
      String UID = "801"; 
      String URL = calling + UID; 
      DefaultHttpClient client = new DefaultHttpClient(); 
      HttpGet httpGet = new HttpGet(URL); 
      HttpResponse httpResponse; 
      try { 
       httpResponse = client.execute(httpGet); 
       HttpEntity httpEntity = httpResponse.getEntity(); 
       xml = EntityUtils.toString(httpEntity); 
      } catch (ClientProtocolException e) { 
       // TODO Auto-generated catch block 
       e.printStackTrace(); 
      } catch (IOException e) { 
       // TODO Auto-generated catch block 
       e.printStackTrace(); 
      } 
      return xml; 
     } 

     @Override 
     protected void onPostExecute(String result) { 
      String xml = result; 
      String KEY_ITEM = "Contact"; 
      String KEY_CONTACTTITLE = "ContactTitle"; 
      String KEY_NAME = "Name"; 
      String KEY_MAINPHONE = "Phone"; 
      String KEY_CELLPHONE = "Cell"; 
      String KEY_FAX = "Fax"; 
      String KEY_EMAIL = "Email"; 
      ArrayList<HashMap<String, String>> menuItems = new ArrayList<HashMap<String, String>>(); 

      Document doc = getDomElement(xml); 
      if (xml != null) { 
       { 
        NodeList nl = doc.getElementsByTagName(KEY_ITEM); 
        for (int i = 0; i < nl.getLength(); i++) { 
         HashMap<String, String> map = new HashMap<String, String>(); 
         Element e = (Element) nl.item(i); 
          map.put(KEY_CONTACTTITLE, getValue(e, KEY_CONTACTTITLE)); 
          map.put(KEY_NAME, getValue(e, KEY_NAME)); 
          map.put(KEY_MAINPHONE, getValue(e, KEY_MAINPHONE)); 
          map.put(KEY_CELLPHONE, getValue(e, KEY_CELLPHONE)); 
          map.put(KEY_FAX, getValue(e, KEY_FAX)); 
          map.put(KEY_EMAIL, getValue(e, KEY_EMAIL)); 
         menuItems.add(map); 
        } 

        String[] hashMapObjects = { KEY_CONTACTTITLE, KEY_NAME, 
          KEY_MAINPHONE, KEY_CELLPHONE, KEY_FAX, KEY_EMAIL }; 
        int[] listItemObjects = { R.id.ContactTitle, R.id.Name, 
          R.id.MainPhone, R.id.Cell, R.id.Fax, R.id.Email }; 
//    ContactListAdapter adapter = new ContactListAdapter(MoveContactsActivity.this, 0, menuItems); 
        SimpleAdapter adapter = new SimpleAdapter (MoveContactsActivity.this,menuItems,R.layout.listed_contacts,hashMapObjects, listItemObjects); 
        SimpleAdapter.ViewBinder binder = new SimpleAdapter.ViewBinder() { 

         @Override 
         public boolean setViewValue(View view, Object object, 
           String value) { 
          if(!(view instanceof TextView)){ 
           return false; 
          } 
          final TextView text = (TextView) view; 
          text.setVisibility(TextUtils.isEmpty(value) ? View.GONE : View.VISIBLE); 
          text.setText(value); 
          return true; 
         } 
        }; 
        adapter.setViewBinder(binder); 
        setListAdapter(adapter); 
       } 
       if (checkNullState() == true) { 
        dismissMyDialog(); 
       } 
      } 
     } 
    } 

    protected void onProgressUpdate(Map... values) { 
    } 

    // XML Parsing Start 
    public Document getDomElement(String xml) { 
     Document doc = null; 
     DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); 
     try { 

      DocumentBuilder db = dbf.newDocumentBuilder(); 

      InputSource is = new InputSource(); 
      is.setCharacterStream(new StringReader(xml)); 
      doc = db.parse(is); 

     } catch (ParserConfigurationException e) { 
      Log.e("Error: ", e.getMessage()); 
      return null; 
     } catch (SAXException e) { 
      Log.e("Error: ", e.getMessage()); 
      return null; 
     } catch (IOException e) { 
      Log.e("Error: ", e.getMessage()); 
      return null; 
     } 
     // return DOM 
     return doc; 
    } 

    public String getValue(Element item, String str) { 
     NodeList n = item.getElementsByTagName(str); 
     return getElementValue(n.item(0)); 
    } 

    public final String getElementValue(Node elem) { 
     Node child; 
     if (elem != null) { 
      if (elem.hasChildNodes()) { 
       for (child = elem.getFirstChild(); child != null; child = child 
         .getNextSibling()) { 
        if (child.getNodeType() == Node.TEXT_NODE) { 
         return child.getNodeValue(); 
        } 
       } 
      } 
     } 
     return null; 
    } 

    // XML Parsing Stop 

    // ProgressDialog Tracking 
    private ProgressDialog pd1; 
    private boolean isMyDialogShowing; 

    boolean checkNullState() { 
     boolean test; 
     if (pd1 != null) { 
      test = true; 
     } else { 
      test = false; 
     } 
     return test; 
    } 

    void showMyDialog() { 
     isMyDialogShowing = true; 
     pd1 = new ProgressDialog(MoveContactsActivity.this); 
     pd1.setTitle("Contacts"); 
     pd1.setMessage("Loading Contacts...."); 
     pd1.show(); 
    } 

    void dismissMyDialog() { 
     pd1.dismiss(); 
     isMyDialogShowing = false; 
    } 
    @Override 
    public boolean onOptionsItemSelected(MenuItem item){ 
     switch(item.getItemId()){ 
     case R.id.menu_settings: 
      Intent intent = new Intent(this, HomeActivity.class); 
      intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); 
      startActivity(intent); 
      return true; 
      default: 
       return super.onOptionsItemSelected(item); 
     } 
    } 
} 

這是菜單XML .....這就是我認爲這個問題是但就像我說我不是偉大的動作條呢。

<menu xmlns:android="http://schemas.android.com/apk/res/android" > 

    <item 
     android:id="@+id/menu_settings" 
     android:orderInCategory="100" 
     android:showAsAction="never" 
     android:title="@string/menu_settings"/> 

</menu 

>

這是logcat的錯誤,當我改變的情況下,以

case android.R.id.home: 

的logcat:

03-26 13:45:40.666: E/AndroidRuntime(19152): FATAL EXCEPTION: main 
03-26 13:45:40.666: E/AndroidRuntime(19152): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.testingapp/com.testingapp.HomeActivity}: java.lang.NullPointerException 
03-26 13:45:40.666: E/AndroidRuntime(19152): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2100) 
03-26 13:45:40.666: E/AndroidRuntime(19152): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2125) 
03-26 13:45:40.666: E/AndroidRuntime(19152): at android.app.ActivityThread.access$600(ActivityThread.java:140) 
03-26 13:45:40.666: E/AndroidRuntime(19152): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1227) 
03-26 13:45:40.666: E/AndroidRuntime(19152): at android.os.Handler.dispatchMessage(Handler.java:99) 
03-26 13:45:40.666: E/AndroidRuntime(19152): at android.os.Looper.loop(Looper.java:137) 
03-26 13:45:40.666: E/AndroidRuntime(19152): at android.app.ActivityThread.main(ActivityThread.java:4898) 
03-26 13:45:40.666: E/AndroidRuntime(19152): at java.lang.reflect.Method.invokeNative(Native Method) 
03-26 13:45:40.666: E/AndroidRuntime(19152): at java.lang.reflect.Method.invoke(Method.java:511) 
03-26 13:45:40.666: E/AndroidRuntime(19152): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1008) 
03-26 13:45:40.666: E/AndroidRuntime(19152): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:775) 
03-26 13:45:40.666: E/AndroidRuntime(19152): at dalvik.system.NativeStart.main(Native Method) 
03-26 13:45:40.666: E/AndroidRuntime(19152): Caused by: java.lang.NullPointerException 
03-26 13:45:40.666: E/AndroidRuntime(19152): at com.testingapp.HomeActivity.onCreate(HomeActivity.java:50) 
03-26 13:45:40.666: E/AndroidRuntime(19152): at android.app.Activity.performCreate(Activity.java:5206) 
03-26 13:45:40.666: E/AndroidRuntime(19152): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1094) 
03-26 13:45:40.666: E/AndroidRuntime(19152): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2064) 
03-26 13:45:40.666: E/AndroidRuntime(19152): ... 11 more 

回答

1

沒有菜單或家庭工作。
您嘗試訪問HomeActivity類的onCreate()函數上的NULL對象的問題。
檢查管線50以HomeActivity.java

Caused by: java.lang.NullPointerException 
at com.testingapp.HomeActivity.onCreate(HomeActivity.java:50) 
+0

這就是:final String RelocationId = bundle.getString(「UID1」); ......當意圖被觸發回家時,ActionBAr – Keeano 2013-03-26 20:17:21

+0

包中的值僅爲Null,爲空,您尚未設置任何額外的啓動Home Activty,你需要添加一個檢查if(null!= bundle){.....}或者將一些數據放在intent.putExtra(「UID1」,「value」)中;在開始活動之前。 – JafarKhQ 2013-03-26 20:23:19

+0

非常感謝,告訴我爲什麼,我能夠把更多的東西放在爲什麼我沒有檢查,但我得到它修復,現在奇妙的工作,現在謝謝你! – Keeano 2013-03-26 21:44:52

-1

對於我來說只能驗證碼:

@Override 
protected void onCreate(Bundle savedInstanceState) {  
    // your code goes here... 
    ActionBar actionBar = getActionBar(); 
    actionBar.setDisplayHomeAsUpEnabled(true);   
    actionBar.setHomeButtonEnabled(true); 
} 

@Override 
public boolean onOptionsItemSelected(MenuItem item) { 
    super.onOptionsItemSelected(item); 

    if (item.getItemId() == android.R.id.home) { 
     finish(); 
    } 

    return true; 
} 
+0

?爲什麼工作? – cHao 2013-03-26 19:46:40

相關問題