对于类型new AdapterView.OnItemClickListener(){},未定义方法getSupportFragmentManager()

时间:2014-11-04 13:40:05

标签: java android android-layout android-activity

我接受了以下错误:

The method getSupportFragmentManager() is undefined for the type new AdapterView.OnItemClickListener(){}

以下行: FragmentManager fragmentManager = getSupportFragmentManager();

以下是整个活动代码:

import android.app.ActionBar;
import android.app.Activity;
import android.content.res.Configuration;
import android.os.Bundle;
import android.support.v4.app.ActionBarDrawerToggle;
import android.support.v4.app.ActivityCompat;
import android.support.v4.app.Fragment;

import android.support.v4.app.FragmentManager;
import android.support.v4.widget.DrawerLayout;
import android.view.Menu;
import android.view.MenuItem;
import android.support.v7.app.ActionBarActivity;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.Toast;

public class MainActivity extends Activity {

    private String[] mOptionMenu;
    private DrawerLayout mDrawerLayout;
    private RelativeLayout mDrawerRelativeLayout;
    private ListView mDrawerList;
    private ActionBarDrawerToggle mDrawerToggle;

    private CharSequence mTitleSection;
    private CharSequence mTitleApp;

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



        mOptionMenu = new String[] { "Opción 1", "Opción 2", "Opción 3" };
         mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
         mDrawerRelativeLayout = (RelativeLayout)
         findViewById(R.id.left_drawer);
         mDrawerList = (ListView) findViewById(R.id.list_view_drawer);
         mDrawerList.setAdapter(new ArrayAdapter<String>(getActionBar()
         .getThemedContext(), android.R.layout.simple_list_item_1,
         mOptionMenu));

         mDrawerList.setOnItemClickListener(new OnItemClickListener() {
         @Override
         public void onItemClick(AdapterView<?> parent, View view,
         int position, long id) {

         Fragment fragment = null;

         switch (position) {
         case 0:
         fragment = new FirstFragment();
         break;
         case 1:
         fragment = new SecondFragment();
         break;
         case 2:
         fragment = new ThirdFragment();
         break;
         }

         FragmentManager fragmentManager = getSupportFragmentManager();

         fragmentManager.beginTransaction()
         .replace(R.id.content_frame, fragment).commit();

         mDrawerList.setItemChecked(position, true);

         mTitleSection = mOptionMenu[position];
         getActionBar().setTitle(mTitleSection);

         mDrawerLayout.closeDrawer(mDrawerRelativeLayout);
         }
         });
         mDrawerList.setItemChecked(0, true);
         mTitleSection = getTitle();
         mTitleApp = getTitle();

         mDrawerToggle = new ActionBarDrawerToggle(this, mDrawerLayout,
         R.drawable.ic_drawer, R.string.drawer_open,
         R.string.drawer_close) {

         public void onDrawerClosed(View view) {
         getActionBar().setTitle(mTitleSection);
         ActivityCompat.invalidateOptionsMenu(MainActivity.this);
         }

         public void onDrawerOpened(View drawerView) {
         getActionBar().setTitle(mTitleSection);
         ActivityCompat.invalidateOptionsMenu(MainActivity.this);
         }
         };

         mDrawerLayout.setDrawerListener(mDrawerToggle);

