-3

この最初のセクションでは、Fragment Activity を使用してタブを作成しました。最初の Tab をクリックすると、Google マップの現在の位置が表示されます。このコードを手伝ってください。私の主な活動は次のとおりです

public class MainTabActivity extends FragmentActivity {
    private FragmentTabHost mTabHost;
    static String TAB_A = "First Tab";
    static String TAB_B = "Second Tab";
    static String TAB_C=  "Third Tab";
    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.bottom_tabs);

        mTabHost = (FragmentTabHost) findViewById(android.R.id.tabhost);
        mTabHost.setup(this, getSupportFragmentManager(), R.id.realtabcontent);



        mTabHost.addTab(
                mTabHost.newTabSpec("first").setIndicator(createTabView(TAB_A, R.drawable.ic_tab_dialer)),
                MainActivity.class, null);

                mTabHost.addTab(
                mTabHost.newTabSpec("second").setIndicator(createTabView(TAB_A, R.drawable.ic_tab_dialer)),
                Fragment1.class, null);

                mTabHost.addTab(
                mTabHost.newTabSpec("third").setIndicator(createTabView(TAB_A, R.drawable.ic_tab_dialer)),
                Fragment1.class, null);


            }

    private View createTabView(final String text, final int id)
    {
        View view = LayoutInflater.from(this).inflate(R.layout.tabs_icon, null);
        ImageView imageView =   (ImageView) view.findViewById(R.id.tab_icon);
        imageView.setImageDrawable(getResources().getDrawable(id));((TextView) view.findViewById(R.id.tab_text)).setText(text);
        return view;
    }

次のような Map クラス

public class MainActivity extends FragmentActivity {

GoogleMap googleMap;
MarkerOptions markerOptions;
LatLng latLng;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    SupportMapFragment supportMapFragment = (SupportMapFragment)
    getSupportFragmentManager().findFragmentById(R.id.map);

    // Getting a reference to the map
    googleMap = supportMapFragment.getMap();

    // Getting reference to btn_find of the layout activity_main
    Button btn_find = (Button) findViewById(R.id.btn_find);

    // Defining button click event listener for the find button
    OnClickListener findClickListener = new OnClickListener() {
        @Override
        public void onClick(View v) {
            // Getting reference to EditText to get the user input location
            EditText etLocation = (EditText) findViewById(R.id.et_location);

            // Getting user input location
            String location = etLocation.getText().toString();

            if(location!=null && !location.equals("")){
                new GeocoderTask().execute(location);
            }
        }
    };

    // Setting button click event listener for the find button
    btn_find.setOnClickListener(findClickListener);

}



// An AsyncTask class for accessing the GeoCoding Web Service
private class GeocoderTask extends AsyncTask<String, Void, List<Address>>{

    @Override
    protected List<Address> doInBackground(String... locationName) {
        // Creating an instance of Geocoder class
        Geocoder geocoder = new Geocoder(getBaseContext());
        List<Address> addresses = null;

        try {
            // Getting a maximum of 3 Address that matches the input text
            addresses = geocoder.getFromLocationName(locationName[0], 3);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return addresses;
    }

    @Override
    protected void onPostExecute(List<Address> addresses) {

        if(addresses==null || addresses.size()==0){
            Toast.makeText(getBaseContext(), "No Location found", Toast.LENGTH_SHORT).show();
        }

        // Clears all the existing markers on the map
        googleMap.clear();

        // Adding Markers on Google Map for each matching address
        for(int i=0;i<addresses.size();i++){

            Address address = (Address) addresses.get(i);

            // Creating an instance of GeoPoint, to display in Google Map
            latLng = new LatLng(address.getLatitude(), address.getLongitude());

            String addressText = String.format("%s, %s",
            address.getMaxAddressLineIndex() > 0 ? address.getAddressLine(0) : "",
            address.getCountryName());

            markerOptions = new MarkerOptions();
            markerOptions.position(latLng);
            markerOptions.title(addressText);

            googleMap.addMarker(markerOptions);

            // Locate the first location
            if(i==0)
                googleMap.animateCamera(CameraUpdateFactory.newLatLng(latLng));
        }
    }
}

そして、次のようにエラーが発生しています

