【问题标题】:Android NullPointerException: Attempt to invoke virtual methodAndroid NullPointerException:尝试调用虚拟方法
【发布时间】:2015-07-18 20:15:09
【问题描述】:

我有以下两个包含 TextView 和 Button 的 Fragment:

public class MyFragmentText extends Fragment {
public static final String EXTRA_MESSAGE = "EXTRA_MESSAGE";

public static final MyFragmentText newInstance(String message)
{
    MyFragmentText f = new MyFragmentText();
    Bundle bdl = new Bundle(1);
    bdl.putString(EXTRA_MESSAGE, message);
    f.setArguments(bdl);
    return f;
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    String message = getArguments().getString(EXTRA_MESSAGE);
    View v = inflater.inflate(R.layout.layout_text, container, false);
    TextView messageTextView = (TextView)v.findViewById(R.id.textView);
    messageTextView.setText(message);
    return v;
}

}

public class MyFragmentButton extends Fragment {
    public static final String EXTRA_MESSAGE = "EXTRA_MESSAGE";

    public static final MyFragmentButton newInstance(String message)
    {
        MyFragmentButton f = new MyFragmentButton();
        Bundle bdl = new Bundle(1);
        bdl.putString(EXTRA_MESSAGE, message);
        f.setArguments(bdl);
        return f;
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        String message = getArguments().getString(EXTRA_MESSAGE);
        View v = inflater.inflate(R.layout.layout_button, container, false);
        TextView messageTextView = (TextView)v.findViewById(R.id.textView);
        messageTextView.setText(message);

        return v;
    }
}

我有三个 XML 文件:

主要布局:

    <android.support.v4.view.ViewPager
        android:id="@+id/viewpager"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent" />
</RelativeLayout>

按钮布局

    <TextView
        android:id="@+id/textView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true"
        android:textAppearance="?android:attr/textAppearanceLarge" />

    <Button
        android:id="@+id/button"
        android:width="100dp"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Click here"/>
</RelativeLayout>

文本视图布局

<?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:orientation="vertical" >

    <TextView
        android:id="@+id/textView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true"
        android:textAppearance="?android:attr/textAppearanceLarge" />

</RelativeLayout>

现在,当我从主程序调用它们以使用以下代码显示滑动窗口时,它运行良好。

公共类 PageViewActivity 扩展 FragmentActivity {

MyPageAdapter pageAdapter;
Button btn;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_page_view);

    List<Fragment> fragments = getFragments();
    pageAdapter = new MyPageAdapter(getSupportFragmentManager(), fragments);

    ViewPager pager = (ViewPager)findViewById(R.id.viewpager);
    pager.setAdapter(pageAdapter);

    //btn  = (Button) findViewById(R.id.button);
    //btn.setText("Hello World!!!");
}

private List<Fragment> getFragments(){
    List<Fragment> fList = new ArrayList<Fragment>();

    fList.add(MyFragmentButton.newInstance("Fragment 1"));
    fList.add(MyFragmentText.newInstance("Fragment 2"));

    return fList;
}

private class MyPageAdapter extends FragmentPagerAdapter {
    private List<Fragment> fragments;

    public MyPageAdapter(FragmentManager fm, List<Fragment> fragments) {
        super(fm);
        this.fragments = fragments;
    }
    @Override
    public Fragment getItem(int position) {
        return this.fragments.get(position);
    }

    @Override
    public int getCount() {
        return this.fragments.size();
    }
}

}

但是如果我申请了

btn  = (Button) findViewById(R.id.button);
        btn.setText("Hello World!!!");

它给了我空指针异常。我想这是因为当我调用 btn 时,thet lauyout 没有膨胀。如果有人能提出任何解决方案,我将不胜感激。

【问题讨论】:

  • 你能准确显示activity_page_view xml布局吗?
  • Mightyseal,我刚刚更新了我的帖子以显示三个布局文件

标签: android layout nullpointerexception layout-inflater


【解决方案1】:

据我所知activity_page_view 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.support.v4.view.ViewPager
        android:id="@+id/viewpager"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent" />
</RelativeLayout>

这个布局没有带按钮的元素,这就是为什么btn = (Button) findViewById(R.id.button); 返回nullbtn.setText("Hello World!!!"); 产生NPE

【讨论】:

  • 有没有办法从其他布局访问控件?我的意思是膨胀其他布局并访问控件
  • @MKS 有,您可以在片段中创建方法,该方法将返回您想要的控制权,但这个想法听起来不太好。基本上,当组件(活动或片段)封装它的控件时会更好。如果您想从另一个组件更改控件状态,您应该以某种方式发送事件哪个组件捕获并更新控件。您可以使用广播或像 otto 这样的事件总线。如果您需要最简单的解决方案 - 只需为此目的创建您自己的界面。
猜你喜欢
  • 2018-08-11
  • 1970-01-01
  • 1970-01-01
  • 2018-01-07
  • 2016-01-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多