1

我遇到了一个问题,当我输入片段的名称时,会发生错误,如下所示:

Type mismatch: cannot convert from Fragment1 to Fragment

这是我的 MainActivity.java 代码:

package com.example.currencyconverter;

import java.io.IOException;
import java.io.PrintWriter;
import java.io.StringWriter;
import java.text.DecimalFormat;

import org.apache.http.client.HttpClient;
import org.apache.http.client.ResponseHandler;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.BasicResponseHandler;
import org.apache.http.impl.client.DefaultHttpClient;

import android.app.ActionBar;
import android.app.ActionBar.Tab;
import android.app.Activity;
import android.app.Fragment;
import android.app.FragmentTransaction;
import android.app.ProgressDialog;
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;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;

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);
}

这是我的 Fragment1 代码:

package com.example.currencyconverter;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

public class Fragment1 {
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    return inflater.inflate(R.layout.activity_main, container, false);
}

}

这是我的片段 2 代码:

package com.example.currencyconverter;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

public class Fragment2 {
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    return inflater.inflate(R.layout.activity_first, container, false);
}

}

这是 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

    }



}

问题就出现在 MainActivity.java 文件中:

Fragment fragment1 = new Fragment1();
Fragment fragment2 = new Fragment2();

new Fragment1();

new Fragment2();

部分代码被突出显示,它说:

Type mismatch: cannot convert from Fragment1 to Fragment

怎么了?我究竟做错了什么?任何帮助是极大的赞赏。

4

2 回答 2

0

Fragment1Fragment2类不扩展Fragment

public Fragment1 extends Fragment {
    ...
}
于 2013-06-26T01:55:49.263 回答
0

尝试更换

Fragment fragment1 = new Fragment1();
Fragment fragment2 = new Fragment2();

Fragment1 fragment1 = new Fragment1();
Fragment2 fragment2 = new Fragment2();
于 2019-06-17T16:52:54.673 回答