替换片段android时出现NullPointerException

时间:2016-04-09 05:04:58

标签: java android android-fragments nullpointerexception fragmentmanager

这不是重复的!我已经阅读了另一个问题,但它提供的答案没有用(我刚引用它时我刚刚宣布了我的对象;没有办法了null)我不确定stackoverflow是如何工作的,但是我不愿意看到这被误解了。

经过几个小时的谷歌搜索并梳理我的代码后,我感到很困惑。

我正在尝试用ScriptFragment代码中的另一个片段(ScenesFragment)替换一个片段(ScriptsFragment)。当片段管理器尝试替换时,它会抛出错误:

  

java.lang.RuntimeException:无法启动活动ComponentInfo {com.alecdb.lineburner / com.alecdb.lineburner.ScriptsActivity}:java.lang.NullPointerException:尝试调用虚拟方法'android.content.res.Resources android空对象引用上的.content.Context.getResources()'

任何帮助都会非常感激 - 我完全被难过了。

这是应该用SceneFragment替换ScriptFragment的部分:

@Override
       public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

            FragmentManager fragmentManager = getFragmentManager();
            FragmentTransaction transaction = fragmentManager.beginTransaction();
            transaction.replace(R.id.fragment_container2,new ScenesFragment());
            transaction.addToBackStack(null);
            transaction.commit();

        }`

这里是SceneFragment(减去我在其他地方测试过的两种方法):

public class ScenesFragment extends Fragment {
public List<String> results = new ArrayList<>();
public SQLiteDatabase newDB;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    // Add this line in order for this fragment to handle menu events.
    setHasOptionsMenu(true);

}

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

    View rootView = inflater.inflate(R.layout.fragement_scenes, container, false);

    listView = new ListView(null);
    listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

        }
    });
    return rootView;
}

这是我最初添加ScriptFragment的MainActivity(ScriptsActivity):

package com.alecdb.lineburner;

import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;

public class ScriptsActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_scripts);
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);

    FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
    fab.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
                    .setAction("Action", null).show();
        }
    });

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

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.menu_scripts, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();

    //noinspection SimplifiableIfStatement
    if (id == R.id.action_settings) {
        return true;
    }

    return super.onOptionsItemSelected(item);
}
}

这是Activity的XML:

<?xml version="1.0" encoding="utf-8"?>

                                            >
<android.support.design.widget.AppBarLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:theme="@style/AppTheme.AppBarOverlay">

    <android.support.v7.widget.Toolbar
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize"
        android:background="?attr/colorPrimary"
        app:popupTheme="@style/AppTheme.PopupOverlay" />

</android.support.design.widget.AppBarLayout>



<android.support.design.widget.FloatingActionButton
    android:id="@+id/fab"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="bottom|end"
    android:layout_margin="@dimen/fab_margin"
    android:src="@android:drawable/ic_dialog_email" />

<FrameLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/fragment_container"
    class="com.alecdb.lineburner.ScriptsFragment">

</FrameLayout>
<FrameLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/fragment_container2"
    class="com.alecdb.lineburner.ScenesFragment">

</FrameLayout>

和SceneFragment的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: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.alecdb.lineburner.ScenesFragment"
            >
<ListView
    android:id="@+id/listView2"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    />

<TextView
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:text="TESTING TESTING TESTING!!!!"/>

最后,ScriptFragment的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:paddingBottom="@dimen/activity_vertical_margin"
                android:paddingLeft="@dimen/activity_horizontal_margin"
                android:paddingRight="@dimen/activity_horizontal_margin"
                android:paddingTop="@dimen/activity_vertical_margin"
                android:id="@+id/scripts_fragment"
                tools:context="com.alecdb.lineburner.ScriptsFragment"
                tools:showIn="@layout/activity_scripts">

    <ListView
        android:id="@+id/listView1"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        />

</RelativeLayout>

我的宣言:

<?xml version="1.0" encoding="utf-8"?>

<application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:supportsRtl="true"
    android:theme="@style/AppTheme">
    <activity
        android:name=".ScriptsActivity"
        android:label="@string/app_name"
        android:theme="@style/AppTheme.NoActionBar">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>

    <provider
        android:authorities="com.alecdb.lineburner.app"
        android:name="com.alecdb.lineburner.data.LineBurnerProvider"
        />
</application>

再次感谢,

Alec Burns

P.S。这是我在StackOverflow上的第一篇文章。如果我做错了,请告诉我。

1 个答案:

答案 0 :(得分:0)

ScenesFragment 中。你没有宣布列表视图。导致异常和所有。