【问题标题】:onCreate Null Pointer Exception [duplicate]onCreate空指针异常[重复]
【发布时间】:2014-07-28 20:07:14
【问题描述】:

我不明白为什么这段代码没有运行:

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_matchpage);

    SeekBar sb1 = (SeekBar) findViewById(R.id.seek1);
            sb1.setEnabled(false);

    if (savedInstanceState == null) {
        getSupportFragmentManager().beginTransaction()
                .add(R.id.container, new PlaceholderFragment()).commit();
    }
}

sb1 将为空。 为什么?

【问题讨论】:

  • seek1 不在activity_matchpage 布局中。可能它在片段布局中。 stackoverflow.com/questions/23653778/…
  • 您有 ID 为 seek1 的组件吗?还有什么错误?
  • 不,seek1 在匹配页中。
  • 当我在按钮单击中运行此代码时,它运行:SeekBar sb1 = (SeekBar) findViewById(R.id.seek1); sb1.setEnabled(false);
  • 为匹配页播种 xml 代码

标签: android xml


【解决方案1】:

我将尝试根据我的经验回答一件事,即错误出现在 SeekBar sb1 = (SeekBar) findViewById(R.id.seek1); 但我注意到 android 从底部编译代码所以可能发生原因在上面或其他地方所以我建议替换以这种方式编写代码:-

  1. protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_matchpage);
    }
    
  2. 如果它执行了,那么只需在setContentview下面添加SeekBar sb1 = (SeekBar) findViewById(R.id.seek1);

它有点大,但由于你没有提供 xml,我们无法确定它的包问题还是其他问题,所以我试图去真正原始和基本......

希望对你有帮助。

【讨论】:

    【解决方案2】:

    您可能正在使用Fragment。您的代码中应该有一个名为PlaceHolderFragment 的类,它有一个onCreateView 方法。

    在该项目中,您有两个 xml 布局,一个名为 activity_matchpage,另一个用于片段,fragment_main。您需要修改后者并像这样保留第一个:

    <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/container"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="youor.packaque.MainActivity"
    tools:ignore="MergeRootFrame" />
    

    fragment_main 中,您声明您的视图。

    <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="com.tformacion.finale.MainActivity$PlaceholderFragment" >
    
            <!-- Your Views here -->
    
    </RelativeLayout>
    

    然后,在您的 Java 代码中,在 PlaceHolderFragment 中,在 onCreateView 内部:

        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                Bundle savedInstanceState) {
            View rootView = inflater.inflate(R.layout.fragment_main, container, false);
    
            seekBar = (seekBar) rootView.findViewById(R.id.yourSeekBarId);
    
        }
    

    如果您想摆脱片段,您只需在 activity_matchpage 中创建您的 XML 并且不添加片段,因此,删除它:

    if (savedInstanceState == null) {
        getSupportFragmentManager().beginTransaction()
                .add(R.id.container, new PlaceholderFragment()).commit();
    }
    

    【讨论】:

    • findViewById 方法不能从 onCreateView() 调用。 “无法从类型 Activity 对非静态方法 findViewById(int) 进行静态引用。”
    • 你需要从rootView rootView.findViewById调用它
    猜你喜欢
    • 1970-01-01
    • 2014-02-04
    • 2016-04-28
    • 1970-01-01
    • 2014-10-16
    • 1970-01-01
    相关资源
    最近更新 更多