         getActionBar().setDisplayHomeAsUpEnabled(true);
         getActionBar().setHomeButtonEnabled(true);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {

        getMenuInflater().inflate(R.menu.main_activity_actions, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {

        if (mDrawerToggle.onOptionsItemSelected(item)) {
            return true;
        }

        switch (item.getItemId()) {
        case R.id.action_settings:
            Toast.makeText(this, "Settings", Toast.LENGTH_SHORT).show();
            ;
            break;
        default:
            return super.onOptionsItemSelected(item);
        }

        return true;
    }

    @Override
    protected void onPostCreate(Bundle savedInstanceState) {
        super.onPostCreate(savedInstanceState);
        mDrawerToggle.syncState();
    }

    @Override
    public void onConfigurationChanged(Configuration newConfig) {
        super.onConfigurationChanged(newConfig);
        mDrawerToggle.onConfigurationChanged(newConfig);
    }
}

首先,该类正在扩展ActionBarActivity,但我在Android 4.42上运行并且首选使用活动。

如果有人可以帮助我,那将会有所帮助。

如果您需要任何澄清,请告诉我。

更新

11-04 06:16:38.879: E/AndroidRuntime(18850): FATAL EXCEPTION: main
11-04 06:16:38.879: E/AndroidRuntime(18850): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.dooba.beta/com.dooba.beta.MainActivity}: android.view.InflateException: Binary XML file line #110: Error inflating class android.widget.TextView
11-04 06:16:38.879: E/AndroidRuntime(18850):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2059)
11-04 06:16:38.879: E/AndroidRuntime(18850):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2084)
11-04 06:16:38.879: E/AndroidRuntime(18850):    at android.app.ActivityThread.access$600(ActivityThread.java:130)
11-04 06:16:38.879: E/AndroidRuntime(18850):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1195)
11-04 06:16:38.879: E/AndroidRuntime(18850):    at android.os.Looper.loop(Looper.java:137)
11-04 06:16:38.879: E/AndroidRuntime(18850):    at android.app.ActivityThread.main(ActivityThread.java:4745)
11-04 06:16:38.879: E/AndroidRuntime(18850):    at java.lang.reflect.Method.invokeNative(Native Method)
11-04 06:16:38.879: E/AndroidRuntime(18850):    at java.lang.reflect.Method.invoke(Method.java:511)
11-04 06:16:38.879: E/AndroidRuntime(18850):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
11-04 06:16:38.879: E/AndroidRuntime(18850):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
11-04 06:16:38.879: E/AndroidRuntime(18850):    at dalvik.system.NativeStart.main(Native Method)
11-04 06:16:38.879: E/AndroidRuntime(18850): Caused by: android.view.InflateException: Binary XML file line #110: Error inflating class android.widget.TextView
11-04 06:16:38.879: E/AndroidRuntime(18850):    at android.view.LayoutInflater.createView(LayoutInflater.java:613)
11-04 06:16:38.879: E/AndroidRuntime(18850):    at com.android.internal.policy.impl.PhoneLayoutInflater.onCreateView(PhoneLayoutInflater.java:56)
11-04 06:16:38.879: E/AndroidRuntime(18850):    at android.view.LayoutInflater.onCreateView(LayoutInflater.java:660)
11-04 06:16:38.879: E/AndroidRuntime(18850):    at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:685)
11-04 06:16:38.879: E/AndroidRuntime(18850):    at android.view.LayoutInflater.rInflate(LayoutInflater.java:746)
11-04 06:16:38.879: E/AndroidRuntime(18850):    at android.view.LayoutInflater.rInflate(LayoutInflater.java:749)
11-04 06:16:38.879: E/AndroidRuntime(18850):    at android.view.LayoutInflater.rInflate(LayoutInflater.java:749)
11-04 06:16:38.879: E/AndroidRuntime(18850):    at android.view.LayoutInflater.inflate(LayoutInflater.java:489)
11-04 06:16:38.879: E/AndroidRuntime(18850):    at android.view.LayoutInflater.inflate(LayoutInflater.java:396)
11-04 06:16:38.879: E/AndroidRuntime(18850):    at android.view.LayoutInflater.inflate(LayoutInflater.java:352)
11-04 06:16:38.879: E/AndroidRuntime(18850):    at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:256)
11-04 06:16:38.879: E/AndroidRuntime(18850):    at android.app.Activity.setContentView(Activity.java:1867)
11-04 06:16:38.879: E/AndroidRuntime(18850):    at com.dooba.beta.MainActivity.onCreate(MainActivity.java:41)
11-04 06:16:38.879: E/AndroidRuntime(18850):    at android.app.Activity.performCreate(Activity.java:5008)
11-04 06:16:38.879: E/AndroidRuntime(18850):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1079)
11-04 06:16:38.879: E/AndroidRuntime(18850):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2023)
11-04 06:16:38.879: E/AndroidRuntime(18850):    ... 11 more
11-04 06:16:38.879: E/AndroidRuntime(18850): Caused by: java.lang.reflect.InvocationTargetException
11-04 06:16:38.879: E/AndroidRuntime(18850):    at java.lang.reflect.Constructor.constructNative(Native Method)
11-04 06:16:38.879: E/AndroidRuntime(18850):    at java.lang.reflect.Constructor.newInstance(Constructor.java:417)
11-04 06:16:38.879: E/AndroidRuntime(18850):    at android.view.LayoutInflater.createView(LayoutInflater.java:587)
11-04 06:16:38.879: E/AndroidRuntime(18850):    ... 27 more
11-04 06:16:38.879: E/AndroidRuntime(18850): Caused by: java.lang.UnsupportedOperationException: Can't convert to color: type=0x2
11-04 06:16:38.879: E/AndroidRuntime(18850):    at android.content.res.TypedArray.getColor(TypedArray.java:326)
11-04 06:16:38.879: E/AndroidRuntime(18850):    at android.widget.TextView.<init>(TextView.java:499)
11-04 06:16:38.879: E/AndroidRuntime(18850):    ... 30 more
11-04 06:18:15.093: D/dalvikvm(18955): GC_CONCURRENT freed 161K, 3% free 8309K/8519K, paused 16ms+2ms, total 43ms

