无法解析setContentView(int)和findViewById(int)API

时间:2015-09-22 15:24:53

标签: android android-fragments android-activity

我是Android开发的新手。我正在创建扩展Fragmentactivity的Activity。编译器说无法解析setContentView(int)和findViewById(int)。

import android.app.Activity;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.app.FragmentTransaction;
import android.support.v4.view.ViewPager;
import android.support.v4.app.Fragment;
import android.os.Bundle;
import android.app.ActionBar;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;


public class MainActivity extends FragmentActivity
implements ActionBar.TabListener {

CollectionPageAdapter my_adapter;
ViewPager mViewPager;

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

    my_adapter = new CollectionPageAdapter(getSupportFragmentManager());

    mViewPager = (ViewPager) findViewById(R.id.pager);
    mViewPager.setAdapter(my_adapter);
}

XML文件:

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" android:id="@+id/container"
android:layout_width="match_parent" android:layout_height="match_parent"
tools:context=".MainActivity" tools:ignore="MergeRootFrame">

<android.support.v4.view.ViewPager
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:id="@+id/pager"
  android:layout_width="match_parent"
  android:layout_height="match_parent"
/>

</FrameLayout>

我不明白这里有什么问题。

请帮帮我。

1 个答案:

答案 0 :(得分:-1)

您不要在片段中致电setContentView,而是需要从View

返回onCreateView

试试这个

 public View onCreateView(LayoutInflater inflater, ViewGroup container,
                                 Bundle savedInstanceState) {

                  ...... //code
         View rootView = inflater.inflate(R.layout.activity_main, container, false);     
         ListView mListView = (ListView) getActivity().findViewById(R.id.listview_forecast);
         mListView.setAdapter(mForecastAdapter);

           //view 
            return rootView;
        }

请随便问一下,我可以帮助你