我尝试了下面提到的这个代码,但在运行时遇到崩溃.发生的错误是Android运行时:
致命异常:主要进程:com.root.specialbridge,PID:17706 kotlin.KotlinNullPointerException at com.root.specialbridge.fragments.profile_fragment.WallFragments.initializeView(WallFragments.kt:49)
class WallFragments : Fragment(){
private var wallAdapter: WallAdapter? = null
private var wall_recycler: RecyclerView? = null
private val wallArrayList: ArrayList<Wall>? = null
private var mainlayout: LinearLayout? = null
private var no_result_found_layout: RelativeLayout? = null
private var userProfileWallInterface: UserProfileWallInterface? = null
internal var wallActivityBeanse: MutableList<WallActivityBeans> = ArrayList()
override fun onCreateView(inflater: LayoutInflater?, container: ViewGroup?, savedInstanceState: Bundle?): View? {
val view = inflater!!.inflate(R.layout.wall_fragments, container, false)
userProfileWallInterface = UserProfileWallPresentation(activity, this)
initializeView()
wallAdapter = WallAdapter(activity, wallActivityBeanse)
wall_recycler!!.adapter = wallAdapter
return …Run Code Online (Sandbox Code Playgroud) 我一直在读很多片段.发现其他人在检索片段视图时遇到问题,因为总是返回null,但没有答案解决了我的问题.我要做的是创建一个图库.我有一个包含图像视图的片段.为了显示片段,我使用了android.support.v4.view.ViewPager.而喂ViewPager我使用一个android.support.v4.app.FragmentStatePagerAdapter.
问题是,当所有内容都构建并显示图像时,我想将当前显示的图像保存到磁盘.所以我需要得到当前的片段imageview,但我不能,因为fragment.getView()总是为null,与片段相关的活动也是null,我无法弄清楚它为什么.这里有一些代码,看看是否有人可以帮助:
这是我的片段:
public class ImageViewURLFragment extends Fragment
{
String _url;
@Override
public View onCreateView(LayoutInflater inflater,
ViewGroup container, Bundle savedInstanceState)
{
// The last two arguments ensure LayoutParams are inflated
// properly.
View rootView = new ImageViewURL(getActivity(), _url);
return rootView;
}
public void setImageURL(String imgURL)
{
_url=imgURL;
}
}
Run Code Online (Sandbox Code Playgroud)
这是我的适配器:
public class ImageViewURLCustomAdapter extends FragmentStatePagerAdapter
{
List<String> _urls=new ArrayList<String>();
public ImageViewURLCustomAdapter(FragmentManager fm)
{
super(fm);
}
@Override
public Fragment getItem(int i)
{
ImageViewURLFragment fragment = new ImageViewURLFragment(); …Run Code Online (Sandbox Code Playgroud) android android-layout android-fragments fragmentstatepageradapter
我已经在Google Play中安装了一个应用程序,在我升级Android Studio之前完全没问题.必须修改build.gradle以便可以编译项目以下是我更改的内容:
dependencies {
compile fileTree(dir: 'libs', include: '*.jar')
compile 'com.android.support:appcompat-v7:21.0.0'
compile 'com.google.android.gms:play-services:6+'
compile 'com.android.support:support-v4:+'
}
Run Code Online (Sandbox Code Playgroud)
唯一的变化是com.android.support:appcompat-v7:+,它更改为com.android.support:appcompat-v7:21.0.0.
我的问题是.findViewById(R.id.tabhost)总是返回null,以便应用程序崩溃.这是我的布局文件:
<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.app.FragmentTabHost xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/tabhost"
android:layout_width="match_parent"
android:layout_height="match_parent" >
Run Code Online (Sandbox Code Playgroud)
我的java代码:
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentTabHost;
public class MenuLeftFragment extends Fragment {
private FragmentTabHost mFragmentTabHost;
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
mView = getView();
if (mView != null) {
mFragmentTabHost = (FragmentTabHost) mView
.findViewById(R.id.tabhost);
Run Code Online (Sandbox Code Playgroud)
任何的想法?谢谢.