设置新文本时android中的按钮复制

设置新文本时android中的按钮复制,android,button,actionbarsherlock,Android,Button,Actionbarsherlock,我正在使用android支持库,以便能够使用Froyo的片段和Sherlock扩展来显示ActionBar 我有一个片段,其中显示了三个文本视图和一个按钮,我希望能够根据意图的内容以友好的方式更改按钮文本。问题是,当我调用button.setText(字符串文本)时,会出现第二个按钮。但是,我在单击每个按钮时调用了button.getId(),并且Id是相同的。我不知道为什么会发生这种情况,所以我非常感谢你的帮助 该应用程序运行在三星Galaxy S上,搭载Android 2.3.3。我无法上传

我正在使用android支持库,以便能够使用Froyo的片段和Sherlock扩展来显示ActionBar

我有一个片段,其中显示了三个文本视图和一个按钮,我希望能够根据意图的内容以友好的方式更改按钮文本。问题是,当我调用button.setText(字符串文本)时,会出现第二个按钮。但是,我在单击每个按钮时调用了button.getId(),并且Id是相同的。我不知道为什么会发生这种情况,所以我非常感谢你的帮助

该应用程序运行在三星Galaxy S上,搭载Android 2.3.3。我无法上传屏幕截图,因为我还没有足够的声誉:(

代码如下:

片段

import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;

import com.actionbarsherlock.app.SherlockFragment;
import com.parse.ParseUser;
import com.tiempoderodar.egdf.EGDF_App;
import com.tiempoderodar.egdf.R;
import com.tiempoderodar.egdf.security.Constants;

/**
 * @author Daniel Leal López
 *
 */
public class ChapterDetailsFragment extends SherlockFragment{

    private Button b;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Log.i("ChapterDetailsFragment", "Created");
    }

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        ParseUser user = EGDF_App.getUser(); 
        b = (Button) getSherlockActivity().findViewById(R.id.buttonWatchChapter);
        if (user != null){

            String chapterNumber = "";
            Bundle extras = getSherlockActivity().getIntent().getExtras();
            String s = extras.getString(Constants.CHAPTER_NUMBER_PARSE);
            if((s != null)&&(!s.equals(""))){
//              tvNumber.setText(s);
                chapterNumber = "Chapter_" + s;
                Log.i("Properties", s);
            }
            String seen = user.getString(chapterNumber);
            if((seen != null)&&(!seen.equals(""))&&(seen.equals(Constants.USER_CHAPTER_SEEN))){
                b.setText("Second text: "+getString(R.string.watch_chapter_button_text));
                Log.i("BUTTON CHANGED", "Text changed to watch");
            }               
            else if((seen != null)&&(!seen.equals(""))&&(seen.equals(Constants.USER_CHAPTER_NOT_SEEN))){
                b.setText("Second text: " + getString(R.string.buy_chapter_button_text));
                Log.i("BUTTON CHANGED", "Text changed to buy");
            }else{
                Log.w("DEV ERROR", "Chapter text not obtained");
            }
        }else{
            Log.w("DEV ERROR", "ParseUser is null in EGDF_App!!!");
        }

    }

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

    public void setText(String item) {
        getSherlockActivity().getSupportActionBar().setTitle(item);
    }

    public void setButtonText(String text){
        b.setText(text);
    }

}
活动

import android.media.MediaPlayer;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;

import com.actionbarsherlock.app.SherlockFragmentActivity;

public class ChapterDetailsActivity extends SherlockFragmentActivity {

    private Bundle extras;
    MediaPlayer mMediaPlayer;

    private String chapterNumber;
    private boolean isChapterSeen;
    //  private int duration;

    @Override
    protected void onCreate(Bundle savedInstanceState) {

        setTheme(R.style.Theme_Sherlock_Light_DarkActionBar);

        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_chapter_details);

