我在我的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");
}
});发布于 2013-02-15 05:45:22
在API11中添加了片段类。看起来您的minSDK版本比这个版本旧。如果你想在一个旧的SDK中使用片段,你应该使用支持库,它为你提供了对片段的支持。
有关支持库,请参阅:http://developer.android.com/tools/extras/support-library.html
https://stackoverflow.com/questions/14884482
复制相似问题