[英]OnTabListener in Android not working
I have run into a problem where when I put the name of a fragment, an error occurs as shows: 我遇到了一个问题,当我输入片段的名称时,会出现如下所示的错误:
Type mismatch: cannot convert from Fragment1 to Fragment
Here is my MainActivity.java code: 这是我的MainActivity.java代码:
package com.example.currencyconverter;
import android.app.ActionBar;
import android.app.ActionBar.Tab;
import android.app.Activity;
import android.app.Fragment;
import android.app.FragmentTransaction;
import android.content.Intent;
import android.os.Bundle;
import android.os.StrictMode;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.LayoutInflater;
import android.view.View;
public class MainActivity extends Activity implements OnClickListener {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ActionBar actionBar = getActionBar();
actionBar.setSubtitle("Created By Rohit Nandakumar");
actionBar.setTitle("Currency Converter");
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
ActionBar.Tab Frag1 = actionBar.newTab().setText("Converter");
ActionBar.Tab Frag2 = actionBar.newTab().setText("Currencies");
Fragment fragment1 = new Fragment1();
Fragment fragment2 = new Fragment2();
Frag1.setTabListener(new MyTabsListener(fragment1));
Frag2.setTabListener(new MyTabsListener(fragment2));
actionBar.addTab(Frag1);
actionBar.addTab(Frag2);
}
Here is my Fragment1 code: 这是我的Fragment1代码:
package com.example.currencyconverter;
import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
public class Fragment1 extends Fragment{
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
return inflater.inflate(R.layout.activity_main, container, false);
}
}
This is my Fragment 2 code: 这是我的片段2代码:
package com.example.currencyconverter;
import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
public class Fragment2 extends Fragment{
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
return inflater.inflate(R.layout.activity_first, container, false);
}
}
Here is the MyTabsListener.java file: 这是MyTabsListener.java文件:
package com.example.currencyconverter;
import android.app.ActionBar.Tab;
import android.app.ActionBar.TabListener;
import android.app.ActionBar;
import android.app.Fragment;
import android.app.FragmentTransaction;
public class MyTabsListener implements TabListener {
public Fragment fragment;
public MyTabsListener(Fragment fragment) {
this.fragment = fragment;
}
@Override
public void onTabReselected(Tab tab, FragmentTransaction ft) {
// TODO Auto-generated method stub
}
@Override
public void onTabSelected(Tab tab, FragmentTransaction ft) {
// TODO Auto-generated method stub
ft.replace(R.id.fragment_container, fragment);
}
@Override
public void onTabUnselected(Tab tab, FragmentTransaction ft) {
// TODO Auto-generated method stub
}
} The problem occurs right here in the MainActivity.java file: }此问题发生在MainActivity.java文件中:
Fragment fragment1 = new Fragment1();
Fragment fragment2 = new Fragment2();
The 的
new Fragment1();
and the 和
new Fragment2();
part of the code is highlighted and it says: 部分代码突出显示,并指出:
Type mismatch: cannot convert from Fragment1 to Fragment
What is happening? 怎么了? What am i doing wrong?
我究竟做错了什么? Any help is greatly appreciated.
任何帮助是极大的赞赏。 I have already asked this question before, but I have gotten no answers that are correct.
我之前已经问过这个问题,但是没有得到正确的答案。 I have searched all over the internet trying to find the answer.
我已经在整个互联网上进行搜索,试图找到答案。 Again, any help is greatly appreciated.
再次感谢您的帮助。
Instead of: 代替:
Fragment fragment1 = new Fragment1();
Fragment fragment2 = new Fragment2();
Try to do: 试着做:
Fragment1 fragment1 = new Fragment1();
Fragment2 fragment2 = new Fragment2();
You need to create new objects of the Fragment classes that you have created( ie create objects of classes Fragment1
and Fragment2
not the objects of type Fragment
. ) 您需要创建已创建的Fragment类的新对象(即,创建
Fragment1
和Fragment2
类的对象,而不是Fragment
类型的对象。)
The way you are doing, you are creating the object of type Fragment
but initializing it with the types Fragment1
and Fragment2
. 这样,您将创建
Fragment
类型的对象,但使用Fragment1
和Fragment2
类型对其进行初始化。 That's why it is giving the error. 这就是为什么它会给出错误。
Hope this helps. 希望这可以帮助。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.