        if (savedInstanceState == null) {
            // During initial setup, plug in the details fragment.
            ChapterDetailsFragment details = new ChapterDetailsFragment();
            details.setArguments(getIntent().getExtras());
            getSupportFragmentManager().beginTransaction().add(
                    android.R.id.content, details).commit();
        }

    }

    public void watchChapter(View view){
        Log.i("Button", "Watch chapter button PRESSED");
        Button b = (Button) view;

        Log.d("BUTTON PARENT VIEW", b.getParent().getClass().getName());
        Log.d("BUTTON ID", String.valueOf(b.getId()));
        String loadChapter = getString(R.string.load_chapter_button_text);
        String watchChapter = getString(R.string.watch_chapter_button_text);
        String buyChapter = getString(R.string.buy_chapter_button_text);

    }

    @Override
    protected void onPause() {
        Log.i("Activity lifecycle", "On pause called");
        super.onPause();
    }

    @Override
    protected void onStop() {
        Log.i("Activity lifecycle", "On stop called");
        super.onStop();
    }

    @Override
    protected void onDestroy() {
        Log.i("Activity lifecycle", "On destroy called");
        super.onDestroy();
        EGDF_App.releaseMediaPlayer();
    }

    @Override
    protected void onResume() {
        Log.i("Activity lifecycle", "On resume called");
        super.onResume();
    }

    @Override
    protected void onStart() {
        Log.i("Activity lifecycle", "On start called");
        super.onStart();
    }
}
活动布局

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <fragment
        android:id="@+id/chapterDetailsFragment"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        class="com.tiempoderodar.egdf.content.ChapterDetailsFragment" />

</LinearLayout> 
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <TextView
        android:id="@+id/textViewChapterNumber"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:textSize="30dip" />

    <TextView
        android:id="@+id/textViewChapterSeason"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:textSize="30dip" />

    <TextView
        android:id="@+id/textViewChapterSinopsis"
        android:layout_width="wrap_content"
        android:layout_height="0dip"
        android:layout_weight="1"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:textSize="30dip" />

    <TextView
        android:id="@+id/textViewChapterCredits"
        android:layout_width="wrap_content"
        android:layout_height="0dip"
        android:layout_weight="1"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:textSize="30dip" />

    <Button 
        android:id="@+id/buttonWatchChapter"
        android:layout_height="wrap_content"
        android:layout_width="wrap_content"
        android:text="@string/watch_chapter_button_text"
        android:layout_gravity="center"
        android:onClick="watchChapter"/>

</LinearLayout>

片段布局

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <fragment
        android:id="@+id/chapterDetailsFragment"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        class="com.tiempoderodar.egdf.content.ChapterDetailsFragment" />

</LinearLayout> 
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <TextView
        android:id="@+id/textViewChapterNumber"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:textSize="30dip" />

    <TextView
        android:id="@+id/textViewChapterSeason"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:textSize="30dip" />

    <TextView
        android:id="@+id/textViewChapterSinopsis"
        android:layout_width="wrap_content"
        android:layout_height="0dip"
        android:layout_weight="1"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:textSize="30dip" />

    <TextView
        android:id="@+id/textViewChapterCredits"
        android:layout_width="wrap_content"
        android:layout_height="0dip"
        android:layout_weight="1"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:textSize="30dip" />

    <Button 
        android:id="@+id/buttonWatchChapter"
        android:layout_height="wrap_content"
        android:layout_width="wrap_content"
        android:text="@string/watch_chapter_button_text"
        android:layout_gravity="center"
        android:onClick="watchChapter"/>

</LinearLayout>


谢谢!

我想我可能已经发现了问题。我正在打电话

b = (Button) getSherlockActivity().findViewById(R.id.button); 
但应该是这样

b = (Button) getView().findViewById(R.id.button);

有了这个更改,它就可以正常工作了

为什么不使用一个按钮对象?你是什么意思?在应用程序类或类似的东西中声明它为静态的?你已经注释掉了顶部的按钮按钮。使用它作为对视图的唯一引用可能会解决问题,而不是运行两次findViewById。恐怕这不是问题所在问题是……我已经取消了注释,并且仅在onActivityCreated()中有getViewById()声明,但它保持不变