Ich versuche, FragmentTabPager Activity zu implementieren, aber es wird überhaupt nicht ausgeführt. Sobald ich die Aktivität anrufe, stoppt die App sofort. Ich habe versucht, mehrere Quellen von Code/Tutorial einschließlich developers.android, aber das gleiche Problem passiert mit allen von ihnen. Und von dem, was ich verstehe, hat niemand das gleiche Problem (zumindest haben sie nicht darüber geschrieben). Das führt mich zu der Annahme, dass ich einen dummen Fehler mache. Ich habe über 2 Tage damit verbracht. Heres der Code für meine fragmentTabPager Aktivität:FragmentTabPager überhaupt nicht funktioniert
package com.my.pack;
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.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TabHost;
import android.widget.TabWidget;
import java.util.ArrayList;
public class FragmentTabsPager extends FragmentActivity {
TabHost mTabHost;
ViewPager mViewPager;
TabsAdapter mTabsAdapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.tabs_viewpager_layout);
mTabHost = (TabHost) findViewById(android.R.id.tabhost);
mTabHost.setup();
mViewPager = (ViewPager) findViewById(R.id.pager);
// Create our tab adapter
mTabsAdapter = new TabsAdapter(this, mTabHost, mViewPager);
// add our tabs to the adapter
mTabsAdapter.addTab(mTabHost.newTabSpec("forex").setIndicator("Forex"),
Forex.class, null);
mTabsAdapter.addTab(mTabHost.newTabSpec("stocks")
.setIndicator("Stocks"), Stocks.class, null);
mTabsAdapter.addTab(
mTabHost.newTabSpec("commodities").setIndicator("Commodities"),
Commodities.class, null);
/*
* mTabsAdapter.addTab(mTabHost.newTabSpec("throttle").setIndicator(
* "Throttle"), LoaderThrottleSupport.ThrottledLoaderListFragment.class,
* null);
*/
if (savedInstanceState != null) {
// restore the last selected tab if we can
mTabHost.setCurrentTabByTag(savedInstanceState.getString("tab"));
}
}
@Override
protected void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
outState.putString("tab", mTabHost.getCurrentTabTag());
}
public static class TabsAdapter extends FragmentPagerAdapter implements
TabHost.OnTabChangeListener, ViewPager.OnPageChangeListener {
private final Context mContext;
private final TabHost mTabHost;
private final ViewPager mViewPager;
private final ArrayList mTabs = new ArrayList();
static final class TabInfo {
private final String tag;
private final Class clss;
private final Bundle args;
TabInfo(String _tag, Class _class, Bundle _args) {
tag = _tag;
clss = _class;
args = _args;
}
}
static class DummyTabFactory implements TabHost.TabContentFactory {
private final Context mContext;
public DummyTabFactory(Context context) {
mContext = context;
}
public View createTabContent(String tag) {
View v = new View(mContext);
v.setMinimumWidth(0);
v.setMinimumHeight(0);
return v;
}
}
public TabsAdapter(FragmentActivity activity, TabHost tabHost,
ViewPager pager) {
super(activity.getSupportFragmentManager());
mContext = activity;
mTabHost = tabHost;
mViewPager = pager;
mTabHost.setOnTabChangedListener(this);
mViewPager.setAdapter(this);
mViewPager.setOnPageChangeListener(this);
}
public void addTab(TabHost.TabSpec tabSpec, Class clss, Bundle args) {
tabSpec.setContent(new DummyTabFactory(mContext));
String tag = tabSpec.getTag();
TabInfo info = new TabInfo(tag, clss, args);
mTabs.add(info);
mTabHost.addTab(tabSpec);
notifyDataSetChanged();
}
@Override
public int getCount() {
return mTabs.size();
}
@Override
public Fragment getItem(int position) {
TabInfo info = (TabInfo) mTabs.get(position);
// Create a new fragment if necessary.
return Fragment.instantiate(mContext, info.clss.getName(),
info.args);
}
public void onTabChanged(String tabId) {
// called when the user clicks on a tab.
int position = mTabHost.getCurrentTab();
mViewPager.setCurrentItem(position);
}
public void onPageScrolled(int position, float positionOffset,
int positionOffsetPixels) {
}
public void onPageSelected(int position) {
TabWidget widget = mTabHost.getTabWidget();
int oldFocusability = widget.getDescendantFocusability();
widget.setDescendantFocusability(ViewGroup.FOCUS_BLOCK_DESCENDANTS);
mTabHost.setCurrentTab(position);
widget.setDescendantFocusability(oldFocusability);
}
public void onPageScrollStateChanged(int state) {
}
}
}
Und mein Manifest aussieht:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.my.pack"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk android:minSdkVersion="8" />
<application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name" >
<activity
android:name=".Main"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".FragmentTabsPager"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.FRAGMENTTABSPAGER" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
<activity
android:name=".Stocks"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.STOCKS" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
<activity
android:name=".Commodities"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.COMMODITIES" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
<activity
android:name=".Forex"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.FOREX" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
</application>
</manifest>
Die xml von tabs_viewpager_layout Datei:
<?xml version="1.0" encoding="utf-8"?>
<TabHost xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@android:id/tabhost"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TabWidget
android:id="@android:id/tabs"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="0"
android:orientation="horizontal" />
<FrameLayout
android:id="@android:id/tabcontent"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_weight="0" />
<android.support.v4.view.ViewPager
android:id="@+id/pager"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1" />
</LinearLayout>
Und mein Haupt, aus dem Ich möchte meine fragmentTab-Aktivität aufrufen:
package com.my.pack;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class Main extends Activity{
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button b1 = (Button) findViewById(R.id.button1);
b1.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
Intent myintent = new Intent(Main.this, FragmentTabsPager.class);
startActivity(myintent);
}
});
}
}
Ich weiß meine 3 Forex, Lager, etc. Pager-Aktivitäten laufen ohne Probleme. Ich überprüfte, indem ich die Absicht in der Hauptrolle zu Forex.class änderte und alle 3 funktionieren gut. Ich dachte auch, dass die Aktivität eine LAUNCHER-Aktivität sein musste und versuchte das nur zu scheitern. Danke im Voraus. Jede Hilfe wäre sehr geschätzt.
Mein logcat ist ..
04-08 19:05:24.839: D/AndroidRuntime(1332): Shutting down VM
04-08 19:05:24.839: W/dalvikvm(1332): threadid=1: thread exiting with uncaught exception (group=0x4001d800)
04-08 19:05:24.869: E/AndroidRuntime(1332): FATAL EXCEPTION: main
04-08 19:05:24.869: E/AndroidRuntime(1332): java.lang.ClassCastException: com.my.pack.Forex
04-08 19:05:24.869: E/AndroidRuntime(1332): at android.support.v4.app.Fragment.instantiate(Fragment.java:384)
04-08 19:05:24.869: E/AndroidRuntime(1332): at com.my.pack.FragmentTabsPager$TabsAdapter.getItem(FragmentTabsPager.java:124)
04-08 19:05:24.869: E/AndroidRuntime(1332): at android.support.v4.app.FragmentPagerAdapter.instantiateItem(FragmentPagerAdapter.java:95)
04-08 19:05:24.869: E/AndroidRuntime(1332): at android.support.v4.view.ViewPager.addNewItem(ViewPager.java:649)
04-08 19:05:24.869: E/AndroidRuntime(1332): at android.support.v4.view.ViewPager.populate(ViewPager.java:783)
04-08 19:05:24.869: E/AndroidRuntime(1332): at android.support.v4.view.ViewPager.onMeasure(ViewPager.java:1016)
04-08 19:05:24.869: E/AndroidRuntime(1332): at android.view.View.measure(View.java:8171)
04-08 19:05:24.869: E/AndroidRuntime(1332): at android.widget.LinearLayout.measureVertical(LinearLayout.java:526)
04-08 19:05:24.869: E/AndroidRuntime(1332): at android.widget.LinearLayout.onMeasure(LinearLayout.java:304)
04-08 19:05:24.869: E/AndroidRuntime(1332): at android.view.View.measure(View.java:8171)
04-08 19:05:24.869: E/AndroidRuntime(1332): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:3132)
04-08 19:05:24.869: E/AndroidRuntime(1332): at android.widget.FrameLayout.onMeasure(FrameLayout.java:245)
04-08 19:05:24.869: E/AndroidRuntime(1332): at android.view.View.measure(View.java:8171)
04-08 19:05:24.869: E/AndroidRuntime(1332): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:3132)
04-08 19:05:24.869: E/AndroidRuntime(1332): at android.widget.FrameLayout.onMeasure(FrameLayout.java:245)
04-08 19:05:24.869: E/AndroidRuntime(1332): at android.view.View.measure(View.java:8171)
04-08 19:05:24.869: E/AndroidRuntime(1332): at android.widget.LinearLayout.measureVertical(LinearLayout.java:526)
04-08 19:05:24.869: E/AndroidRuntime(1332): at android.widget.LinearLayout.onMeasure(LinearLayout.java:304)
04-08 19:05:24.869: E/AndroidRuntime(1332): at android.view.View.measure(View.java:8171)
04-08 19:05:24.869: E/AndroidRuntime(1332): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:3132)
04-08 19:05:24.869: E/AndroidRuntime(1332): at android.widget.FrameLayout.onMeasure(FrameLayout.java:245)
04-08 19:05:24.869: E/AndroidRuntime(1332): at android.view.View.measure(View.java:8171)
04-08 19:05:24.869: E/AndroidRuntime(1332): at android.view.ViewRoot.performTraversals(ViewRoot.java:801)
04-08 19:05:24.869: E/AndroidRuntime(1332): at android.view.ViewRoot.handleMessage(ViewRoot.java:1727)
04-08 19:05:24.869: E/AndroidRuntime(1332): at android.os.Handler.dispatchMessage(Handler.java:99)
04-08 19:05:24.869: E/AndroidRuntime(1332): at android.os.Looper.loop(Looper.java:123)
04-08 19:05:24.869: E/AndroidRuntime(1332): at android.app.ActivityThread.main(ActivityThread.java:4627)
04-08 19:05:24.869: E/AndroidRuntime(1332): at java.lang.reflect.Method.invokeNative(Native Method)
04-08 19:05:24.869: E/AndroidRuntime(1332): at java.lang.reflect.Method.invoke(Method.java:521)
04-08 19:05:24.869: E/AndroidRuntime(1332): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
04-08 19:05:24.869: E/AndroidRuntime(1332): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
04-08 19:05:24.869: E/AndroidRuntime(1332): at dalvik.system.NativeStart.main(Native Method)
04-08 19:05:27.898: I/Process(1332): Sending signal. PID: 1332 SIG: 9
Wenn Sie * sagen * - meinst du du eine „Force Close“ get Dialog „Sobald ich die Aktivität der App rufen Sie sofort stoppt.“? In diesem Fall poste bitte den Stacktrace vom Logcat. Vielen Dank. –
Zunächst einmal, danke für Ihre Antwort und ja, ich bekomme eine Kraft in der Nähe. Mein Logcat ist oben eingefügt (alles). Und nochmals vielen Dank, ich weiß es wirklich zu schätzen, dass du dich damit befasst hast. Vielen Dank. – Ryan
Also, irgendwelche Gedanken .... @alextsc – Ryan