gpt4 book ai didi

java - 我不明白如何使用带有选项卡的操作栏 sherlock

转载 作者:行者123 更新时间:2023-12-01 18:55:54 25 4
gpt4 key购买 nike

我正在尝试向操作栏 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 这是一个 fragment

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

最佳答案

您需要添加

super.onActivityCreated(savedInstanceState) 

之后

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

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

did not call through to super.onActivityCreated()

一些方法,即与 Activity 和 Fragment 的初始化和生命周期有关的方法,需要调用它们重写的父类(super class)方法,因为在链的更高层完成了一些事情, 不是重写的。但请注意,并非所有重写的方法都需要调用 super,Activity 和 Fragment 生命周期方法是一种特殊情况,因为它们非常复杂并且您无法期望它们每次创建应用程序时重写整个 Activity/Fragment 类。

关于java - 我不明白如何使用带有选项卡的操作栏 sherlock,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14005395/

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