我有一个简单的Android应用程序,我主要是为了练习。它包含MainActivity和2个片段,一个使用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)
如果我理解正确,这是由我的应用尝试在创建视图之前填充ListView引起的。在填充函数触发之前,我无法弄清楚如何创建视图。我该怎么办?
我的主要活动代码:
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片段代码:
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
更新
我将我的片段改为:
@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片段:
<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" />
答案 0 :(得分:3)
最快可以开始对片段中的视图层次结构进行更改,它位于onCreateView()中。您可以遵循以下模式:
@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;
}
答案 1 :(得分:2)
问题是getView()
返回null
,因为您尚未设置片段的视图。 (您仍然在onCreateView()
中创建它。)请注意,您已经有一个名为View
的{{1}}引用。实际上,您只是将XML布局夸大了这个view
对象。改为使用它:
View
答案 2 :(得分:0)
您应该在片段中的makeListViewArray()
中调用onViewCreated()
,此时视图已创建(因为您可能已经从方法的名称中找到了它)
此外,在您的活动中,请致电Budynki.newInstance();
而不是new Budynki().newInstance();
; newInstace
是一种静态方法,因此您无需实例化您的类即可调用它。同样适用于Mapa
片段