运行代码时,我在logcat中始终收到此错误:java.lang.NullPointerException:尝试在空对象引用上调用虚拟方法'void android.widget.TextView.setVisibility(int)'
在调试时,我收到以下消息: 添加的EditText不是TextInputEditText。请改用该类。
我一直在寻找几天,但是没有运气。我仍在学习使用Android Studio的工作,因此希望能为解决我的问题提供任何帮助!
我的“ GroupsFragment.java”:
import { NgModule } from '@angular/core';
import { IonicPageModule } from 'ionic-angular';
import { UsersPage } from './users';
import { AddButton } from '../../components/add-button.component';
@NgModule({
declarations: [
UsersPage,
AddButton
],
imports: [
IonicPageModule.forChild(UsersPage),
],
exports: [
AddButton
]
})
export class UsersPageModule { }
我的“ groups_lay_out.xml”:
package com.example.flow.displayClasses.GroupScreens;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import com.amigold.fundapter.BindDictionary;
import com.amigold.fundapter.FunDapter;
import com.amigold.fundapter.extractors.StringExtractor;
import com.example.flow.R;
import com.amigold.fundapter.BindDictionary;
import com.amigold.fundapter.FunDapter;
import com.amigold.fundapter.extractors.StringExtractor;
import android.content.Intent;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.BaseAdapter;
import android.widget.EditText;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import com.example.flow.classes.Friends;
import com.example.flow.classes.Group;
import com.example.flow.classes.User;
import com.example.flow.displayClasses.FriendsScreen.FriendsFragment;
import android.support.design.widget.TextInputEditText;
import android.support.design.widget.TextInputLayout;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import android.view.MenuItem;
import android.support.annotation.Nullable;
import android.widget.Toast;
public class GroupsFragment extends Fragment {
private final String TAG = this.getClass().getSimpleName();
private static final String ARG_PARAM1 = "param1";
private static final String ARG_PARAM2 = "param2";
private String mParam1;
private String mParam2;
ArrayList<Group> groups = new ArrayList<Group>();
public GroupsFragment() {
// Required empty public constructor
}
public static GroupsFragment newInstance(String param1, String param2) {
GroupsFragment fragment = new GroupsFragment();
Bundle args = new Bundle();
args.putString(ARG_PARAM1, param1);
args.putString(ARG_PARAM2, param2);
fragment.setArguments(args);
return fragment;
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
super.onCreate(savedInstanceState);
if (getArguments() != null) {
mParam1 = getArguments().getString(ARG_PARAM1);
mParam2 = getArguments().getString(ARG_PARAM2);
}
}
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View RootView = inflater.inflate(R.layout.fragment_groups, container, false);
// Hardcoded for testing
User u1 = new User("Bram", "bram.de.plekker@student.ehb.be", "1234", "Captain Plek");
User u2 = new User("Bram1", "bram.de.plekker@student.ehb.be", "1234", "Captain Plek");
User u3 = new User("Bram2", "bram.de.plekker@student.ehb.be", "1234", "Captain Plek");
User u4 = new User("Bram3", "bram.de.plekker@student.ehb.be", "1234", "Captain Plek");
User u5 = new User("Bram4", "bram.de.plekker@student.ehb.be", "1234", "Captain Plek");
User u6 = new User("Bram5", "bram.de.plekker@student.ehb.be", "1234", "Captain Plek");
User u7 = new User("Bram6", "bram.de.plekker@student.ehb.be", "1234", "Captain Plek");
User u8 = new User("Bram7", "bram.de.plekker@student.ehb.be", "1234", "Captain Plek");
User u9 = new User("Bram8", "bram.de.plekker@student.ehb.be", "1234", "Captain Plek");
User u10 = new User("Bram9", "bram.de.plekker@student.ehb.be", "1234", "Captain Plek");
User u11 = new User("Bram10", "bram.de.plekker@student.ehb.be", "1234", "Captain Plek");
User u12 = new User("Bram11", "bram.de.plekker@student.ehb.be", "1234", "Captain Plek");
User u13 = new User("Bram12", "bram.de.plekker@student.ehb.be", "1234", "Captain Plek");
User u14 = new User("Bram13", "bram.de.plekker@student.ehb.be", "1234", "Captain Plek");
User u15 = new User("Bram14", "bram.de.plekker@student.ehb.be", "1234", "Captain Plek");
User u16 = new User("Bram15", "bram.de.plekker@student.ehb.be", "1234", "Captain Plek");
User u17 = new User("Bram16", "bram.de.plekker@student.ehb.be", "1234", "Captain Plek");
User u18 = new User("Bram17", "bram.de.plekker@student.ehb.be", "1234", "Captain Plek");
ArrayList<User> ug1 = new ArrayList<User>();
ug1.add(u1);
ug1.add(u2);
ug1.add(u3);
ArrayList<User> ug2 = new ArrayList<User>();
ug2.add(u4);
ug2.add(u5);
ug2.add(u6);
//Collections.addAll(ug1, u4, u5, u6);
ArrayList<User> ug3 = new ArrayList<User>();
ug3.add(u1);
ug3.add(u7);
ug3.add(u8);
ug3.add(u9);
//Collections.addAll(ug1, u7, u8, u9);
ArrayList<User> ug4 = new ArrayList<User>();
Collections.addAll(ug1, u10, u11, u12);
ArrayList<User> ug5 = new ArrayList<User>();
Collections.addAll(ug1, u13, u14, u15);
ArrayList<User> ug6 = new ArrayList<User>();
Collections.addAll(ug1, u14, u5, u6);
ArrayList<User> ug7 = new ArrayList<User>();
Collections.addAll(ug1, u4, u10, u6, u18, u17);
ArrayList<User> ug8 = new ArrayList<User>();
Collections.addAll(ug1, u4, u5, u6);
Group g1 = new Group(1, ug1, u1, "budies", u1, "date", true);
Group g8 = new Group(2, ug1, u1, "budies", u1, "date", false);
Group g9 = new Group(3, ug1, u1, "budies", u1, "date", true);
Group g10 = new Group(4, ug1, u1, "budies", u1, "date", false);
Group g11 = new Group(5, ug1, u1, "budies", u1, "date", true);
Group g12 = new Group(6, ug1, u1, "budies", u1, "date", false);
Group g13 = new Group(7, ug1, u1, "budies", u1, "date", false);
Group g14 = new Group(8, ug1, u1, "budies", u1, "date", false);
Group g15 = new Group(9, ug1, u1, "budies", u1, "date", true);
Group g16 = new Group(10, ug1, u1, "budies", u1, "date", true);
Group g17 = new Group(11, ug1, u1, "budies", u1, "date", true);
Group g18 = new Group(12, ug1, u1, "budies", u1, "date", false);
Collections.addAll(groups, g1, g8, g9, g10, g11, g12, g13, g14, g15, g16, g17, g18);
/*
BindDictionary<Group> dictionary = new BindDictionary<>();
dictionary.addStringField(R.id.blabla, new StringExtractor<Group>() {
@Override
public String getStringValue(Group groupname, int position) {
return groupname.getName();
}
});
dictionary.addStringField(R.id.prepaidIcon, new StringExtractor<Group>() {
@Override
public String getStringValue(Group prepaid, int position) {
return "" + prepaid.isPrepaid();
}
});
FunDapter adapter = new FunDapter(GroupsFragment.this.getActivity(), groups, R.layout.groups_layout, dictionary);
*/
//getActivity() returns the Activity hosting the Fragment,
// while getView() returns the view you inflated and returned by onCreateView
//may cause nullpointer exception
ListView list = (ListView)RootView.findViewById(R.id.listView);
CustomAdapter adapter = new CustomAdapter();
list.setAdapter(adapter);
list.setOnItemClickListener(new AdapterView.OnItemClickListener()
{
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id)
{
Group selectedProduct = groups.get(position);
Toast.makeText(GroupsFragment.this.getActivity(), selectedProduct.getName(), Toast.LENGTH_SHORT).show();
}
});
return RootView;
}
class CustomAdapter extends BaseAdapter {
int index;
@Override
public int getCount() {
return groups.size();
}
@Override
public Object getItem(int position) {
return null;
}
@Override
public long getItemId(int position) {
return 0;
}
public View getView(int i, View view, ViewGroup viewgroup) {
if (groups.get(i).isPrepaid()) {
view = getLayoutInflater().inflate(R.layout.groups_prepaid_layout, null);
} else {
view = getLayoutInflater().inflate(R.layout.groups_layout, null);
}
TextView textView = view.findViewById(R.id.blabla);
ImageView image = view.findViewById(R.id.member);
String t = groups.get(i).getName();
textView.setText(t);//nullpointer exception
return view;
}
}
}
我的“ fragment_groups.xml”:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/blabla"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="20dp"
android:text="@string/textview"
android:textAppearance="?android:attr/textAppearanceLarge" />
<ImageView
android:id="@+id/member"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/blabla"
android:layout_marginStart="20dp"
android:src="@drawable/ic_member" />
</RelativeLayout>
我的logcat:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/groupsLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".displayClasses.GroupScreens.GroupsFragment"
>
<TextView
android:id="@+id/groupName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="10dp"
android:text="@string/groups"
android:textColor="#428EC4"
android:textSize="30sp"
android:textStyle="bold" />
<EditText
android:id="@+id/search_bar"
android:layout_width="wrap_content"
android:layout_height="40dp"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:layout_alignParentEnd="true"
android:layout_marginStart="20dp"
android:layout_marginTop="77dp"
android:layout_marginEnd="20dp"
android:background="@drawable/ic_bordersearch"
android:ems="10"
android:gravity="center"
android:hint="@string/search_text" />
<ImageButton
android:id="@+id/searchButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignTop="@+id/search_bar"
android:layout_alignEnd="@+id/search_bar"
android:layout_alignBottom="@+id/search_bar"
android:layout_marginTop="5dp"
android:layout_marginEnd="5dp"
android:layout_marginBottom="5dp"
android:background="@android:color/transparent"
android:src="@drawable/ic_search_icon" />
<ImageButton
android:id="@+id/plusButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:layout_alignParentBottom="true"
android:layout_marginStart="298dp"
android:layout_marginEnd="25dp"
android:layout_marginBottom="55dp"
android:background="@android:color/transparent"
android:src="@drawable/ic_plussign" />
<RelativeLayout
android:id="@+id/linearLayout"
android:layout_width="wrap_content"
android:layout_height="386dp"
android:layout_below="@+id/search_bar"
android:layout_alignParentStart="true"
android:layout_alignParentEnd="true"
android:layout_alignParentBottom="true"
android:layout_marginStart="20dp"
android:layout_marginTop="19dp"
android:layout_marginEnd="20dp"
android:layout_marginBottom="50dp"
android:orientation="vertical">
<ListView
android:id="@+id/listView"
android:layout_width="match_parent"
android:layout_height="match_parent"></ListView>
</RelativeLayout>
</RelativeLayout>
我的调试器:
Process: com.example.flow, PID: 16906
java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.TextView.setText(java.lang.CharSequence)' on a null object reference
at com.example.flow.displayClasses.GroupScreens.GroupsFragment$CustomAdapter.getView(GroupsFragment.java:237)
at android.widget.AbsListView.obtainView(AbsListView.java:2366)
at android.widget.ListView.makeAndAddView(ListView.java:2052)
at android.widget.ListView.fillDown(ListView.java:786)
at android.widget.ListView.fillFromTop(ListView.java:847)
at android.widget.ListView.layoutChildren(ListView.java:1826)
at android.widget.AbsListView.onLayout(AbsListView.java:2165)
at android.view.View.layout(View.java:20672)
at android.view.ViewGroup.layout(ViewGroup.java:6194)
at android.widget.RelativeLayout.onLayout(RelativeLayout.java:1083)
at android.view.View.layout(View.java:20672)
at android.view.ViewGroup.layout(ViewGroup.java:6194)
at android.widget.RelativeLayout.onLayout(RelativeLayout.java:1083)
at android.view.View.layout(View.java:20672)
at android.view.ViewGroup.layout(ViewGroup.java:6194)
at android.widget.RelativeLayout.onLayout(RelativeLayout.java:1083)
at android.view.View.layout(View.java:20672)
at android.view.ViewGroup.layout(ViewGroup.java:6194)
at android.support.design.widget.HeaderScrollingViewBehavior.layoutChild(HeaderScrollingViewBehavior.java:142)
at android.support.design.widget.ViewOffsetBehavior.onLayoutChild(ViewOffsetBehavior.java:41)
at android.support.design.widget.AppBarLayout$ScrollingViewBehavior.onLayoutChild(AppBarLayout.java:1556)
at android.support.design.widget.CoordinatorLayout.onLayout(CoordinatorLayout.java:888)
at android.view.View.layout(View.java:20672)
at android.view.ViewGroup.layout(ViewGroup.java:6194)
at android.support.v4.widget.DrawerLayout.onLayout(DrawerLayout.java:1231)
at android.view.View.layout(View.java:20672)
at android.view.ViewGroup.layout(ViewGroup.java:6194)
at android.widget.RelativeLayout.onLayout(RelativeLayout.java:1083)
at android.view.View.layout(View.java:20672)
at android.view.ViewGroup.layout(ViewGroup.java:6194)
at android.widget.FrameLayout.layoutChildren(FrameLayout.java:323)
at android.widget.FrameLayout.onLayout(FrameLayout.java:261)
at android.view.View.layout(View.java:20672)
at android.view.ViewGroup.layout(ViewGroup.java:6194)
at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1812)
at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1656)
at android.widget.LinearLayout.onLayout(LinearLayout.java:1565)
at android.view.View.layout(View.java:20672)
at android.view.ViewGroup.layout(ViewGroup.java:6194)
at android.widget.FrameLayout.layoutChildren(FrameLayout.java:323)
at android.widget.FrameLayout.onLayout(FrameLayout.java:261)
at android.view.View.layout(View.java:20672)
at android.view.ViewGroup.layout(ViewGroup.java:6194)
at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1812)
at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1656)
at android.widget.LinearLayout.onLayout(LinearLayout.java:1565)
at android.view.View.layout(View.java:20672)
at android.view.ViewGroup.layout(ViewGroup.java:6194)
at android.widget.FrameLayout.layoutChildren(FrameLayout.java:323)
at android.widget.FrameLayout.onLayout(FrameLayout.java:261)
at com.android.internal.policy.DecorView.onLayout(DecorView.java:753)
at android.view.View.layout(View.java:20672)
at android.view.ViewGroup.layout(ViewGroup.java:6194)
at android.view.ViewRootImpl.performLayout(ViewRootImpl.java:2792)
at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:2319)
at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1460)
at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:7183)
at android.view.Choreographer$CallbackRecord.run(Choreographer.java:949)
at android.view.Choreographer.doCallbacks(Choreographer.java:761)
2018-11-21 14:01:06.799 16906-16906/com.example.flow E/AndroidRuntime: at android.view.Choreographer.doFrame(Choreographer.java:696)
at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:935)
at android.os.Handler.handleCallback(Handler.java:873)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:193)
at android.app.ActivityThread.main(ActivityThread.java:6669)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858)
答案 0 :(得分:0)
您收到此错误,因为在尝试设置文本之前尚未初始化TextView。在当前块之外声明它,并在onCreateView中初始化。然后,您可以在代码中的任意位置设置文本。
例如,
TextView textView;
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View RootView = inflater.inflate(R.layout.fragment_groups, container, false);
textView = view.findViewById(R.id.blabla);
}