更新2:

11-04 06:16:38.879: E/AndroidRuntime(18850): FATAL EXCEPTION: main
11-04 06:16:38.879: E/AndroidRuntime(18850): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.dooba.beta/com.dooba.beta.MainActivity}: android.view.InflateException: Binary XML file line #110: Error inflating class android.widget.TextView
11-04 06:16:38.879: E/AndroidRuntime(18850):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2059)
11-04 06:16:38.879: E/AndroidRuntime(18850):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2084)
11-04 06:16:38.879: E/AndroidRuntime(18850):    at android.app.ActivityThread.access$600(ActivityThread.java:130)
11-04 06:16:38.879: E/AndroidRuntime(18850):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1195)
11-04 06:16:38.879: E/AndroidRuntime(18850):    at android.os.Looper.loop(Looper.java:137)
11-04 06:16:38.879: E/AndroidRuntime(18850):    at android.app.ActivityThread.main(ActivityThread.java:4745)
11-04 06:16:38.879: E/AndroidRuntime(18850):    at java.lang.reflect.Method.invokeNative(Native Method)
11-04 06:16:38.879: E/AndroidRuntime(18850):    at java.lang.reflect.Method.invoke(Method.java:511)
11-04 06:16:38.879: E/AndroidRuntime(18850):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
11-04 06:16:38.879: E/AndroidRuntime(18850):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
11-04 06:16:38.879: E/AndroidRuntime(18850):    at dalvik.system.NativeStart.main(Native Method)
11-04 06:16:38.879: E/AndroidRuntime(18850): Caused by: android.view.InflateException: Binary XML file line #110: Error inflating class android.widget.TextView
11-04 06:16:38.879: E/AndroidRuntime(18850):    at android.view.LayoutInflater.createView(LayoutInflater.java:613)
11-04 06:16:38.879: E/AndroidRuntime(18850):    at com.android.internal.policy.impl.PhoneLayoutInflater.onCreateView(PhoneLayoutInflater.java:56)
11-04 06:16:38.879: E/AndroidRuntime(18850):    at android.view.LayoutInflater.onCreateView(LayoutInflater.java:660)
11-04 06:16:38.879: E/AndroidRuntime(18850):    at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:685)
11-04 06:16:38.879: E/AndroidRuntime(18850):    at android.view.LayoutInflater.rInflate(LayoutInflater.java:746)
11-04 06:16:38.879: E/AndroidRuntime(18850):    at android.view.LayoutInflater.rInflate(LayoutInflater.java:749)
11-04 06:16:38.879: E/AndroidRuntime(18850):    at android.view.LayoutInflater.rInflate(LayoutInflater.java:749)
11-04 06:16:38.879: E/AndroidRuntime(18850):    at android.view.LayoutInflater.inflate(LayoutInflater.java:489)
11-04 06:16:38.879: E/AndroidRuntime(18850):    at android.view.LayoutInflater.inflate(LayoutInflater.java:396)
11-04 06:16:38.879: E/AndroidRuntime(18850):    at android.view.LayoutInflater.inflate(LayoutInflater.java:352)
11-04 06:16:38.879: E/AndroidRuntime(18850):    at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:256)
11-04 06:16:38.879: E/AndroidRuntime(18850):    at android.app.Activity.setContentView(Activity.java:1867)
11-04 06:16:38.879: E/AndroidRuntime(18850):    at com.dooba.beta.MainActivity.onCreate(MainActivity.java:41)
11-04 06:16:38.879: E/AndroidRuntime(18850):    at android.app.Activity.performCreate(Activity.java:5008)
11-04 06:16:38.879: E/AndroidRuntime(18850):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1079)
11-04 06:16:38.879: E/AndroidRuntime(18850):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2023)
11-04 06:16:38.879: E/AndroidRuntime(18850):    ... 11 more
11-04 06:16:38.879: E/AndroidRuntime(18850): Caused by: java.lang.reflect.InvocationTargetException
11-04 06:16:38.879: E/AndroidRuntime(18850):    at java.lang.reflect.Constructor.constructNative(Native Method)
11-04 06:16:38.879: E/AndroidRuntime(18850):    at java.lang.reflect.Constructor.newInstance(Constructor.java:417)
11-04 06:16:38.879: E/AndroidRuntime(18850):    at android.view.LayoutInflater.createView(LayoutInflater.java:587)
11-04 06:16:38.879: E/AndroidRuntime(18850):    ... 27 more
11-04 06:16:38.879: E/AndroidRuntime(18850): Caused by: java.lang.UnsupportedOperationException: Can't convert to color: type=0x2
11-04 06:16:38.879: E/AndroidRuntime(18850):    at android.content.res.TypedArray.getColor(TypedArray.java:326)
11-04 06:16:38.879: E/AndroidRuntime(18850):    at android.widget.TextView.<init>(TextView.java:499)
11-04 06:16:38.879: E/AndroidRuntime(18850):    ... 30 more
11-04 06:18:15.093: D/dalvikvm(18955): GC_CONCURRENT freed 161K, 3% free 8309K/8519K, paused 16ms+2ms, total 43ms
11-04 06:29:51.550: E/AndroidRuntime(19286): FATAL EXCEPTION: main
11-04 06:29:51.550: E/AndroidRuntime(19286): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.dooba.beta/com.dooba.beta.MainActivity}: java.lang.NullPointerException
11-04 06:29:51.550: E/AndroidRuntime(19286):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2059)
11-04 06:29:51.550: E/AndroidRuntime(19286):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2084)
11-04 06:29:51.550: E/AndroidRuntime(19286):    at android.app.ActivityThread.access$600(ActivityThread.java:130)
11-04 06:29:51.550: E/AndroidRuntime(19286):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1195)
11-04 06:29:51.550: E/AndroidRuntime(19286):    at android.os.Handler.dispatchMessage(Handler.java:99)
11-04 06:29:51.550: E/AndroidRuntime(19286):    at android.os.Looper.loop(Looper.java:137)
11-04 06:29:51.550: E/AndroidRuntime(19286):    at android.app.ActivityThread.main(ActivityThread.java:4745)
11-04 06:29:51.550: E/AndroidRuntime(19286):    at java.lang.reflect.Method.invokeNative(Native Method)
11-04 06:29:51.550: E/AndroidRuntime(19286):    at java.lang.reflect.Method.invoke(Method.java:511)
11-04 06:29:51.550: E/AndroidRuntime(19286):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
11-04 06:29:51.550: E/AndroidRuntime(19286):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
11-04 06:29:51.550: E/AndroidRuntime(19286):    at dalvik.system.NativeStart.main(Native Method)
11-04 06:29:51.550: E/AndroidRuntime(19286): Caused by: java.lang.NullPointerException
11-04 06:29:51.550: E/AndroidRuntime(19286):    at com.dooba.beta.MainActivity.onCreate(MainActivity.java:51)
11-04 06:29:51.550: E/AndroidRuntime(19286):    at android.app.Activity.performCreate(Activity.java:5008)
11-04 06:29:51.550: E/AndroidRuntime(19286):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1079)
11-04 06:29:51.550: E/AndroidRuntime(19286):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2023)
11-04 06:29:51.550: E/AndroidRuntime(19286):    ... 11 more

我相信错误发生在以下几行:

 mDrawerList.setAdapter(new ArrayAdapter<String>(getActionBar()
         .getThemedContext(), android.R.layout.simple_list_item_1,
         mOptionMenu));

1 个答案:

答案 0 :(得分:6)

Activity类没有getSupportFragmentManager()方法。相反,请将您的活动扩展为FragmentActivity