2015-11-15 84 views
1
package com.example.dylan.sunshine; 

import android.app.Activity; 
import android.app.Fragment; 
import android.content.Intent; 
import android.os.Bundle; 
import android.util.Log; 
import android.view.LayoutInflater; 
import android.view.Menu; 
import android.view.MenuInflater; 
import android.view.MenuItem; 
import android.view.View; 
import android.view.ViewGroup; 
import android.widget.ShareActionProvider; 
import android.widget.TextView; 
import android.support.v4.view.MenuItemCompat; 

public class DetailActivity extends Activity { 




    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_detail); 
     if (savedInstanceState == null) { 
      getFragmentManager().beginTransaction().add(R.id.container, new DetailFragment()).commit(); 
     } 

    } 




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

    @Override 
    public boolean onOptionsItemSelected(MenuItem item) { 
     // Handle action bar item clicks here. The action bar will 
     // automatically handle clicks on the Home/Up button, so long 
     // as you specify a parent activity in AndroidManifest.xml. 
     int id = item.getItemId(); 

     //noinspection SimplifiableIfStatement 
     if (id == R.id.action_settings) { 
      startActivity(new Intent(this, SettingsActivity.class)); 
      return true; 
     } 

     return super.onOptionsItemSelected(item); 
    } 


    public static class DetailFragment extends Fragment { 


     private static final String LOG_TAG = DetailFragment.class.getSimpleName(); 
     private static final String FORECAST_SHARE_HASHTAG = " #SunshineApp"; 

     private String mForecastStr; 
     public DetailFragment() { 
      setHasOptionsMenu(true); 
     } 


     @Override 
     public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { 
      View rootView = inflater.inflate(R.layout.fragment_detail, container, false); 
      Intent intent = getActivity().getIntent(); 

      if (intent != null && intent.hasExtra(Intent.EXTRA_TEXT)) { 
       mForecastStr = intent.getStringExtra(Intent.EXTRA_TEXT); 
       ((TextView) rootView.findViewById(R.id.detail_text)) 
         .setText(mForecastStr); 


      } 
      return rootView; 
     } 

     @Override 
     public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) { 
      // Inflate the menu; this adds items to the action bar if it is present. 
      inflater.inflate(R.menu.detailfragment, menu); 

      // Retrieve the share menu item 
      MenuItem menuItem = menu.findItem(R.id.action_share); 

      // Get the provider and hold onto it to set/change the share intent. 
      ShareActionProvider mShareActionProvider = 
        (ShareActionProvider) MenuItemCompat.getActionProvider(menuItem); 

      // Attach an intent to this ShareActionProvider. You can update this at any time, 
      // like when the user selects a new piece of data they might like to share. 
      if (mShareActionProvider != null) { 
       mShareActionProvider.setShareIntent(createShareForecastIntent()); 
      } else { 
       Log.d(LOG_TAG, "Share Action Provider is null?"); 
      } 
     } 

     private Intent createShareForecastIntent() { 
      Intent shareIntent = new Intent(Intent.ACTION_SEND); 
      shareIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET); 
      shareIntent.setType("text/plain"); 
      shareIntent.putExtra(Intent.EXTRA_TEXT, 
        mForecastStr + FORECAST_SHARE_HASHTAG); 
      return shareIntent; 
     } 
    } 

    } 

这是我Detailactivity我爬MenuitemCompat它说,它无法解析符号错误“MenuitemCompat”MenuItemCompat无法解决

这是我detailfragment.xml

<?xml version="1.0" encoding="utf-8"?> 
<menu xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:bwq="http://schemas.android.com/apk/res-auto"> 

    <item 
     android:id="@+id/action_share" 
     android:title="@string/action_share" 
     android:showAsAction="always" 
     /> 

</menu> 

谁能帮我并向我解释我在做什么错是错的

解决:我对这篇文章使用了第二个答案 Want to use ViewPager, cannot get android.support.* to be recognized?

新增V4,V7和V13 libary的,并与我的一切都进口一些小的调整工作

回答

2

如果你还没有加入此导入到你的Detailactivity你可能会得到这个错误。

import android.support.v4.view.MenuItemCompat; 
+0

以及进口android.support.v4.view。我得到一个未使用的导入和MenuItemCompat我仍然得不到解决符号 –

+1

它适用于我。确保将其添加到您创建“ShareActionProvider”的活动中。 – krrish

+0

我更新帖子我张贴洞活动,现在你可以看到我使用进口jet我得到的错误我告诉你的导入和MenuItemCompat –

0

解决:我在本文中使用了第二个答案想要使用ViewPager,无法获取android.support。*进行识别吗?

新增V4,V7和V13 libary的,并与我的一切都进口一些小的调整工作