    10-30 06:44:20.128: E/Trace(4500): error opening trace file: No such file or directory (2)
10-30 06:44:20.908: D/AndroidRuntime(4500): Shutting down VM
10-30 06:44:20.908: W/dalvikvm(4500): threadid=1: thread exiting with uncaught exception (group=0x40a71930)
10-30 06:44:20.928: E/AndroidRuntime(4500): FATAL EXCEPTION: main
10-30 06:44:20.928: E/AndroidRuntime(4500): java.lang.ClassCastException: com.example.mapexampleapp.MainActivity cannot be cast to android.support.v4.app.Fragment
10-30 06:44:20.928: E/AndroidRuntime(4500):     at android.support.v4.app.Fragment.instantiate(Fragment.java:402)
10-30 06:44:20.928: E/AndroidRuntime(4500):     at android.support.v4.app.FragmentTabHost.doTabChanged(FragmentTabHost.java:339)
10-30 06:44:20.928: E/AndroidRuntime(4500):     at android.support.v4.app.FragmentTabHost.onAttachedToWindow(FragmentTabHost.java:276)
10-30 06:44:20.928: E/AndroidRuntime(4500):     at android.view.View.dispatchAttachedToWindow(View.java:11937)
10-30 06:44:20.928: E/AndroidRuntime(4500):     at android.view.ViewGroup.dispatchAttachedToWindow(ViewGroup.java:2415)
10-30 06:44:20.928: E/AndroidRuntime(4500):     at android.view.ViewGroup.dispatchAttachedToWindow(ViewGroup.java:2422)
10-30 06:44:20.928: E/AndroidRuntime(4500):     at android.view.ViewGroup.dispatchAttachedToWindow(ViewGroup.java:2422)
10-30 06:44:20.928: E/AndroidRuntime(4500):     at android.view.ViewGroup.dispatchAttachedToWindow(ViewGroup.java:2422)
10-30 06:44:20.928: E/AndroidRuntime(4500):     at android.view.ViewGroup.dispatchAttachedToWindow(ViewGroup.java:2422)
10-30 06:44:20.928: E/AndroidRuntime(4500):     at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1192)
10-30 06:44:20.928: E/AndroidRuntime(4500):     at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:989)
10-30 06:44:20.928: E/AndroidRuntime(4500):     at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:4351)
10-30 06:44:20.928: E/AndroidRuntime(4500):     at android.view.Choreographer$CallbackRecord.run(Choreographer.java:749)
10-30 06:44:20.928: E/AndroidRuntime(4500):     at android.view.Choreographer.doCallbacks(Choreographer.java:562)
10-30 06:44:20.928: E/AndroidRuntime(4500):     at android.view.Choreographer.doFrame(Choreographer.java:532)
10-30 06:44:20.928: E/AndroidRuntime(4500):     at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:735)
10-30 06:44:20.928: E/AndroidRuntime(4500):     at android.os.Handler.handleCallback(Handler.java:725)
10-30 06:44:20.928: E/AndroidRuntime(4500):     at android.os.Handler.dispatchMessage(Handler.java:92)
10-30 06:44:20.928: E/AndroidRuntime(4500):     at android.os.Looper.loop(Looper.java:137)
10-30 06:44:20.928: E/AndroidRuntime(4500):     at android.app.ActivityThread.main(ActivityThread.java:5041)
10-30 06:44:20.928: E/AndroidRuntime(4500):     at java.lang.reflect.Method.invokeNative(Native Method)
10-30 06:44:20.928: E/AndroidRuntime(4500):     at java.lang.reflect.Method.invoke(Method.java:511)
10-30 06:44:20.928: E/AndroidRuntime(4500):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
10-30 06:44:20.928: E/AndroidRuntime(4500):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
10-30 06:44:20.928: E/AndroidRuntime(4500):     at dalvik.system.NativeStart.main(Native Method)
4

1 に答える 1

0
10-30 06:44:20.928: E/AndroidRuntime(4500): java.lang.ClassCastException: com.example.mapexampleapp.MainActivity cannot be cast to android.support.v4.app.Fragment
10-30 06:44:20.928: E/AndroidRuntime(4500):     at android.support.v4.app.Fragment.instantiate(Fragment.java:402)
10-30 06:44:20.928: E/AndroidRuntime(4500):     at android.support.v4.app.FragmentTabHost.doTabChanged(FragmentTabHost.java:339)

クラス キャスト例外。タブホストの初期化中にキャストしよMainActivityうとしました。Fragmentこれが原因です:

mTabHost.addTab(
        mTabHost.newTabSpec("first").setIndicator(createTabView(TAB_A, R.drawable.ic_tab_dialer)),
        MainActivity.class, null);

ではなく、フラグメント クラスをタブ ホストに渡す必要がありますMainActivity.class

于 2013-10-30T11:01:00.277 に答える