为什么我这个 ViewPager 程序发布到手机就闪退

谁学逆向工程 2016-10-11 04:33:50
视频教程是苹果系统和eclipse。我是Windows和Android studio

<!-- view1.xml -->
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="第一个界面"/>
</LinearLayout>


<!-- view2.xml -->
]<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="第二个界面"/>
</LinearLayout>


<!-- view3.xml -->
]<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="第三个界面"/>
</LinearLayout>


<!-- view4.xml -->
]<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="第四个界面"/>
</LinearLayout>


//MyPagerAdapter.java
package com.example.administrator.viewpager;

import android.support.v4.view.PagerAdapter;
import android.view.View;
import android.view.ViewGroup;

import java.util.List;

/**
* Created by Administrator on 2016/10/11.
*/
public class MyPagerAdapter extends PagerAdapter
{
List<View> viewList;
public MyPagerAdapter(List<View> viewListv)
{
this.viewList = viewList;
}
//返回的是页卡的数量
public int getCount()
{
return viewList.size();
}

//view是否来自于对象
public boolean isViewFromObject(View view, Object object)
{
return view==object;
}

//实例化一个页卡
public Object instantiateItem(ViewGroup container, int position)
{
container.addView(viewList.get(position));
return viewList.get(position);
}

//销毁一个页卡
public void destroyItem(ViewGroup container, int position, Object object)
{
container.removeView(viewList.get(position));
}
}


//MainActivity.java
package com.example.administrator.viewpager;

import android.app.Activity;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.view.ViewParent;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends Activity {

List<View> viewList;
ViewPager pager;
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
viewList = new ArrayList<View>();
//通过View对象去作为ViewPager的数据源
View view1 = View.inflate(this, R.layout.view1, null);
View view2 = View.inflate(this, R.layout.view2, null);
View view3 = View.inflate(this, R.layout.view3, null);
View view4 = View.inflate(this, R.layout.view4, null);
viewList.add(view1);
viewList.add(view2);
viewList.add(view3);
viewList.add(view4);

//初始化ViwPager
pager = (ViewPager) findViewById(R.id.pager);

//创建PagerAdapter的适配器
MyPagerAdapter adapter = new MyPagerAdapter(viewList);

//ViewPager加载适配器
pager.setAdapter(adapter);
}
}


<! activity_main.xml -->
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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:orientation="vertical">

<android.support.v4.view.ViewPager
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:id="@+id/pager">
</android.support.v4.view.ViewPager>

</LinearLayout>
...全文
146 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
谁学逆向工程 2016-10-11
  • 打赏
  • 举报
回复
引用 6 楼 ink_s 的回复:
我早上发了个贴 200分的 没人回 问题结局了 还没结贴
可怜的你。
ink_s 2016-10-11
  • 打赏
  • 举报
回复
引用 5 楼 xiaoyuanyuan2009 的回复:
[quote=引用 4 楼 ink_s 的回复:]原来是无满意呀
是呀,我先找到了问题在哪,然后就结贴了,当时你还没回帖呢,我就选择无满意结贴了[/quote] 我早上发了个贴 200分的 没人回 问题结局了 还没结贴
谁学逆向工程 2016-10-11
  • 打赏
  • 举报
回复
引用 4 楼 ink_s 的回复:
原来是无满意呀
是呀,我先找到了问题在哪,然后就结贴了,当时你还没回帖呢,我就选择无满意结贴了
ink_s 2016-10-11
  • 打赏
  • 举报
回复
引用 3 楼 ink_s 的回复:
[quote=引用 2 楼 xiaoyuanyuan2009 的回复:] [quote=引用 1 楼 ink_s 的回复:] 燃鹅 我什么错误LOG也看不到呀( ⊙ o ⊙ )!
我先结的贴,你后回的帖,我已经找到毛病在哪了。MyAdapter类的构造函数的参数多打了个字母[/quote]0.0 没人回复不能结贴呀[/quote]原来是无满意呀
ink_s 2016-10-11
  • 打赏
  • 举报
回复
引用 2 楼 xiaoyuanyuan2009 的回复:
[quote=引用 1 楼 ink_s 的回复:] 燃鹅 我什么错误LOG也看不到呀( ⊙ o ⊙ )!
我先结的贴,你后回的帖,我已经找到毛病在哪了。MyAdapter类的构造函数的参数多打了个字母[/quote]0.0 没人回复不能结贴呀
谁学逆向工程 2016-10-11
  • 打赏
  • 举报
回复
引用 1 楼 ink_s 的回复:
燃鹅 我什么错误LOG也看不到呀( ⊙ o ⊙ )!
我先结的贴,你后回的帖,我已经找到毛病在哪了。MyAdapter类的构造函数的参数多打了个字母
ink_s 2016-10-11
  • 打赏
  • 举报
回复
燃鹅 我什么错误LOG也看不到呀( ⊙ o ⊙ )!

80,351

社区成员

发帖
与我相关
我的任务
社区描述
移动平台 Android
androidandroid-studioandroidx 技术论坛(原bbs)
社区管理员
  • Android
  • yechaoa
  • 失落夏天
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