当我尝试运行必须使用可扩展列表视图项的片段时,我一直得到一个空指针异常。我猜它与片段有关,因为类似的代码在一个活动上运行。
这是错误(Logcat)消息:
11-02 14:11:27.136 12947-12947/com.example.vhuhwavho.ctistudentportal E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.vhuhwavho.ctistudentportal, PID: 12947
java.lang.NullPointerException
at com.example.vhuhwavho.ctistudentportal.ForYouFragment.onCreateView(ForYouFragment.java:117)
at android.app.Fragment.performCreateView(Fragment.java:1700)
at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:890)
at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:1062)
at android.app.BackStackRecord.run(BackStackRecord.java:684)
at android.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1453)
at android.app.FragmentManagerImpl$1.run(FragmentManager.java:443)
at android.os.Handler.handleCallback(Handler.java:733)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:146)
at android.app.ActivityThread.main(ActivityThread.java:5487)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1283)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1099)
at dalvik.system.NativeStart.main(Native Method)
这是片段代码:
package com.example.vhuhwavho.ctistudentportal;
import android.app.Fragment;
import android.content.Context;
import android.content.SharedPreferences;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Typeface;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v4.content.res.ResourcesCompat;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.BaseExpandableListAdapter;
import android.widget.Button;
import android.widget.ExpandableListAdapter;
import android.widget.ExpandableListView;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.ProgressBar;
import android.widget.TextView;
import android.widget.Toast;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import static android.content.Context.MODE_PRIVATE;
/**
* Created by Vhuhwavho on 2017/10/31.
*/
public class ForYouFragment extends Fragment {
// To keep track of the counter, we need to add a global variable to the project,
// along with a key for saving and restoring.
static final String KEY_STUDENT_NUMBER = "studentNumber";
String studentNumber = "";
private static final int CODE_GET_REQUEST = 1024;
private static final int CODE_POST_REQUEST = 1025;
private Button mSubmit;
private Context mContext;
private LayoutInflater mInflater;
private View view;
private List < String > listHeadings;
private List < String > image_urls;
private List < Bitmap > images;
private List < String > messages;
private LinkedHashMap < String, GroupInfo > gMessages = new LinkedHashMap < String, GroupInfo > ();
private ArrayList < GroupInfo > list = new ArrayList < GroupInfo > ();
private ExpandableListAdapter listAdapter;
private ExpandableListView simpleExpandableListView;
//Connectivity manage instance
private ConnectivityManager mConnMgr;
// Image view reference
private ImageView imageView;
int imageCounter = 0, imageCounterSize = 0;
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
view = inflater.inflate(R.layout.for_you_fragment, container, false);
String extraStr;
try {
extraStr = getArguments().getString("studentNumber");
Log.e("try studentNumber", "tried student number");
} catch (NullPointerException e) {
studentNumber = "PT2014-1282";
Log.e("catch studentNumber", "catched student number");
}
requestForYou();
//get reference of the ExpandableListView
simpleExpandableListView = (ExpandableListView) this.getActivity().findViewById(R.id.simpleExpandableListView);
if ( simpleExpandableListView == null )
Log.d("Null or Not", "simpleExpandableList == null");
// create the adapter by passing your ArrayList data
listAdapter = new ExpandableListAdapter (this.getActivity().getApplicationContext(), list);
// attach the adapter to the expandable list view
simpleExpandableListView.setAdapter(listAdapter);
//expand all the Groups
expandAll();
// setOnChildClickListener listener for child row click
simpleExpandableListView.setOnChildClickListener(new ExpandableListView.OnChildClickListener() {
@Override
public boolean onChildClick(ExpandableListView parent, View v, int groupPosition, int childPosition, long id) {
//get the group header
GroupInfo headerInfo = list.get(groupPosition);
//get the child info
ChildInfo detailInfo = headerInfo.getHeadingList().get(childPosition);
//display it or do something with it
Toast.makeText(getActivity(), " Clicked on :: " + headerInfo.getHeading()
+ "/" , Toast.LENGTH_LONG).show();
return false;
}
});
// setOnGroupClickListener listener for group heading click
simpleExpandableListView.setOnGroupClickListener(new ExpandableListView.OnGroupClickListener() {
@Override
public boolean onGroupClick(ExpandableListView parent, View v, int groupPosition, long id) {
//get the group header
GroupInfo headerInfo = list.get(groupPosition);
//display it or do something with it
return false;
}
});
listHeadings = new ArrayList<>();
messages = new ArrayList<>();
image_urls = new ArrayList<>();
return view;
} // end of onCreateView method
// To receive notifications of application state change, 2 methods are needed
// the onSaveInstanceState() and onRestoreInstanceState() methods
@Override
public void onSaveInstanceState (Bundle outState) {
super.onSaveInstanceState(outState);
outState.putString(KEY_STUDENT_NUMBER, studentNumber);
}
@Override
public void onActivityCreated (Bundle savedInstanceState) {
super.onActivityCreated (savedInstanceState);
if (savedInstanceState != null) {
studentNumber = savedInstanceState.getString(KEY_STUDENT_NUMBER);
}
}
// save the data before the activity closes
@Override
public void onPause () {
super.onPause();
SharedPreferences settings = this.getActivity().getPreferences(MODE_PRIVATE);
SharedPreferences.Editor editor = settings.edit();
editor.putString(KEY_STUDENT_NUMBER, studentNumber);
editor.commit();
}
//method to expand all groups
private void expandAll() {
int count = listAdapter.getGroupCount();
for (int i = 0; i < count; i++){
simpleExpandableListView.expandGroup(i);
}
}
//method to collapse all groups
private void collapseAll() {
int count = listAdapter.getGroupCount();
for (int i = 0; i < count; i++){
simpleExpandableListView.collapseGroup(i);
}
}
private void requestForYou () {
PerformNetworkRequest request = new PerformNetworkRequest(API.URL_REQUEST_FOR_YOU, CODE_GET_REQUEST);
request.execute();
}
private void refreshForYou( JSONArray forYou) throws JSONException {
for ( int i = 0; i < forYou.length(); i++ ) {
JSONObject whats_happening = forYou.getJSONObject(i);
String heading = whats_happening.getString( "heading" );
String image_url = whats_happening.getString( "url" );
String message = whats_happening.getString( "message" );
listHeadings.add ( heading ) ;
messages.add ( message );
image_urls.add ( image_url );
Log.d("ForYouFragment", "refreshForYou");
}
imageCounterSize = listHeadings.size();
if ( mConnMgr != null ) {
// Get active network info
NetworkInfo networkInfo = mConnMgr.getActiveNetworkInfo();
// If any active network is available and internet connection is available
if ( networkInfo != null && networkInfo.isConnected() ) {
for ( String url : image_urls ) {
// Start a new Image Download Async Task
new DownloadImageTask().execute( url );
}
} else {
// If network is off or internet is not available, inform the user
Toast.makeText(this.getActivity(), "Network not Available", Toast.LENGTH_SHORT).show();
}
}
Log.d("ForYouFragment", "Finished refreshForYou");
for ( int loadData = 0; loadData < imageCounterSize; loadData++ ) {
addMessage ( listHeadings.get( loadData ) , messages.get( loadData ) , images.get( loadData ));
}
}
//here we maintain our products in various departments
private int addMessage ( String heading, String message, Bitmap image ){
int groupPosition = 0;
//check the hash map if the group already exists
GroupInfo headerInfo = gMessages.get(heading);
//add the group if doesn't exists
if( headerInfo == null ) {
headerInfo = new GroupInfo();
headerInfo.setHeading(heading);
gMessages.put(heading, headerInfo);
list.add(headerInfo);
}
//get the children for the group
ArrayList<ChildInfo> productList = headerInfo.getHeadingList();
//size of the children list
int listSize = productList.size();
//add to the counter
listSize++;
//create a new child and add that to the group
ChildInfo detailInfo = new ChildInfo();
detailInfo.setSequence ( String.valueOf( listSize ) );
detailInfo.setMessage ( message );
productList.add ( detailInfo );
headerInfo.setHeadingList ( productList );
//find the group position inside the list
groupPosition = list.indexOf(headerInfo);
return groupPosition;
}
public class PerformNetworkRequest extends AsyncTask<Void, Void, String> {
String url;
HashMap < String, String > params; // Store the names of the subjects enrolled
int requestCode;
PerformNetworkRequest ( String URL, int code ) {
url = URL;
requestCode = code;
}
@Override
protected void onPreExecute() {
super.onPreExecute();
}
@Override
protected void onPostExecute( String s ) {
Log.d("fetchWhatsHappening", "onPostExecute Register Response: " + s.toString());
super.onPostExecute( s );
try {
JSONObject root_object = new JSONObject( s );
JSONArray array = root_object.getJSONArray("for_you");
Log.d("GET URL", "Checking URL... " + API.URL_REQUEST_FOR_YOU );
if (!root_object.getBoolean("error")) {
Toast.makeText( getActivity(), root_object.getString("message"), Toast.LENGTH_SHORT ).show();
refreshForYou ( array );
} else {
}
} catch (JSONException e) {
e.printStackTrace();
Log.e("JSON Parser", "Error parsing data " + e.toString());
}
}
@Override
protected String doInBackground(Void... voids) {
RequestHandler requestHandler = new RequestHandler();
if (requestCode == CODE_POST_REQUEST)
return requestHandler.sendPostRequest(url, params);
if (requestCode == CODE_GET_REQUEST)
return requestHandler.sendGetRequest(url);
return null;
} // end of doInBackground method
} // end of PerformNetworkRequest class
/*--------------------------- Setting ListView and Downloading Images ------------------------*/
public class GroupInfo {
private String heading;
private Bitmap image;
private ArrayList < ChildInfo > list = new ArrayList < ChildInfo >();
public String getHeading() {
return heading;
}
public void setHeading ( String heading ) {
this.heading = heading;
}
public void setImage (Bitmap image) {
this.image = image;
}
public Bitmap getImage () {
return image;
}
public ArrayList < ChildInfo > getHeadingList() {
return list;
}
public void setHeadingList(ArrayList<ChildInfo> headingList) {
this.list = headingList;
}
}
public class ChildInfo {
private String sequence = "";
private String message = "";
public String getSequence() {
return sequence;
}
public void setSequence(String sequence) {
this.sequence = sequence;
}
public String getMessage() {
return message;
}
public void setMessage(String message) {
this.message = message;
}
}
public class ExpandableListAdapter extends BaseExpandableListAdapter {
private Context context;
LayoutInflater inflater;
private ArrayList<GroupInfo> forYou;
public ExpandableListAdapter (Context applicationContext, ArrayList<GroupInfo> forYou ) {
this.context = context;
this.forYou = forYou;
inflater = ( LayoutInflater.from( applicationContext ) );
}
@Override
public Object getChild( int groupPosition, int childPosition ) {
ArrayList < ChildInfo > headingList = forYou.get(groupPosition).getHeadingList();
return headingList.get(childPosition);
}
@Override
public long getChildId( int groupPosition, int childPosition ) {
return childPosition;
}
@Override
public View getChildView(int groupPosition, int childPosition, boolean isLastChild,
View view, ViewGroup parent) {
view = inflater.inflate(R.layout.for_you_list_item, null);
ChildInfo detailInfo = (ChildInfo) getChild( groupPosition, childPosition );
TextView childItem = (TextView) view.findViewById( R.id.forYouMessage );
childItem.setText( detailInfo.getMessage().trim() );
return view;
}
@Override
public int getChildrenCount ( int groupPosition ) {
ArrayList < ChildInfo > headingList = forYou.get( groupPosition ).getHeadingList();
return headingList.size();
}
@Override
public Object getGroup(int groupPosition) {
return forYou.get(groupPosition);
}
@Override
public int getGroupCount() {
return forYou.size();
}
@Override
public long getGroupId(int groupPosition) {
return groupPosition;
}
@Override
public View getGroupView(int groupPosition, boolean isLastChild, View view,
ViewGroup parent) {
view = inflater.inflate(R.layout.for_you_heading_list_view, null);
GroupInfo headerInfo = (GroupInfo) getGroup(groupPosition);
TextView heading = (TextView) view.findViewById(R.id.forYouHeading);
ImageView image = (ImageView) view.findViewById(R.id.forYouIcon);
heading.setText(headerInfo.getHeading().trim());
image.setImageBitmap(headerInfo.getImage());
return view;
}
@Override
public boolean hasStableIds() {
return true;
}
@Override
public boolean isChildSelectable(int groupPosition, int childPosition) {
return true;
}
} // end of ExpandableListAdapter
private class DownloadImageTask extends AsyncTask < String, Void, Bitmap > {
@Override
protected Bitmap doInBackground(String... urls) {
return downloadImage ( urls[0] );
}
@Override
protected void onPostExecute (Bitmap bitmap) {
if ( imageCounter < imageCounterSize ) {
if (bitmap == null) {
Drawable vectorDrawable = ResourcesCompat.getDrawable(getActivity().getResources(), R.drawable.no_image, null);
Bitmap no_image = ((BitmapDrawable) vectorDrawable).getBitmap();
images.add(no_image);
}
else if (bitmap != null) { // Add the newly downloaded image to the list
images.add( bitmap );
}
imageCounter++;
}
} // end of onPostExecute
}
public Bitmap downloadImage (String path) {
final String TAG = "Download Task";
Bitmap bitmap = null;
InputStream inStream;
try {
// Create a URL Connection object and set its parameters
URL url = new URL(path);
HttpURLConnection urlConn = (HttpURLConnection) url.openConnection();
// Set connection time out of 5 seconds
urlConn.setConnectTimeout(5000);
// Set connection time out of 2.5 seconds
urlConn.setReadTimeout(2500);
// Set HTTP request method
urlConn.setRequestMethod("GET");
urlConn.setDoInput(true);
// Perform network request
inStream = urlConn.getInputStream();
// Convert the input stream to Bitmap object
bitmap = BitmapFactory.decodeStream(inStream);
} catch (MalformedURLException e) {
Log.e(TAG, "URL error : " + e.getMessage());
} catch (IOException e) {
Log.e(TAG, "Download Failed : " + e.getMessage());}
return bitmap;
}
}
这是带有ExpandableListView的for_you_fragment.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">
<ExpandableListView
android:id="@+id/simpleExpandableListView"
android:layout_width="match_parent"
android:layout_height="fill_parent"
android:divider="#f00"
android:childDivider="#0f0"
android:dividerHeight="1dp" />
</RelativeLayout>
这是list_item.xml代码:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/forYouMessage"
android:textSize="16dip"
android:paddingLeft="10dp"
android:paddingStart="10dp"
android:paddingTop="5dp"
android:paddingBottom="5dp"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
这是list_view.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">
<ImageView
android:id="@+id/forYouIcon"
android:layout_width="fill_parent"
android:layout_height="200dp" />
<TextView
android:id="@+id/forYouHeading"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_below="@+id/forYouIcon"
android:text="..."
android:textStyle="bold" />
</RelativeLayout>
答案 0 :(得分:-1)
你在这里传递了错误的背景:
//get reference of the ExpandableListView
simpleExpandableListView = (ExpandableListView) this.getActivity().findViewById(R.id.simpleExpandableListView);
您必须传递您正在膨胀的视图,如下所示:
simpleExpandableListView = (ExpandableListView) view.findViewById(R.id.simpleExpandableListView);