gpt4 book ai didi

java - 在显示内容之前初始化 Android 中的 fragment

转载 作者:行者123 更新时间:2023-12-01 16:53:53 24 4
gpt4 key购买 nike

我有一个简单的 Android 应用程序,主要用于练习。它包含 MainActivity 和 2 个 fragment ,一个带有 ImageView,第二个带有 Listview。我尝试使用 ArrayAdapter 填充 ListView,但目前遇到此错误:

FATAL EXCEPTION: main
Process: com.ikurek.pwr, PID: 2792
java.lang.NullPointerException: Attempt to invoke virtual method 'android.view.View android.view.View.findViewById(int)' on a null object reference
at com.ikurek.pwr.Budynki.onCreateView(Budynki.java:40)
at android.support.v4.app.Fragment.performCreateView(Fragment.java:1962)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1067)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1248)
at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:738)
at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1613)
at android.support.v4.app.FragmentManagerImpl$1.run(FragmentManager.java:517)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5417)

如果我理解正确,这是由我的应用程序在创建 View 之前尝试填充 ListView 引起的。我不知道如何在填充函数触发之前创建 View 。我该怎么做?

我的MainActivity代码:

public class MainActivity extends AppCompatActivity
implements NavigationView.OnNavigationItemSelectedListener {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
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();
}
});

DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
drawer.setDrawerListener(toggle);
toggle.syncState();

NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
navigationView.setNavigationItemSelectedListener(this);


}

@Override
public void onBackPressed() {
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
if (drawer.isDrawerOpen(GravityCompat.START)) {
drawer.closeDrawer(GravityCompat.START);
} else {
super.onBackPressed();
}
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main, menu);
return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();


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

return super.onOptionsItemSelected(item);
}

@SuppressWarnings("StatementWithEmptyBody")

@Override
public boolean onNavigationItemSelected(MenuItem item) {
// Handle navigation view item clicks here.
int id = item.getItemId();
FragmentManager fragmentManager = getSupportFragmentManager();

//Obsługa poszczególych opcji menu
if (id == R.id.nav_Budynki) {

Budynki fragment = null;
fragment = new Budynki().newInstance();
fragmentManager.beginTransaction().replace(R.id.startScreen, fragment).commit();

} else if (id == R.id.nav_Mapa) {

Mapa fragment = null;
fragment = new Mapa().newInstance();
fragmentManager.beginTransaction().replace(R.id.startScreen, fragment).commit();

} else if (id == R.id.nav_slideshow) {

} else if (id == R.id.nav_manage) {

} else if (id == R.id.nav_share) {

} else if (id == R.id.nav_send) {

}



DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
drawer.closeDrawer(GravityCompat.START);
return true;
}

和我的ListView fragment 代码:

public class Budynki extends Fragment {

public Budynki() {
}


public static Budynki newInstance() {
Budynki fragment = new Budynki();
return fragment;
}

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
return inflater.inflate(R.layout.fragment_budynki, container, false);
}

public void makeListViewArray()
{
String[] arrayBudynki ={"A","B","C"};
ArrayAdapter<String> adapter = new ArrayAdapter <String> (getActivity(), android.R.layout.simple_list_item_1, arrayBudynki);
ListView lista= (ListView) getView().findViewById(R.id.listView);
lista.setAdapter(adapter);
}

我试图自己找到解决方案,但恐怕找不到。我尝试在 OnCreate() 和 OnCreateView() 中调用 makeListViewArray() ,但没有成功。请帮忙:-D

更新:

我将 fragment 更改为:

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_budynki, container, false);
// make changes to view and its children here
String[] arrayBudynki ={"A","B","C"};
ArrayAdapter<String> adapter = new ArrayAdapter <String> (getActivity(), android.R.layout.simple_list_item_1, arrayBudynki);
ListView lista= (ListView) getView().findViewById(R.id.listView);
lista.setAdapter(adapter);
return view;
}

我仍然遇到同样的错误,请问有什么想法吗?

更新:

Budynki.xml fragment :

<FrameLayout 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"
tools:context="com.ikurek.pwr.Budynki">

<ListView
android:layout_width="wrap_content"
android:layout_height="496dp"
android:id="@+id/listView"
android:layout_gravity="center_horizontal|top" />

最佳答案

最快可以在 onCreateView() 内部开始更改 fragment 中的 View 层次结构。您可以遵循以下模式:

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_budynki, container, false);
// make changes to view and its children here
return view;
}

关于java - 在显示内容之前初始化 Android 中的 fragment ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35439368/

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