我是android编程的新手。所以请原谅我,如果我问无意义的问题。在关注如何创建ListFragments的几个教程后,编写的程序编译没有错误,但拒绝安装在AVD上。 这是它给出的错误
[2014-05-08 07:44:46 - TabsWithSwipe] Uploading TabsWithSwipe.apk onto device 'emulator- 5554'
[2014-05-08 07:44:46 - TabsWithSwipe] Installing TabsWithSwipe.apk...
[2014-05-08 07:48:59 - TabsWithSwipe] Failed to install TabsWithSwipe.apk on device 'emulator-5554!
[2014-05-08 07:48:59 - TabsWithSwipe] (null)
[2014-05-08 07:49:00 - TabsWithSwipe] Launch canceled!
以下是个别代码,如果它有助于详细说明问题。 MainActivity.java
package com.example.tabswithswipe;
import android.annotation.SuppressLint;
import android.app.ActionBar;
import android.app.ActionBar.Tab;
import android.app.FragmentTransaction;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.support.v4.view.ViewPager;
import com.example.tabswithswipe.adapter.TabsPagerAdapter;
@SuppressLint("NewApi")
public class MainActivity extends FragmentActivity implements ActionBar.TabListener{
private ViewPager viewPager;
private TabsPagerAdapter mAdapter;
private ActionBar actionBar;
//tab titles
private String[] tabs = {"Top Rated","Games","Movies"};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
viewPager = (ViewPager)findViewById(R.id.pager);
actionBar = getActionBar();
mAdapter = new TabsPagerAdapter(getSupportFragmentManager());
viewPager.setAdapter(mAdapter);
actionBar.setHomeButtonEnabled(false);
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
//ADding TAbs
for (String tab_name : tabs)
{
actionBar.addTab(actionBar.newTab().setText(tab_name)
.setTabListener(this));
}
//on swipping the viewpager make respective tab selected
viewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
public void onPageSelected(int position)
{
//on changing the page
//make respected tab selected
actionBar.setSelectedNavigationItem(position);
}
public void onPageScrolled(int arg0, float arg1, int arg2)
{
}
public void onPageScrollStateChanged(int arg0)
{
}
});
}
@Override
public void onTabReselected(Tab tab, FragmentTransaction ft) {
// TODO Auto-generated method stub
}
@Override
public void onTabSelected(Tab tab, FragmentTransaction ft) {
// TODO Auto-generated method stub
viewPager.setCurrentItem(tab.getPosition());
}
@Override
public void onTabUnselected(Tab tab, FragmentTransaction ft) {
// TODO Auto-generated method stub
}
}
activity_main.xml中
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >
<android.support.v4.view.ViewPager
android:id="@+id/pager"
android:layout_width="match_parent"
android:layout_height="match_parent">
</android.support.v4.view.ViewPager>
</RelativeLayout>
这是TabsPagerAdapter的类
package com.example.tabswithswipe.adapter;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.app.ListFragment;
import com.example.tabswithswipe.GamesFragment;
import com.example.tabswithswipe.MoviesFragment;
import com.example.tabswithswipe.TopRatedFragment;
public class TabsPagerAdapter extends FragmentPagerAdapter{
public TabsPagerAdapter(FragmentManager fm){
super(fm);
}
public ListFragment getItem(int index)
{
switch(index)
{
case 0:
//Top Rated fragment activity
return new TopRatedFragment();
case 1:
//Games fragment activity
return new GamesFragment();
case 2:
//Movies fragment activity
return new MoviesFragment();
}
return null;
}
public int getCount()
{
//get item count,equal to number of tabs
return 3;
}
}
这是一个名为GameFragments的自定义ListFragments
package com.example.tabswithswipe;
import android.annotation.SuppressLint;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.support.v4.app.ListFragment;
@SuppressLint("NewApi")
public class GamesFragment extends ListFragment {
String[] list_items;
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
View rootView = inflater.inflate(R.layout.fragment_games, container, false);
list_items = getResources().getStringArray(R.array.my_data_list);
setListAdapter(new ArrayAdapter<String>(getActivity(), android.R.layout.simple_list_item_1,list_items));
return rootView;
}
}
以下是此gameFragment的xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#ff8400"
android:orientation="vertical" >
<TextView android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:textSize="20dp"
android:layout_centerInParent ="true"/>
<ListView
android:id="@+id/listView1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true" >
</ListView>
</RelativeLayout>
其他两个海关清单碎片类和xml是相同的。 我将不胜感激任何帮助
答案 0 :(得分:0)
您的模拟器看起来不支持您使用的API,因为模拟器上的Android版本太低了。
尝试使用Android版本的模拟器&gt; 4.0