gpt4 book ai didi

java - 方法 getSupportFragmentManager() 未定义类型 new AdapterView.OnItemClickListener(){}

转载 作者:行者123 更新时间:2023-11-29 07:00:40 25 4
gpt4 key购买 nike

我收到以下错误:

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

对于以下行:FragmentManager fragmentManager = getSupportFragmentManager();

下面是整个 Activity 代码:

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

起初类(class)扩展了 ActionBarActivity,但我在 android 4.42 上运行并且更喜欢使用 Activity 。

如果有人能帮助我,那会很有帮助。

如果您需要任何说明,请告诉我。

更新:

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

最佳答案

Activity 类没有 getSupportFragmentManager() 方法。而是让您的 Activity 扩展 FragmentActivity 确实有。

关于java - 方法 getSupportFragmentManager() 未定义类型 new AdapterView.OnItemClickListener(){},我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26736562/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com