1
我rececing以下錯誤:方法getSupportFragmentManager()是未定義的類型新AdapterView.OnItemClickListener(){}
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));
感謝您的回覆。它確實已經清除了問題,但在運行應用程序時沒有受到嚴格的限制,我收到了我的初始文章更新部分中顯示的錯誤。 – John 2014-11-04 14:21:35
在您的其中一個XML中,您爲其中一個TextView設置了無效顏色。 – 2014-11-04 14:23:03
感謝您的及時回覆。它向前邁進了一步,但它認爲將其改爲碎片引發了另一個錯誤。我已將它列入我最初發布的更新2部分。 – John 2014-11-04 14:33:19