0

我正在尝试将选项卡添加到操作栏 sherlock,但它一直在崩溃并且无法启动。我应该在 xml 文件中放入什么,我找不到放入其中的内容

MainActivity.java

import com.actionbarsherlock.app.ActionBar.Tab;
import com.actionbarsherlock.app.ActionBar.TabListener;
import com.actionbarsherlock.app.*;
import com.actionbarsherlock.view.Menu;
import com.actionbarsherlock.view.MenuInflater;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentTransaction;
import android.util.Log;


public class MainActivity extends SherlockFragmentActivity{

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

        final ActionBar actionBar = getSupportActionBar();
        actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);

        ActionBar.Tab tabData = actionBar.newTab();
        tabData.setText("Match Data");
        tabData.setTabListener(new MyTabListener());
        actionBar.addTab(tabData);

      //  tabRed.setTabListener(new TabListener<TabFragment>(this, "tab1", TabFragment.class));
       /* Tab tabComp = actionBar.newTab();
        tabComp.setText("New Competition");
        tabComp.setTabListener(new TabListener<ScoreRedActivity>(this, "B", ScoreRedActivity.class));
        actionBar.addTab(tabComp);*/
       // tabBlue.setTabListener(new TabListener<TabFragment>(this, tabBlue, TabFragment.class));





    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu){
        MenuInflater inflater = getSupportMenuInflater();
        inflater.inflate(R.menu.activity_main, menu);
        return true;
    }

    private class MyTabListener implements ActionBar.TabListener{
        @Override
        public void onTabSelected(Tab tab, FragmentTransaction ft){
            if(tab.getPosition() == 0)
            {
                ScoreRedActivity frag = new ScoreRedActivity();
                ft.replace(R.id.textView1, frag);

            }
        }

        @Override
        public void onTabUnselected(Tab tab, FragmentTransaction ft){

        }

        @Override
        public void onTabReselected(Tab tab, FragmentTransaction ft){
        }

    }



}

我的 ScoreRedActivity.java 这是一个片段

import com.actionbarsherlock.app.SherlockFragment;

import android.os.Bundle;
import android.app.Activity;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;

public class ScoreRedActivity extends SherlockFragment {

    EditText t;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        return inflater.inflate(R.layout.activity_score_red, container, false);
    }

    @Override
    public void onActivityCreated(Bundle savedInstanceState){
        t = (EditText) getActivity().findViewById(R.id.weightedRed);
    }

}

activity_score_red.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".ScoreRedActivity" >

    <TextView 
        android:layout_height="wrap_content"
        android:layout_width="wrap_content" 
        android:text="You are presently in Fragment A" 
        android:id="@+id/textView1"/>

    <Button 
        android:layout_height="wrap_content" 
        android:layout_width="wrap_content" 
        android:text="Click me for toast"
        android:id="@+id/button1"/>


</LinearLayout>

这是日志猫

12-22 13:21:38.076: E/AndroidRuntime(394): FATAL EXCEPTION: main
12-22 13:21:38.076: E/AndroidRuntime(394): java.lang.RuntimeException: Unable to start activity ComponentInfo{org.say.upscorer/org.say.upscorer.MainActivity}: android.support.v4.app.SuperNotCalledException: Fragment ScoreRedActivity{45f8b1c8 #0 id=0x1020002} did not call through to super.onActivityCreated()
12-22 13:21:38.076: E/AndroidRuntime(394):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663)
12-22 13:21:38.076: E/AndroidRuntime(394):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
12-22 13:21:38.076: E/AndroidRuntime(394):  at android.app.ActivityThread.access$2300(ActivityThread.java:125)
12-22 13:21:38.076: E/AndroidRuntime(394):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
12-22 13:21:38.076: E/AndroidRuntime(394):  at android.os.Handler.dispatchMessage(Handler.java:99)
12-22 13:21:38.076: E/AndroidRuntime(394):  at android.os.Looper.loop(Looper.java:123)
12-22 13:21:38.076: E/AndroidRuntime(394):  at android.app.ActivityThread.main(ActivityThread.java:4627)
12-22 13:21:38.076: E/AndroidRuntime(394):  at java.lang.reflect.Method.invokeNative(Native Method)
12-22 13:21:38.076: E/AndroidRuntime(394):  at java.lang.reflect.Method.invoke(Method.java:521)
12-22 13:21:38.076: E/AndroidRuntime(394):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
12-22 13:21:38.076: E/AndroidRuntime(394):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
12-22 13:21:38.076: E/AndroidRuntime(394):  at dalvik.system.NativeStart.main(Native Method)
12-22 13:21:38.076: E/AndroidRuntime(394): Caused by: android.support.v4.app.SuperNotCalledException: Fragment ScoreRedActivity{45f8b1c8 #0 id=0x1020002} did not call through to super.onActivityCreated()
12-22 13:21:38.076: E/AndroidRuntime(394):  at android.support.v4.app.Fragment.performActivityCreated(Fragment.java:1470)
12-22 13:21:38.076: E/AndroidRuntime(394):  at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:931)
12-22 13:21:38.076: E/AndroidRuntime(394):  at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1088)
12-22 13:21:38.076: E/AndroidRuntime(394):  at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:682)
12-22 13:21:38.076: E/AndroidRuntime(394):  at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1444)
12-22 13:21:38.076: E/AndroidRuntime(394):  at android.support.v4.app.FragmentActivity.onStart(FragmentActivity.java:551)
12-22 13:21:38.076: E/AndroidRuntime(394):  at android.app.Instrumentation.callActivityOnStart(Instrumentation.java:1129)
12-22 13:21:38.076: E/AndroidRuntime(394):  at android.app.Activity.performStart(Activity.java:3781)
12-22 13:21:38.076: E/AndroidRuntime(394):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2636)
12-22 13:21:38.076: E/AndroidRuntime(394):  ... 11 more
4

1 回答 1

4

您需要添加

super.onActivityCreated(savedInstanceState) 

t = (EditText) getActivity().findViewById(R.id.weightedRed); 

这是因为,正如您的 LogCat 所说

did not call through to super.onActivityCreated()

一些方法,即那些与初始化和活动和片段的生命周期有关的方法,需要调用它们覆盖的超类方法,因为有些事情在没有覆盖的链上完成。但是请注意,并非所有被覆盖的方法都需要调用super,Activity 和 Fragment 生命周期方法是一种特殊情况,因为它们非常复杂,而且您不能期望每次创建应用程序时都重写整个 Activity/Fragment 类。

于 2012-12-22T18:34:58.690 回答