1

私はAndroidサポートライブラリを使用して、Froyoのフラグメントと、Sherlock拡張機能を使用してActionBarを表示できるようにしています。

3つのtextViewと1つのボタンを表示するフラグメントがあり、インテントの内容に応じてボタンのテキストを動的に変更できるようにしたいと考えています。問題は、button.setText(String text)を呼び出すと、2番目のボタンが表示されることです。ただし、それぞれをクリックするときにbutton.getId()を呼び出しましたが、Idは同じです。なぜこれが起こっているのか私にはわかりませんので、助けていただければ幸いです。

このアプリは、Android2.3.3を搭載したSamsungGalaxySで実行されます。まだ十分な評判がないため、スクリーンキャプチャをアップロードできません:(

これはコードです:

断片

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>

ありがとう!

4

1 に答える 1

0

私は問題を見つけたかもしれないと思います。私は呼んでいた

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

しかし、それは

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

その変更でそれは正しく動作します

于 2012-09-15T22:03:22.003 に答える