Java Android Studio编码

Java Android Studio编码,java,android,android-fragments,Java,Android,Android Fragments,我的任务是将活动转换为片段 我做了一些改变,但仍然不起作用。我遗漏了一些东西,但我想不出来。我确实看到的一个问题是,公共类QuoteFragment扩展了SingleFregmentActivity,它是underline…不确定我忘了做什么。救命啊 当我想要将活动转换为片段时,我忘记了什么 活动的代码 import android.os.Bundle; import android.support.design.widget.FloatingActionButton; import andro

我的任务是将活动转换为片段

我做了一些改变,但仍然不起作用。我遗漏了一些东西,但我想不出来。我确实看到的一个问题是,公共类QuoteFragment扩展了SingleFregmentActivity,它是underline…不确定我忘了做什么。救命啊

当我想要将活动转换为片段时,我忘记了什么

活动的代码

import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Menu;
import android.view.MenuItem;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.content.Intent;

/**
* Main activity for the application.
 * Displays a series of quotes
 */
public class QuoteFragment extends SingleFragmentActivity{
/** Key for fact about author stored in Intent sent to AuthorFactActivity. */
public static final String EXTRA_AUTHOR_FACT =
        "edu.andrews.cptr252.stephanien.quoteoftheday.author_fact";
private static final String KEY_QUOTE_INDEX = "quoteIndex";
/**ImageView used to display inspirational image*/
private ImageView mImageView;

private TextView mQuoteTextView;
private TextView mAuthorTextView;
private Button mNextButton;

/**Quotes used in app */
private Quote[] mQuoteList = new Quote[]{
        new Quote(R.string.quote_text_0, R.string.quote_author_0,
                R.string.author_fact_0, R.drawable.mountain_pic),
        new Quote(R.string.quote_text_1, R.string.quote_author_1,
                R.string.author_fact_1, R.drawable.lier),
        new Quote(R.string.quote_text_2, R.string.quote_author_2,
                R.string.author_fact_2, R.drawable.math),
        new Quote(R.string.quote_text_3, R.string.quote_author_3,
                R.string.author_fact_3, R.drawable.smiley),
        new Quote(R.string.quote_text_4, R.string.quote_author_4,
                R.string.author_fact_4, R.drawable.th),
};

/** Index of current quote in list */
private int mCurrentIndex = 0;

/** Launch activity to display author fact */
private void displayAuthorFact(){
    //Create intent with name of class for second activity.
    //This intent will be sent to the Activity Manager in the OS
    //Which will launch the activity.
    Intent i = new Intent(QuoteFragment.this, AuthorFactActivity.class);
    //Add extra containing resource id for fact
    i.putExtra(EXTRA_AUTHOR_FACT, mQuoteList[mCurrentIndex].getAuthorFact());
    //Send the intent to the activity manager.
    startActivity(i);
}

/**
 * Remember the current quote when the activity is destroyed
 * @param savedInstanceState Bundle used for saving identity of current quote
 */
@Override
public void onSaveInstanceState(Bundle savedInstanceState) {
    super.onSaveInstanceState(savedInstanceState);
    //Stored the index of the current quote in the bundle.
    //Use our key to access the value later.
    savedInstanceState.putInt(KEY_QUOTE_INDEX, mCurrentIndex);
}

/**
 * Setup and inflate layout.
 * @param savedInstanceState Previously saved Bundle
 */
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    View v = inflater.inflate(R.layout.activity_fragment, container, false);
    //mQuoteTextView.setText("This should generate an error. Do you see why?");

    //Re-display the same quote we were on when activity destroyed
    if(savedInstanceState != null){
        mCurrentIndex = savedInstanceState.getInt(KEY_QUOTE_INDEX);
    }
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    //setSupportActionBar(toolbar);

    FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
    fab.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View view) {
            Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
                    .setAction("Action", null).show();
        }
    });
    //Display that text for the quote
    mQuoteTextView = (TextView) findViewById(R.id.quoteTextView);
    int quote = mQuoteList[mCurrentIndex].getQuote();
    mQuoteTextView.setText(quote);
    mQuoteTextView.setOnClickListener(new View.OnClickListener(){
        @Override
        public void onClick(View v) {
            displayAuthorFact();
        }
    });

    //Display the author of the quote
    mAuthorTextView = (TextView) findViewById(R.id.authorTextView);
    int author = mQuoteList[mCurrentIndex].getAuthor();
    mAuthorTextView.setText(author);
    mAuthorTextView.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            displayAuthorFact();
        }
    });

    //Display image
    mImageView = (ImageView) findViewById(R.id.imageView);
    mImageView.setImageResource(R.drawable.mountain_pic);

    //set up listener to handle next button presses
    mNextButton = (Button) findViewById(R.id.nextButton);
    mNextButton.setOnClickListener(new View.OnClickListener(){
        @Override
        public void onClick(View v) {
            // move to the next quote in the list
            //if index reaches end array,
            //reset index to zero (first quote)
            mCurrentIndex++;
            if(mCurrentIndex == mQuoteList.length){
                mCurrentIndex = 0;
            }
            updateQuote();
        }
    });
    return v;
}
/** Display the quote at the current index. */
private void updateQuote(){
    int quote = mQuoteList[mCurrentIndex].getQuote();
    int author = mQuoteList[mCurrentIndex].getAuthor();
    int picture = mQuoteList[mCurrentIndex].getPicture();
    mQuoteTextView.setText(quote);
    mAuthorTextView.setText(author);
    mImageView.setImageResource(picture);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.menu_quote, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();

    //noinspection SimplifiableIfStatement
    if (id == R.id.action_settings) {
        return true;
    }

    return super.onOptionsItemSelected(item);
}
}
  • 创建
    QuoteFragment
  • QuoteActivity
  • 删除
    setContent(R.layout.activity_quote)
    并实现
    onCreateView(布局充气机、视图组容器、捆绑包保存状态)
    方法
  • 在创建的视图中初始化视图(视图视图,Bundle savedInstanceState)方法
  • activity\u quote.xml
    重命名为
    fragment\u quote.xml
  • 创建xml文件
    activity_quote.xml
    ,并将
    QuoteFragment
    添加为内容:

    <fragment android:name="com.example.android.fragments.QuoteFragment"
              android:layout_width="match_parent"
              android:layout_height="match_parent" />
    


  • 根据下面的答案,我认为你的答案有点模糊。我重写了我需要帮助的内容。下划线可能表示编译器错误。如果您将鼠标悬停在它上面,或者最好只是尝试编译代码,您应该会看到错误的文本。它可能会帮助你找出问题所在;如果没有,将错误全文输入您的问题将有助于我们帮助您。@JeffreyBosboom
    QuoteFragment extends SingleFragmentActivity
    。它不是碎片。
    protected void onCreate(Bundle savedInstanceState) {
       super.onCreate(savedInstanceState);
       setContentView(R.layout.activity_quote);
    }