0

我在 Bookinfo.title、Bookinfo.author 和 Book.isbn 变量的片段中遇到了这个错误。我不知道为什么。在尝试纠正它时,所有文档只是给了我另一个错误。至于 Bookinf,它还有另一个由 getter 和 setter 组成的类。我在 BookDetailsFragment 类中的 Fragment 一词上出现错误。错误提示将 @SuppressLint 'NewApi' 添加到 BookDetailsFragment。任何帮助将不胜感激,谢谢。

这是我的 BookDetailsFragment 代码:

import android.app.Fragment;

public class BookDetailsFragment extends Fragment {

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, 
        Bundle savedInstanceState) {

    //View view = inflater.inflate(R.layout.book_details, container, false);
    View view = inflater.inflate(R.layout.book_details, null);


    System.out.println("BookDetailsActivity executed");

    //Defines the TextViews in R.layout.book_details
    TextView bkTitle = (TextView)view.findViewById(R.id.bookTitle);
    TextView bkAuth = (TextView)view.findViewById(R.id.bookAuthor);
    TextView bkIsbn = (TextView)view.findViewById(R.id.bookISBN);


    //Retrieve the bundle object passed from BuyFragTab
    Bundle b = getArguments();


    //Getting the item's clicked position and setting corresponding details

    bkTitle.setText("Title:    " + Bookinfo.title[b.getInt("position")]);
    bkAuth.setText("Author:    " + Bookinfo.author[b.getInt("position")]);
    bkIsbn.setText("ISBN:      " + Bookinfo.isbn[b.getInt("position")]);

    return view;
    }

}

这是 Bookinfo 类代码:

package com.skipster.BookBarter;

public class Bookinfo {
private String title;
private String author;
private String isbn;


public Bookinfo(String title, String author, String isbn) {
    this.title = title;
    this.author = author;
    this.isbn = isbn;

            // TODO Auto-generated constructor stub
    }
    public String getTitle() {
        return title;
    }
    public void setTitle(String title) {
        this.title = title;
    }
    public String getAuthor() {
        return author;
    }
    public void setAuthor(String author) {
        this.author = author;
    }
    public String getIsbn() {
        return isbn;
    }
    public void setIsbn(String isbn) {
        this.isbn = isbn;
    }

    //returns all the previous variables to the program that made the call
    @Override
    public String toString() {
        return title + " " + author + " " + isbn;
    }
}

这是 BookDetailsActivity 的代码:

import android.os.Bundle;
import android.app.Activity;
import android.app.FragmentManager;
import android.app.FragmentTransaction;

public class BookDetailsActivity extends Activity {

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    //setting the layout for this activity
    setContentView(R.layout.book_details_activity_layout);

    //get fragment manager for fragment related operations
    FragmentManager fm = getFragmentManager();

    //get fragment transaction object, which can add, move or replace a fragmnt
    FragmentTransaction ft = fm.beginTransaction();

    //instantiating the fragment BookDetailsFragment
    BookDetailsFragment detailsFragment = new BookDetailsFragment();

    //creating a bundle object to pass the data (clicked item's position)
    //from this activity to fragment
    Bundle b = new Bundle();

    //setting the data to the bundle object from the Intent
    b.putInt("position", getIntent().getIntExtra("position", 0));
    System.out.println("the bundle passed" + b);

    //setting the bundle object to the fragment
    detailsFragment.setArguments(b);

    //adding the fragment to the fragment transaction
    ft.add(R.id.book_details_fragment_container, detailsFragment);

    //add the fragment transaction to backstack
    ft.addToBackStack(null);

    //Executing the transaction
    ft.commit();
}
}

这是启动意图的 onClickListener 的代码:

bookLV.setOnItemClickListener(new OnItemClickListener() {
        public void onItemClick(AdapterView<?> parent, View view, int position,
                long id){

        String selectTitle, selectAuthor, selectIsbn = null;

        //When item is clicked, show it's detailed view
        Bookinfo bkRecs = (Bookinfo)parent.getItemAtPosition(position);


        //Creating an intent object to start BookDetailsActivity
        Intent bkIntent = new Intent("com.skipster.BookBarter.BOOKDETAILSACTIVITY");

        //Setting data (the clicked item's position to the intent
        bkIntent.putExtra("position", position);

        System.out.println("Data loaded for intent");
        //Start the activity
        startActivity(bkIntent);

        System.out.println("Intent activity started");
        }
    });
4

1 回答 1

0

在 API 11 中添加了 Fragment 类。看起来您的 minSDK 版本比这更旧。如果您想在较旧的 SDK 中使用 Fragments,您应该使用支持库,它为您提供对 Fragments 的支持。

支持库参考:http: //developer.android.com/tools/extras/support-library.html

于 2013-02-14T21:45:21.970 回答