Android将图像按钮等中的常规布局更改为选项卡布局错误? [英] Android Changing a Normal Layout to Tab Layout error in Image buttons and other?

查看:109
本文介绍了Android将图像按钮等中的常规布局更改为选项卡布局错误?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我关注了

I followed this to create an Android Radio Stream

那边工作正常,所以我正在尝试为此添加标签页布局

over there It's working fine So I am trying to add a tab-layout to this

所以我遵循了添加标签页布局

So I followed this to add Tab layout

在那边我添加了,但出现了很多错误,所以我做了一些修改

Over there I have Added But I got Lot of error so I have Changed some like

FROM

 protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    setContentView(R.layout.main_activity);

为此

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {

    View viewRoot = inflater.inflate(R.layout.main_activity, container, false);

并在on-create的某些位置添加了viewRoot.findViewById,但是在创建之后,我曾经给出过类似getActivity().findViewById

And added viewRoot.findViewById in some places in on-create but after On create What ever are there I have Given like getActivity().findViewById

因为我在onCreate中给出了return viewRoot;,所以不可能

Because I have Given return viewRoot; in onCreate it is not possible

现在我在play.setBackground(getResources().getDrawable(R.drawable.volume0));上遇到错误

    java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.ImageView.setBackground(android.graphics.drawable.Drawable)' on a null object reference
at star.yx.tabview.ProfileFragment.updatePlayStatus(ProfileFragment.java:370)
at star.yx.tabview.ProfileFragment.setIFace(ProfileFragment.java:205)
at star.yx.tabview.ProfileFragment.onCreateView(ProfileFragment.java:158)
at android.support.v4.app.Fragment.performCreateView(Fragment.java:2184)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1298)
at android.support.v4.app.FragmentManagerImpl.moveFragmentsToInvisible(FragmentManager.java:2323)
at android.support.v4.app.FragmentManagerImpl.executeOpsTogether(FragmentManager.java:2136)
at android.support.v4.app.FragmentManagerImpl.optimizeAndExecuteOps(FragmentManager.java:2092)
at android.support.v4.app.FragmentManagerImpl.execSingleAction(FragmentManager.java:1969)
at android.support.v4.app.BackStackRecord.commitNowAllowingStateLoss(BackStackRecord.java:620)
at android.support.v4.app.FragmentPagerAdapter.finishUpdate(FragmentPagerAdapter.java:143)
at android.support.v4.view.ViewPager.populate(ViewPager.java:1268)
at android.support.v4.view.ViewPager.populate(ViewPager.java:1116)
at android.support.v4.view.ViewPager.onMeasure(ViewPager.java:1642)
at android.view.View.measure(View.java:18596)
at android.widget.RelativeLayout.measureChildHorizontal(RelativeLayout.java:728)
at android.widget.RelativeLayout.onMeasure(RelativeLayout.java:464)
at android.view.View.measure(View.java:18596)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5827)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:430)
at android.support.v7.widget.ContentFrameLayout.onMeasure(ContentFrameLayout.java:139)
at android.view.View.measure(View.java:18596)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5827)
at android.support.v7.widget.ActionBarOverlayLayout.onMeasure(ActionBarOverlayLayout.java:393)
at android.view.View.measure(View.java:18596)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5827)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:430)
at android.view.View.measure(View.java:18596)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5827)
at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1435)
at android.widget.LinearLayout.measureVertical(LinearLayout.java:721)
at android.widget.LinearLayout.onMeasure(LinearLayout.java:612)
at android.view.View.measure(View.java:18596)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5827)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:430)
at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2897)
at android.view.View.measure(View.java:18596)
at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:2248)
at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1306)
at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1548)
at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1191)
at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:6642)
at android.view.Choreographer$CallbackRecord.run(Choreographer.java:777)
at android.view.Choreographer.doCallbacks(Choreographer.java:590)
at android.view.Choreographer.doFrame(Choreographer.java:560)
at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:763)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:145)
at android.app.ActivityThread.main(ActivityThread.java:5951)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1400)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:

我已经在ProfileFragment中添加了我的代码以用于测试....

I have Added my code in ProfileFragment for Testing purpose....

有人可以建议我如何使用我的活动吗?在示例表格布局等片段中,请以这种方式建议我

Can anyone Suggest me How to use My activity In fragments like my example table layout Please suggest me on this kind

更新

我正在尝试所有.

我只是在平板电脑布局中使用收音机布局,所以在一个选项卡中,我可以在其他选项卡中播放收音机,也可以在其他选项卡中使用.但是在这里,我的广播服务不在片段/选项卡布局中,所以我遇到了很多错误有人可以建议我如何在我的广播活动中添加制表符布局,但我却遵循我的android教程,但我尝试又累了,请帮助我如何使用那两个例子合而为一...

I am just Using my Radio Layout In a Tablet Layout So in one tab I can play the radio in other I can Use some other. But Here My radio service is not in fragment/Tab layout So I am getting A lot of errors Can anyone Suggest me How to add Tab-layout to my Radio Activity I followed my android tutorials but I tried and tired Please Help me How to Use those two examples in one ...

推荐答案

原因

onResume中,您正在调用setIFace()方法,该方法在内部触发updatePlayStatus方法.

In onResume you are calling setIFace() method which internally triggers updatePlayStatus method.

如果您查看updatePlayStatus方法,您将看到有一个ImageView播放,分配给ID为icon_play

If you look at updatePlayStatus method you will see there is an ImageView play which is assigned to view with ID icon_play

private void updatePlayStatus() {
    ImageView play = (ImageView) findViewById(R.id.icon_play);

    if ("stop".equals(etat_lecture) || "pause".equals(etat_lecture)) {
        play.setBackground(getDrawable(R.drawable.ic_equalizer0));
    } else {
        play.setBackground(getDrawable(R.drawable.ic_equalizer));
    }
}

如果Android找不到ID = icon_play的任何视图,则Android将自动为其分配null.稍后您调用play.setBackground,由于play变量在您的情况下为null,因此您将获得Null Pointer异常.

If Android can't find any view with ID = icon_play then Android will automatically assign it null. Later you are calling play.setBackground, Since play variable is null in your case, you are getting Null Pointer exception.

解决方案

  • 首先,检查片段类中是否使用了正确的布局.必须有一个ID为icon_play的视图.
  • 其次,尝试清理并构建该项目.可能是由于生成的R.java类错误导致的.
  • 最重要,请使用viewRoot.findViewById代替getActivity().findViewById getActivity可能返回null ,或者最好使用 ButterKnife 避免所有这些样板
  • First, check if you are using the correct layout in your fragment class ie. there must be some view with ID icon_play.
  • Secondly, try to clean and build the project. It can occur because of faulty generated R.java class
  • Most important , Use viewRoot.findViewById instead of getActivity().findViewById, getActivity might return null, or better use ButterKnife to avoid all those boilerplate

MainActivity代码是完整的网格,我将其分解为片段

MainActivity code was complete mesh, I have broken it down into fragments

您可以在我的GitHub上查看源代码

You can take a look at source code on my GitHub

https://github.com/hiteshsahu/Android-Radio

这篇关于Android将图像按钮等中的常规布局更改为选项卡布局错误?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