选项卡式 Activity Android
Tabbed Activity Android
我正在开发一个 android 应用程序,其中有 3 个选项卡:订单状态、订单详细信息、订单信息。
我的问题是当我尝试将 Fragment 添加到此应用程序时收到错误消息。
这是我的代码:
orderTabbedFraagment.java
package com.example.kamran.bluewhite;
import android.support.design.widget.TabLayout;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
public class OrderInfoTabbed extends AppCompatActivity {
/**
* The {@link android.support.v4.view.PagerAdapter} that will provide
* fragments for each of the sections. We use a
* {@link FragmentPagerAdapter} derivative, which will keep every
* loaded fragment in memory. If this becomes too memory intensive, it
* may be best to switch to a
* {@link android.support.v4.app.FragmentStatePagerAdapter}.
*/
private SectionsPagerAdapter mSectionsPagerAdapter;
/**
* The {@link ViewPager} that will host the section contents.
*/
private ViewPager mViewPager;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_order_info_tabbed);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
// Create the adapter that will return a fragment for each of the three
// primary sections of the activity.
mSectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager());
// Set up the ViewPager with the sections adapter.
mViewPager = (ViewPager) findViewById(R.id.container);
mViewPager.setAdapter(mSectionsPagerAdapter);
TabLayout tabLayout = (TabLayout) findViewById(R.id.tabs);
tabLayout.setupWithViewPager(mViewPager);
}
@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_order_info_tabbed, 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);
}
/**
* A placeholder fragment containing a simple view.
*/
public static class PlaceholderFragment extends Fragment {
/**
* The fragment argument representing the section number for this
* fragment.
*/
private static final String ARG_SECTION_NUMBER = "section_number";
public PlaceholderFragment() {
}
/**
* Returns a new instance of this fragment for the given section
* number.
*/
public static PlaceholderFragment newInstance(int sectionNumber) {
PlaceholderFragment fragment = new PlaceholderFragment();
Bundle args = new Bundle();
args.putInt(ARG_SECTION_NUMBER, sectionNumber);
fragment.setArguments(args);
return fragment;
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_order_info_tabbed, container, false);
TextView textView = (TextView) rootView.findViewById(R.id.section_label);
textView.setText(getString(R.string.section_format, getArguments().getInt(ARG_SECTION_NUMBER)));
return rootView;
}
}
/**
* A {@link FragmentPagerAdapter} that returns a fragment corresponding to
* one of the sections/tabs/pages.
*/
public class SectionsPagerAdapter extends FragmentPagerAdapter {
public SectionsPagerAdapter(FragmentManager fm) {
super(fm);
}
@Override
public Fragment getItem(int position) {
switch (position){
case 0:
orderInfoFragment a = new orderInfoFragment();
return a;
}
}
@Override
public int getCount() {
// Show 3 total pages.
return 3;
}
@Override
public CharSequence getPageTitle(int position) {
switch (position) {
case 0:
return "Order Details";
case 1:
return "Order Info";
case 2:
return "Order Status";
}
return null;
}
}
}
订单信息片段.java
package com.example.kamran.bluewhite;
import android.app.Fragment;
import android.app.ProgressDialog;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.Toast;
import com.google.firebase.database.DataSnapshot;
import com.google.firebase.database.DatabaseError;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;
import com.google.firebase.database.ValueEventListener;
import java.util.ArrayList;
/**
* Created by rahafo on 12/5/2017.
*/
public class orderInfoFragment extends Fragment{
double sum=0;
DatabaseReference mDatabase;
ListView shoppingList;
ArrayList<Shopping_cart> Shopping_cart;
DatabaseReference Shopping_cartRef;
DatabaseReference ShoppingcartProduct;
ProgressDialog mProgressDialog;
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
final View view = inflater.inflate(R.layout.activity_order_info,container,false);
mProgressDialog = new ProgressDialog(getActivity());
mProgressDialog.setMessage("Loading Products..");
mProgressDialog.show();
Intent i = getActivity().getIntent();
String uid = i.getExtras().getString("name","");
Shopping_cart = new ArrayList<Shopping_cart>();
LinearLayout productList = (LinearLayout) view.findViewById(R.id.shoppingList);
mDatabase = FirebaseDatabase.getInstance().getReference();
Shopping_cartRef = mDatabase.child("Order");
ShoppingcartProduct = Shopping_cartRef.child(uid).child("products");
ValueEventListener eventListener = new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
for(DataSnapshot ds : dataSnapshot.getChildren()) {
Shopping_cart product = ds.getValue(Shopping_cart.class);
Shopping_cart.add(product);
adapterShopping itemsAdapter = new adapterShopping(getActivity(), Shopping_cart);
shoppingList = (ListView) view.findViewById(R.id.listViewshopping);
shoppingList.setAdapter(itemsAdapter);
}
mProgressDialog.dismiss();
}
@Override
public void onCancelled(DatabaseError databaseError) {}
};
ShoppingcartProduct.addListenerForSingleValueEvent(eventListener);
return view;
}
}
我得到的错误是:Error:(134, 28) error: incompatible types: orderInfoFragment cannot be converted to Fragment
但是 orderInfoFragment 正在扩展 Fragment 所以我不知道是什么问题!
我是不是做错了什么?
您正在混淆 imports
,这是很常见的问题。要么你需要坚持支持库提供的组件,如 Fragment
s 和东西(所以你应该有 import android.support....
条目) - 这就是你在 OrderInfoTabbed
class 代码中所拥有的。或者使用 plaftorm 提供的 Fragment
s 和东西 - 正如你在 orderInfoFragment
class 中所做的那样。但是,虽然您可以混合使用它们,并且从技术上讲,可以在您的代码中 有意 同时使用它们,但它们 NOT 等效,正如您所看到的不能从一个转换到另一个。
作为解决方案,删除 import android.app....
条目并重新添加它(参见 Android Studio can help you)这次请注意您选择的是哪一个,因为 AS 会显示选项列表。
如果不确定你想要哪一个,请坚持支持 lib 的那个,我相信你无论如何都想要。
我正在开发一个 android 应用程序,其中有 3 个选项卡:订单状态、订单详细信息、订单信息。 我的问题是当我尝试将 Fragment 添加到此应用程序时收到错误消息。 这是我的代码:
orderTabbedFraagment.java
package com.example.kamran.bluewhite;
import android.support.design.widget.TabLayout;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
public class OrderInfoTabbed extends AppCompatActivity {
/**
* The {@link android.support.v4.view.PagerAdapter} that will provide
* fragments for each of the sections. We use a
* {@link FragmentPagerAdapter} derivative, which will keep every
* loaded fragment in memory. If this becomes too memory intensive, it
* may be best to switch to a
* {@link android.support.v4.app.FragmentStatePagerAdapter}.
*/
private SectionsPagerAdapter mSectionsPagerAdapter;
/**
* The {@link ViewPager} that will host the section contents.
*/
private ViewPager mViewPager;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_order_info_tabbed);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
// Create the adapter that will return a fragment for each of the three
// primary sections of the activity.
mSectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager());
// Set up the ViewPager with the sections adapter.
mViewPager = (ViewPager) findViewById(R.id.container);
mViewPager.setAdapter(mSectionsPagerAdapter);
TabLayout tabLayout = (TabLayout) findViewById(R.id.tabs);
tabLayout.setupWithViewPager(mViewPager);
}
@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_order_info_tabbed, 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);
}
/**
* A placeholder fragment containing a simple view.
*/
public static class PlaceholderFragment extends Fragment {
/**
* The fragment argument representing the section number for this
* fragment.
*/
private static final String ARG_SECTION_NUMBER = "section_number";
public PlaceholderFragment() {
}
/**
* Returns a new instance of this fragment for the given section
* number.
*/
public static PlaceholderFragment newInstance(int sectionNumber) {
PlaceholderFragment fragment = new PlaceholderFragment();
Bundle args = new Bundle();
args.putInt(ARG_SECTION_NUMBER, sectionNumber);
fragment.setArguments(args);
return fragment;
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_order_info_tabbed, container, false);
TextView textView = (TextView) rootView.findViewById(R.id.section_label);
textView.setText(getString(R.string.section_format, getArguments().getInt(ARG_SECTION_NUMBER)));
return rootView;
}
}
/**
* A {@link FragmentPagerAdapter} that returns a fragment corresponding to
* one of the sections/tabs/pages.
*/
public class SectionsPagerAdapter extends FragmentPagerAdapter {
public SectionsPagerAdapter(FragmentManager fm) {
super(fm);
}
@Override
public Fragment getItem(int position) {
switch (position){
case 0:
orderInfoFragment a = new orderInfoFragment();
return a;
}
}
@Override
public int getCount() {
// Show 3 total pages.
return 3;
}
@Override
public CharSequence getPageTitle(int position) {
switch (position) {
case 0:
return "Order Details";
case 1:
return "Order Info";
case 2:
return "Order Status";
}
return null;
}
}
}
订单信息片段.java
package com.example.kamran.bluewhite;
import android.app.Fragment;
import android.app.ProgressDialog;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.Toast;
import com.google.firebase.database.DataSnapshot;
import com.google.firebase.database.DatabaseError;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;
import com.google.firebase.database.ValueEventListener;
import java.util.ArrayList;
/**
* Created by rahafo on 12/5/2017.
*/
public class orderInfoFragment extends Fragment{
double sum=0;
DatabaseReference mDatabase;
ListView shoppingList;
ArrayList<Shopping_cart> Shopping_cart;
DatabaseReference Shopping_cartRef;
DatabaseReference ShoppingcartProduct;
ProgressDialog mProgressDialog;
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
final View view = inflater.inflate(R.layout.activity_order_info,container,false);
mProgressDialog = new ProgressDialog(getActivity());
mProgressDialog.setMessage("Loading Products..");
mProgressDialog.show();
Intent i = getActivity().getIntent();
String uid = i.getExtras().getString("name","");
Shopping_cart = new ArrayList<Shopping_cart>();
LinearLayout productList = (LinearLayout) view.findViewById(R.id.shoppingList);
mDatabase = FirebaseDatabase.getInstance().getReference();
Shopping_cartRef = mDatabase.child("Order");
ShoppingcartProduct = Shopping_cartRef.child(uid).child("products");
ValueEventListener eventListener = new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
for(DataSnapshot ds : dataSnapshot.getChildren()) {
Shopping_cart product = ds.getValue(Shopping_cart.class);
Shopping_cart.add(product);
adapterShopping itemsAdapter = new adapterShopping(getActivity(), Shopping_cart);
shoppingList = (ListView) view.findViewById(R.id.listViewshopping);
shoppingList.setAdapter(itemsAdapter);
}
mProgressDialog.dismiss();
}
@Override
public void onCancelled(DatabaseError databaseError) {}
};
ShoppingcartProduct.addListenerForSingleValueEvent(eventListener);
return view;
}
}
我得到的错误是:Error:(134, 28) error: incompatible types: orderInfoFragment cannot be converted to Fragment 但是 orderInfoFragment 正在扩展 Fragment 所以我不知道是什么问题! 我是不是做错了什么?
您正在混淆 imports
,这是很常见的问题。要么你需要坚持支持库提供的组件,如 Fragment
s 和东西(所以你应该有 import android.support....
条目) - 这就是你在 OrderInfoTabbed
class 代码中所拥有的。或者使用 plaftorm 提供的 Fragment
s 和东西 - 正如你在 orderInfoFragment
class 中所做的那样。但是,虽然您可以混合使用它们,并且从技术上讲,可以在您的代码中 有意 同时使用它们,但它们 NOT 等效,正如您所看到的不能从一个转换到另一个。
作为解决方案,删除 import android.app....
条目并重新添加它(参见 Android Studio can help you)这次请注意您选择的是哪一个,因为 AS 会显示选项列表。
如果不确定你想要哪一个,请坚持支持 lib 的那个,我相信你无论如何都想要。