我的应用程序总是在该行崩溃:listview1.setAdapter(new ArrayAdapter(MainActivity.getAppContext(), android.R.layout.simple_list_item_1, listContents));
我的代码:
package de.nathan.android.droidschool;
import android.content.Context;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;
import java.util.ArrayList;
import java.util.List;
import java.util.Locale;
public class MainActivity extends FragmentActivity
{
private static Context context;
/**
* The {@link android.support.v4.view.PagerAdapter} that will provide
* fragments for each of the sections. We use a
* {@link android.support.v4.app.FragmentPagerAdapter} derivative, which
* will keep every loaded fragment in memory. If this becomes too memory
* intensive, it may be best to switch to a
* {@link android.support.v4.app.FragmentStatePagerAdapter}.
*/
SectionsPagerAdapter mSectionsPagerAdapter;
/**
* The {@link ViewPager} that will host the section contents.
*/
ViewPager mViewPager;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Create the adapter that will return a fragment for each of the three
// primary sections of the app.
mSectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager());
// Set up the ViewPager with the sections adapter.
mViewPager = (ViewPager) findViewById(R.id.pager);
mViewPager.setAdapter(mSectionsPagerAdapter);
MainActivity.context = getApplicationContext();
}
public static Context getAppContext()
{
return MainActivity.context;
}
@Override
public boolean onCreateOptionsMenu(Menu menu)
{
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
/**
* A {@link FragmentPagerAdapter} that returns a fragment corresponding to
* one of the sections/tabs/pages.
*/
public class SectionsPagerAdapter extends FragmentPagerAdapter
{
public SectionsPagerAdapter(FragmentManager fm)
{
super(fm);
}
@Override
public Fragment getItem(int position)
{
// getItem is called to instantiate the fragment for the given page.
// Return a DummySectionFragment (defined as a static inner class
// below) with the page number as its lone argument.
Fragment fragment;
switch(position)
{
case 0:
fragment = new fragmentTab1();
break;
case 1:
fragment = new fragmentTab2();
break;
case 2:
fragment = new fragmentTab3();
break;
default:
throw new IllegalArgumentException("Invalid section number");
}
Bundle args = new Bundle();
args.putInt(fragmentTab1.ARG_SECTION_NUMBER, position + 1);
fragment.setArguments(args);
return fragment;
}
@Override
public int getCount()
{
// Show 3 total pages.
return 3;
}
@Override
public CharSequence getPageTitle(int position)
{
Locale l = Locale.getDefault();
switch (position) {
case 0:
return getString(R.string.title_section1).toUpperCase(l);
case 1:
return getString(R.string.title_section2).toUpperCase(l);
case 2:
return getString(R.string.title_section3).toUpperCase(l);
}
return null;
}
}
/**
* A dummy fragment representing a section of the app, but that simply
* displays dummy text.
*/
public class fragmentTab1 extends Fragment
{
/**
* The fragment argument representing the section number for this
* fragment.
*/
public static final String ARG_SECTION_NUMBER = "section_number";
public void onStart()
{
List listContents = new ArrayList();
listContents.add("one");
listContents.add("two");
listContents.add("three");
listContents.add("four");
listContents.add("five");
listContents.add("six");
listContents.add("seven");
listContents.add("eight");
listContents.add("nine");
listContents.add("ten");
listContents.add("eleven");
ListView listview1;
listview1 = (ListView) findViewById(R.id.listView);
listview1.setAdapter(new ArrayAdapter(MainActivity.getAppContext(), android.R.layout.simple_list_item_1, listContents));
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_layout_tab1, container, false);
TextView dummyTextView = (TextView) rootView.findViewById(R.id.section_label);
dummyTextView.setText(Integer.toString(getArguments().getInt(ARG_SECTION_NUMBER)));
return rootView;
}
}
public class fragmentTab2 extends Fragment
{
/**
* The fragment argument representing the section number for this
* fragment.
*/
public static final String ARG_SECTION_NUMBER = "section_number";
public fragmentTab2() {
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_layout_tab2, container, false);
TextView dummyTextView = (TextView) rootView.findViewById(R.id.section_label);
dummyTextView.setText(Integer.toString(getArguments().getInt(ARG_SECTION_NUMBER)));
return rootView;
}
}
public class fragmentTab3 extends Fragment
{
/**
* The fragment argument representing the section number for this
* fragment.
*/
public static final String ARG_SECTION_NUMBER = "section_number";
public fragmentTab3()
{
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState)
{
View rootView = inflater.inflate(R.layout.fragment_layout_tab3, container, false);
TextView dummyTextView = (TextView) rootView.findViewById(R.id.section_label);
dummyTextView.setText(Integer.toString(getArguments().getInt(ARG_SECTION_NUMBER)));
return rootView;
}
}
}
这里是logcat:
08-12 23:51:56.885 6008-6008/de.nathan.android.droidschool I/System.out: debugger has settled (1347)
08-12 23:51:56.975 6088-6092/? D/dalvikvm: GC_CONCURRENT freed 398K, 6% free 7821K/8280K, paused 7ms+2ms, total 62ms
08-12 23:51:56.975 6088-6088/? D/dalvikvm: WAIT_FOR_CONCURRENT_GC blocked 22ms
08-12 23:51:56.995 25995-26080/? D/dalvikvm: GC_FOR_ALLOC freed 71K, 7% free 8744K/9316K, paused 73ms, total 74ms
08-12 23:51:57.005 25995-6084/? I/GCoreUlr: Ensuring that reporting is stopped because of reasons: {account#-13#=[InactiveReason{mVersionCode=0, mIdentifier=6, mName='ReportingNotSelected'}]}
08-12 23:51:57.005 25995-6084/? D/GCoreFlp: Unknown pending intent to remove.
08-12 23:51:57.015 25995-6084/? I/GCoreUlr: Unbound from all location providers
08-12 23:51:57.085 6088-6092/? D/dalvikvm: GC_CONCURRENT freed 497K, 7% free 7838K/8396K, paused 2ms+4ms, total 38ms
08-12 23:51:57.085 6088-6103/? D/dalvikvm: WAIT_FOR_CONCURRENT_GC blocked 11ms
08-12 23:51:57.085 6088-6088/? D/dalvikvm: WAIT_FOR_CONCURRENT_GC blocked 16ms
08-12 23:51:57.095 25995-26080/? D/dalvikvm: GC_FOR_ALLOC freed 38K, 7% free 8738K/9316K, paused 47ms, total 47ms
08-12 23:51:57.155 438-900/? W/ActivityManager: Permission denied: checkComponentPermission() owningUid=10055
08-12 23:51:57.155 438-900/? W/ActivityManager: Permission denied: checkComponentPermission() owningUid=10055
08-12 23:51:57.195 438-6553/? W/ActivityManager: Permission denied: checkComponentPermission() owningUid=10055
08-12 23:51:57.195 438-6553/? W/ActivityManager: Permission denied: checkComponentPermission() owningUid=10055
08-12 23:51:57.225 438-6553/? W/ActivityManager: Permission denied: checkComponentPermission() owningUid=10055
08-12 23:51:57.225 438-6553/? W/ActivityManager: Permission denied: checkComponentPermission() owningUid=10055
08-12 23:51:57.235 6088-6092/? D/dalvikvm: GC_CONCURRENT freed 440K, 6% free 7911K/8412K, paused 4ms+4ms, total 52ms
08-12 23:51:57.235 6088-6088/? D/dalvikvm: WAIT_FOR_CONCURRENT_GC blocked 36ms
08-12 23:51:57.245 438-831/? W/ActivityManager: Permission denied: checkComponentPermission() owningUid=10055
08-12 23:51:57.245 438-831/? W/ActivityManager: Permission denied: checkComponentPermission() owningUid=10055
08-12 23:51:57.275 6008-6008/de.nathan.android.droidschool D/AndroidRuntime: Shutting down VM
08-12 23:51:57.275 6008-6008/de.nathan.android.droidschool W/dalvikvm: threadid=1: thread exiting with uncaught exception (group=0x41760700)
08-12 23:51:57.295 6088-6088/? I/d: Channel:
08-12 23:51:57.305 6063-6067/? D/dalvikvm: GC_CONCURRENT freed 379K, 6% free 7749K/8192K, paused 16ms+4ms, total 41ms
08-12 23:51:57.305 6008-6008/de.nathan.android.droidschool E/AndroidRuntime: FATAL EXCEPTION: main
android.support.v4.app.SuperNotCalledException: Fragment fragmentTab1{41ef7c58 #0 id=0x7f080000 android:switcher:2131230720:0} did not call through to super.onStart()
at android.support.v4.app.Fragment.performStart(Fragment.java:1486)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:941)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1088)
at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:682)
at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1444)
at android.support.v4.app.FragmentManagerImpl.executePendingTransactions(FragmentManager.java:461)
at android.support.v4.app.FragmentPagerAdapter.finishUpdate(FragmentPagerAdapter.java:141)
at android.support.v4.view.ViewPager.populate(ViewPager.java:1064)
at android.support.v4.view.ViewPager.populate(ViewPager.java:911)
at android.support.v4.view.ViewPager.onMeasure(ViewPager.java:1432)
at android.view.View.measure(View.java:15848)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5008)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
at android.view.View.measure(View.java:15848)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5008)
at com.android.internal.widget.ActionBarOverlayLayout.onMeasure(ActionBarOverlayLayout.java:302)
at android.view.View.measure(View.java:15848)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5008)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2189)
at android.view.View.measure(View.java:15848)
at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:1905)
at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1104)
at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1284)
at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1004)
at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:5481)
at android.view.Choreographer$CallbackRecord.run(Choreographer.java:749)
at android.view.Choreographer.doCallbacks(Choreographer.java:562)
at android.view.Choreographer.doFrame(Choreographer.java:532)
at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:735)
at android.os.Handler.handleCallback(Handler.java:730)
at android.os.Handler.dispatchMessage(Handler.java:92)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:5103)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:525)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
at dalvik.system.NativeStart.main(Native Method)
08-12 23:51:57.335 6088-6092/? D/dalvikvm: GC_CONCURRENT freed 514K, 7% free 7909K/8484K, paused 3ms+3ms, total 55ms
08-12 23:51:57.335 6088-6088/? D/dalvikvm: WAIT_FOR_CONCURRENT_GC blocked 34ms
08-12 23:51:57.335 438-901/? W/ActivityManager: Force finishing activity de.nathan.android.droidschool/.MainActivity
08-12 23:51:57.355 6088-6088/? I/LicenseChecker: Binding to licensing service.
08-12 23:51:57.635 438-901/? I/WindowManager: Screenshot max retries 4 of Token{42a105d8 ActivityRecord{422e4c88 u0 de.nathan.android.droidschool/.MainActivity}} appWin=Window{41f107b8 u0 Starting de.nathan.android.droidschool} drawState=4
08-12 23:51:57.635 438-901/? W/WindowManager: Screenshot failure taking screenshot for (800x1280) to layer 21010
08-12 23:51:57.645 438-817/? W/ActivityManager: Permission Denial: Accessing service ComponentInfo{com.android.vending/com.google.android.finsky.services.LicensingService} from pid=6088, uid=10120 requires com.android.vending.CHECK_LICENSE
08-12 23:51:57.655 6088-6088/? I/AppUpgradeReceiver: Notified of app replace. package:de.nathan.android.droidschool#Intent;action=android.intent.action.PACKAGE_REPLACED;launchFlags=0x8000010;component=com.shazam.android/com.shazam.upgrade.AppUpgradeReceiver;B.android.intent.extra.REPLACING=true;i.android.intent.extra.UID=10018;i.android.intent.extra.user_handle=0;end
08-12 23:51:57.655 438-664/? I/ActivityManager: No longer want com.android.keychain (pid 5914): empty #17
08-12 23:51:57.745 6088-6092/? D/dalvikvm: GC_CONCURRENT freed 253K, 5% free 8064K/8484K, paused 3ms+2ms, total 30ms
08-12 23:51:58.055 438-3894/? D/dalvikvm: GC_EXPLICIT freed 2659K, 24% free 19511K/25460K, paused 4ms+19ms, total 193ms
08-12 23:51:58.135 438-454/? W/ActivityManager: Activity pause timeout for ActivityRecord{422e4c88 u0 de.nathan.android.droidschool/.MainActivity}
08-12 23:51:58.195 438-665/? I/ActivityManager: No longer want jp.co.omronsoft.iwnnime.ml (pid 5931): empty #17
08-12 23:51:58.295 706-706/? I/AndroidInput: sensor listener setup
08-12 23:51:58.305 706-884/? I/AndroidGraphics: framebuffer: (5, 6, 5, 0)
08-12 23:51:58.305 706-884/? I/AndroidGraphics: depthbuffer: (16)
08-12 23:51:58.305 706-884/? I/AndroidGraphics: stencilbuffer: (0)
08-12 23:51:58.305 706-884/? I/AndroidGraphics: samples: (0)
08-12 23:51:58.305 706-884/? I/AndroidGraphics: coverage sampling: (false)
08-12 23:51:58.315 706-884/? I/AndroidGraphicsLW: Managed meshes/app: { }
08-12 23:51:58.315 706-884/? I/AndroidGraphicsLW: Managed textures/app: { }
08-12 23:51:58.315 706-884/? I/AndroidGraphicsLW: Managed shaders/app: { }
08-12 23:51:58.315 706-884/? I/AndroidGraphicsLW: Managed buffers/app: { }
08-12 23:51:58.315 706-884/? V/WorldSize: Landscape: false
08-12 23:52:00.505 16244-16244/? D/Wetter.com: [23:52:00.508, main]: widget on receive: com.wetter.androidclient.widgets.WetterWidgetProvider4x2
08-12 23:52:00.515 16244-16244/? D/Wetter.com: [23:52:00.514, main]: WetterWidgetUpdateService - onStartCommand
08-12 23:52:00.515 16244-16244/? D/Wetter.com: [23:52:00.517, main]: widgetId: 33 - onStartCommand
08-12 23:52:00.515 16244-16244/? D/Wetter.com: [23:52:00.518, main]: widgetId: 33 - onStartCommand - update datetime only
08-12 23:52:00.625 16244-16244/? D/Wetter.com: [23:52:00.634, main]: widgetId: 33 - set update timer for CLOCK to 12.08.2013 23:53:00, pi: 2000033
08-12 23:52:02.235 15389-15393/? D/dalvikvm: GC_CONCURRENT freed 339K, 7% free 8566K/9168K, paused 6ms+4ms, total 59ms
08-12 23:52:08.205 438-454/? W/ActivityManager: Activity destroy timeout for ActivityRecord{422e4c88 u0 de.nathan.android.droidschool/.MainActivity}
当我切换到横向时的 logcat:
08-13 00:37:52.745 13523-13523/de.nathan.android.droidschool D/dalvikvm: newInstance failed: no <init>()
08-13 00:37:52.755 13523-13523/de.nathan.android.droidschool D/AndroidRuntime: Shutting down VM
08-13 00:37:52.755 13523-13523/de.nathan.android.droidschool W/dalvikvm: threadid=1: thread exiting with uncaught exception (group=0x41760700)
08-13 00:37:52.815 13523-13523/de.nathan.android.droidschool E/AndroidRuntime: FATAL EXCEPTION: main
java.lang.RuntimeException: Unable to start activity ComponentInfo{de.nathan.android.droidschool/de.nathan.android.droidschool.MainActivity}: android.support.v4.app.Fragment$InstantiationException: Unable to instantiate fragment de.nathan.android.droidschool.MainActivity$fragmentTab1: make sure class name exists, is public, and has an empty constructor that is public
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2211)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2261)
at android.app.ActivityThread.handleRelaunchActivity(ActivityThread.java:3740)
at android.app.ActivityThread.access$700(ActivityThread.java:141)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1262)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:5103)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:525)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
at dalvik.system.NativeStart.main(Native Method)
Caused by: android.support.v4.app.Fragment$InstantiationException: Unable to instantiate fragment de.nathan.android.droidschool.MainActivity$fragmentTab1: make sure class name exists, is public, and has an empty constructor that is public
at android.support.v4.app.Fragment.instantiate(Fragment.java:405)
at android.support.v4.app.FragmentState.instantiate(Fragment.java:97)
at android.support.v4.app.FragmentManagerImpl.restoreAllState(FragmentManager.java:1767)
at android.support.v4.app.FragmentActivity.onCreate(FragmentActivity.java:208)
at de.nathan.android.droidschool.MainActivity.onCreate(MainActivity.java:43)
at android.app.Activity.performCreate(Activity.java:5133)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2175)
... 12 more
Caused by: java.lang.InstantiationException: can't instantiate class de.nathan.android.droidschool.MainActivity$fragmentTab1; no empty constructor
at java.lang.Class.newInstanceImpl(Native Method)
at java.lang.Class.newInstance(Class.java:1130)
at android.support.v4.app.Fragment.instantiate(Fragment.java:394)
... 19 more