gpt4 book ai didi

java - 替换fragment android时出现NullPointerException

转载 作者:太空宇宙 更新时间:2023-11-04 12:44:08 28 4
gpt4 key购买 nike

这不是重复的!我已经阅读了另一个问题,但它提供的答案不起作用(我刚刚在引用它时声明了我的对象;它不可能为空)我还不确定 stackoverflow 是如何工作的,但我不想看到它因误解而被锁定。

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

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

java.lang.RuntimeException: Unable to start activity ComponentInfo{com.alecdb.lineburner/com.alecdb.lineburner.ScriptsActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.res.Resources android.content.Context.getResources()' on a null object reference

任何帮助将不胜感激 - 我完全被难住了。

这是应该用 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;
}

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

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>

还有我的 list :

<?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>

再次感谢,

亚历克·伯恩斯

附注这是我在 StackOverflow 上的第一篇文章。如果我做错了什么,请告诉我。

最佳答案

场景 fragment 中。你没有声明 ListView 。这导致了异常和所有。

关于java - 替换fragment android时出现NullPointerException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36512952/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com