所有人的祝福,在此应用程序中,需要更改菜单项单击上的文本颜色。 Textview是在自定义行布局中,由适配器和片段Tab布局中设置的适配器夸大。在菜单项上,单击需要更改listview整个listview的文本颜色。在正常应用程序上,它可以正常工作,但在片段上,如果有人可以提供帮助,它将在错误“空对象引用上给出Adaptor.setTextColor(int)'”。谢谢
MainActivity.java
private SectionsPagerAdapter mSectionsPagerAdapter;
private ViewPager mViewPager;
NewsAdaptor adaptor;
protected void onCreate ( Bundle savedInstanceState ) {
super.onCreate(savedInstanceState);
Context mContext = getApplicationContext();
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
mSectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager());
mViewPager = (ViewPager) findViewById(R.id.container);
mViewPager.setAdapter(mSectionsPagerAdapter);
mViewPager.setOffscreenPageLimit(10);
TabLayout tabLayout = (TabLayout) findViewById(R.id.tabs);
mViewPager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(tabLayout));
tabLayout.addOnTabSelectedListener(new TabLayout.ViewPagerOnTabSelectedListener(mViewPager));
}
@Override
public boolean onCreateOptionsMenu ( Menu menu ) {
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
@SuppressLint("ResourceType")
@Override
public boolean onOptionsItemSelected ( MenuItem item ) {
int id = item.getItemId();
if (id == R.id.chagecolor) {
adaptor.setTextColor(Color.GREEN);
adaptor.notifyDataSetChanged();
}
return super.onOptionsItemSelected(item);
}
newsApapter.java
public class NewsAdaptor extends BaseAdapter {
TextView tvtitle;
private int color;
Context context;
public NewsAdaptor ( Context context, ArrayList <NewsItem> newsList ) {
this.context = context;
this.newsList = newsList;
this.color = Color.RED;
}
ArrayList<NewsItem> newsList;
@Override
public int getCount () {
return newsList.size();
}
@Override
public Object getItem ( int position ) {
return newsList.get(position);
}
@Override
public long getItemId ( int position ) {
return 0;
}
@Override
public View getView ( int position, View convertView, ViewGroup parent ) {
if (convertView == null){
convertView=View.inflate(context, R.layout.newsitemlist_layout,null);
}
NewsItem currentNews = newsList.get(position);
tvtitle = (TextView) convertView.findViewById(R.id.textView1id);
tvtitle.setText(currentNews.getTitle());
tvtitle.setTextColor(color);
return convertView;
}
public void setTextColor(int color) {
this.color = color;
}
}
现在在实现了whare适配器的片段中
NEwsFragment.java
public class TRADE extends Fragment {
public TRADE () {
// Required empty public constructor
}
ArrayList<NewsItem> newsItemsList;
ListView ali;
NewsAdaptor adapter;
@Override
public View onCreateView ( LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState ) {
View view= inflater.inflate(R.layout.fragment_trade, container, false);
newsItemsList = new ArrayList<>();
ali = (ListView)view. findViewById(R.id.listtrade);
RequestQueue requestQueue = Volley.newRequestQueue(getContext());
StringRequest stringRequest = new StringRequest("https://urdu.geo.tv/rss/1/3", new Response.Listener <String>() {
@Override
public void onResponse ( String response ) {
Document document = Jsoup.parse(response);
Elements itemElements = document.getElementsByTag("item");
for (int i = 0; i < itemElements.size(); i++) {
Element item = itemElements.get(i);
String title = rem(item.child(0).text());
NewsItem news = new NewsItem();
news.title = title;
newsItemsList.add(news);
}
adapter = new NewsAdaptor(getContext(), newsItemsList);
ali.setAdapter(adapter);
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse ( VolleyError error ) {
}
});
requestQueue.add(stringRequest);
newsitemlist_layout.xml
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:id="@+id/textView1id"
android:layout_width="237dp"
android:layout_height="83dp"
android:layout_alignEnd="@+id/pubDateid"
android:layout_alignParentTop="true"
android:layout_alignRight="@+id/pubDateid"
android:ellipsize="end"
android:gravity="right"
android:text="News TITLE"
android:textSize="18sp"
android:textStyle="bold" />
</RelativeLayout>
答案 0 :(得分:0)
尝试这种方式 在color.xml中添加绿色 并尝试这种方式
@SuppressLint("ResourceType")
@Override
public boolean onOptionsItemSelected ( MenuItem item ) {
int id = item.getItemId();
if (id == R.id.chagecolor) {
adaptor.setTextColor(ContextCompat.getColor(context, R.color.green));
adaptor.notifyDataSetChanged();
}
return super.onOptionsItemSelected(item);
}
答案 1 :(得分:0)
MainActivity 和 Fragment 中的 adaptor 变量不同,因此 MainActivity中的 adaptor < / strong>尚未定义。因此,您将得到nullPointerException。我认为您需要重新设计代码或尝试在 Fragment 类上实现此代码。
首先
@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setHasOptionsMenu(true); //Remember this line
}
然后
@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
inflater.inflate(R.menu.your_frag_menu, menu);
super.onCreateOptionsMenu(menu, inflater);
}
最后,
@Override
public boolean onOptionsItemSelected ( MenuItem item ) {
int id = item.getItemId();
if (id == R.id.chagecolor) {
adaptor.setTextColor(Color.GREEN);
adaptor.notifyDataSetChanged();
}
return super.onOptionsItemSelected(item);
}
希望它会有所帮助:)
答案 2 :(得分:0)
您的onOptionsItemSelected方法(位于MainActivity上)不知道适配器对象的声明,因为您没有在MainActivity上声明它。您要做的是
1)从MainActivity中删除onOptionsItemSelected代码。
2)启用片段中的选项菜单:
@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setHasOptionsMenu(true);
}
3)然后将其充气:
@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
inflater.inflate(R.menu.your_frag_menu, menu);
super.onCreateOptionsMenu(menu, inflater);
}
4)然后使用onOptionsItemSelected回调进行操作:
@Override
public boolean onOptionsItemSelected (MenuItem item) {
final int id = item.getItemId();
if (id == R.id.chagecolor) {
adaptor.setTextColor(getResources.getColor(R.color.GREEN);
adaptor.notifyDataSetChanged();
}
return super.onOptionsItemSelected(item);
}