-3
將我的項目遷移到gradle後出現以下錯誤。我不確定爲什麼會發生這種情況,並且在遷移之前這種情況正在發生。我試圖將文件複製到一個全新的項目,這些項目是從頭開始構建的,現在我遇到了這些問題。將項目遷移到gradle後的空指針異常
Process: com.hospital.headling.hospital, PID: 5243
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.hospital.headling.hospital/com.hospital.headling.hospital.ListViewActivity}: java.lang.NullPointerException: Attempinvoke virtual method 'void com.hospital.headling.hospital.util.ExpandableHeightGridView.setAdapter(android.widget.ListAdapter)' on a null object reference
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2665)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2726)
at android.app.ActivityThread.-wrap12(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1477)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6119)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void com.hospital.headling.hospital.util.ExpandableHeightGridView.setAdapter(android.widget.ListAdapter)' on a null object reference
at com.hospital.headling.hospital.ListViewActivity.setAdapters(ListViewActivity.java:127)
at com.hospital.headling.hospital.ListViewActivity.onCreate(ListViewActivity.java:61)
at android.app.Activity.performCreate(Activity.java:6679)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1118)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2618)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2726)
at android.app.ActivityThread.-wrap12(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1477)
atandroid.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6119)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776)
ListViewActivity.java
package com.hospital.headling.hospital;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import com.hospital.headling.hospital.adapter.CustomAdapter;
import com.hospital.headling.hospital.util.ExpandableHeightGridView;
import org.apache.commons.lang3.ArrayUtils;
import java.util.ArrayList;
public class ListViewActivity extends Activity {
private ExpandableHeightGridView gridView;
private ListView listView;
//drawables array for gridview
private static final int[] osImage = { R.drawable.cardiac, R.drawable.ortho,
R.drawable.neuro, R.drawable.cancer, R.drawable.doctors, R.drawable.services,
R.drawable.visit, R.drawable.careers, R.drawable.bills, R.drawable.email, R.drawable.about };
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ArrayList<String> menu_links = new ArrayList<>();
menu_links.add("Cardiac");
menu_links.add("Orthopedic");
menu_links.add("Neurosciences");
menu_links.add("Cancer");
menu_links.add("Find A Doctor");
menu_links.add("Medical Services");
menu_links.add("Patients and Visitors");
menu_links.add("Careers");
menu_links.add("Pay Your Bill");
// Find a reference to the {@link ListView} in the layout
ListView companiesListView = (ListView) findViewById(R.id.list);
// Create a new {@link ArrayAdapter} of the menu_links
ArrayAdapter<String> adapter = new ArrayAdapter<String>(
this, android.R.layout.simple_list_item_1, menu_links);
// Set the adapter on the {@link ListView}
// so the list can be populated in the user interface
companiesListView.setAdapter(adapter);
///end of stuff
setAdapters();
//start
companiesListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
switch (position) {
/* case 0: Intent Cardiac = new Intent(this, Cardiac.class);
startActivity(Cardiac);
break;
case 1: Intent Orthopedic = new Intent(this, Orthopedic.class);
startActivity(newActivity);
break;
case 2: Intent Neurosciences = new Intent(this, Neurosciences.class);
startActivity(newActivity);
break;
case 3: Intent Cancer = new Intent(this, Cancer.class);
startActivity(FindADoctor);
break;
case 4: Intent newActivity = new Intent(this, reservetickets.class);
startActivity(newActivity);
break;
case 4: Intent newActivity = new Intent(this, reservetickets.class);
startActivity(newActivity);
break;
case 4: Intent newActivity = new Intent(this, reservetickets.class);
startActivity(newActivity);
break;
case 4: Intent newActivity = new Intent(this, reservetickets.class);
startActivity(newActivity);
break; */
case 9:
Intent ContactForm = new Intent(ListViewActivity.this, com.hospital.headling.hospital.ContactForm.class);
startActivity(ContactForm);
break;
}
}
});
//finish
}
/**
* set footer for listview (it's a gridview)
*/
@SuppressLint("InflateParams")
private void addGridViewAsListViewFooter() {
View footer = (View) getLayoutInflater().inflate(R.layout.layout_gridview, null);
listView.addFooterView(footer);
gridView = (ExpandableHeightGridView) footer.findViewById(R.id.gridview);
// set expand to show all gridview rows
gridView.setExpanded(true);
}
/**
* set adapters for list view and gridview
*/
private void setAdapters() {
// convert int array to Integer array by apache common lang library
Integer[] osImageList = ArrayUtils.toObject(osImage);
// set gridview adapter
CustomAdapter gridViewAdapter = new CustomAdapter(this, R.layout.item_grid, R.id.image, osImageList);
gridView.setAdapter(gridViewAdapter);
}
}
ExampleHeightGridView.java
package com.hospital.headling.hospital.util;
import android.content.Context;
import android.util.AttributeSet;
import android.view.ViewGroup;
import android.widget.GridView;
public class ExpandableHeightGridView extends GridView {
boolean expanded = false;
public ExpandableHeightGridView(Context context) {
super(context);
}
public ExpandableHeightGridView(Context context, AttributeSet attrs) {
super(context, attrs);
}
public ExpandableHeightGridView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
public boolean isExpanded() {
return expanded;
}
@Override
public void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
if (isExpanded()) {
// Calculate entire height by providing a very large height hint.
// MEASURED_SIZE_MASK represents the largest height possible.
int expandSpec = MeasureSpec.makeMeasureSpec(MEASURED_SIZE_MASK, MeasureSpec.AT_MOST);
super.onMeasure(widthMeasureSpec, expandSpec);
ViewGroup.LayoutParams params = getLayoutParams();
params.height = getMeasuredHeight();
} else {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
}
}
public void setExpanded(boolean expanded) {
this.expanded = expanded;
}
}
layout_gridview.xml
<LinearLayout 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:orientation="vertical" >
<com.healinghospital.healinghospital.util.ExpandableHeightGridView
android:id="@+id/gridview"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:horizontalSpacing="10dp"
android:verticalSpacing="10dp"
android:numColumns="6"
android:stretchMode="columnWidth"/>
<!--<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:background="#000099"
android:text="@string/corporation"
android:textColor="@android:color/white" /> -->
<TextView
android:id="@+id/list"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:background="#000099"
android:textColor="@android:color/white" />
</LinearLayout>
activity_mail.xml
<LinearLayout 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:orientation="vertical">
<ListView
android:id="@+id/list"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
</LinearLayout>
CustomAdapter.java
package com.hospital.headling.hospital.adapter;
import android.app.Activity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import com.hospital.headling.hospital.R;
public class CustomAdapter extends ArrayAdapter<Integer> {
private Activity activity;
public CustomAdapter(Activity activity, int resource, int textViewResourceId, Integer[] osimage) {
super(activity, resource, textViewResourceId, osimage);
this.activity = activity;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder = null;
// inflate layout from xml
LayoutInflater inflater = (LayoutInflater) activity
.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
// If holder not exist then locate all view from UI file.
if (convertView == null) {
// inflate UI from XML file
convertView = inflater.inflate(R.layout.item_grid, parent, false);
// get all UI view
holder = new ViewHolder(convertView);
// set tag for holder
convertView.setTag(holder);
} else {
// if holder created, get tag from view
holder = (ViewHolder) convertView.getTag();
}
holder.image.setImageResource(getItem(position));
return convertView;
}
private class ViewHolder {
private ImageView image;
public ViewHolder(View v) {
image = (ImageView)v.findViewById(R.id.image);
}
}
}
下一次,請不要事後所有的前初始化XML和Java代碼在你的問題(它需要很多時間來閱讀所有內容)。您應該通過第一條logcat消息瞭解問題: null對象引用 –
上的java.lang.NullPointerException:Attempinvoke虛方法'void com.hospital.headling.hospital.util.ExpandableHeightGridView.setAdapter(android.widget.ListAdapter)' Gradle在你自己的代碼中不會導致NullPointerExceptions,只是你知道的。 –
@HonorLT XML和一些Java代碼是必要的(因爲ID可能會從XML中丟失,這會導致它爲空)。然而,一個[mcve](關鍵字最小),將是不錯的 –