我正在处理其中的标签和片段。在其中一个片段上得到了奇怪的运行时错误:
logcat的:
05-13 09:52:46.052 1276-1276/com.myapp.gridview E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: com.myapp.gridview, PID: 1276
java.lang.NullPointerException
at com.myapp.gridview.BoardsFragment.onResume(BoardsFragment.java:34)
at android.support.v4.app.Fragment.performResume(Fragment.java:1543)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:974)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1115)
at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:682)
at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1478)
at android.support.v4.app.FragmentManagerImpl.executePendingTransactions(FragmentManager.java:478)
at android.support.v4.app.FragmentPagerAdapter.finishUpdate(FragmentPagerAdapter.java:141)
at android.support.v4.view.ViewPager.populate(ViewPager.java:1068)
at android.support.v4.view.ViewPager.populate(ViewPager.java:914)
at android.support.v4.view.ViewPager.onMeasure(ViewPager.java:1436)
at android.view.View.measure(View.java:16497)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5125)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
at android.view.View.measure(View.java:16497)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5125)
at com.android.internal.widget.ActionBarOverlayLayout.onMeasure(ActionBarOverlayLayout.java:327)
at android.view.View.measure(View.java:16497)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5125)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2291)
at android.view.View.measure(View.java:16497)
at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:1916)
at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1113)
at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1295)
at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1000)
at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:5670)
at android.view.Choreographer$CallbackRecord.run(Choreographer.java:761)
at android.view.Choreographer.doCallbacks(Choreographer.java:574)
at android.view.Choreographer.doFrame(Choreographer.java:544)
at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:747)
at android.os.Handler.handleCallback(Handler.java:733)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5017)
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:779)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
at dalvik.system.NativeStart.main(Native Method)
和片段itslef:
import android.content.Context;
import android.support.v4.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.TextView;
import java.util.ArrayList;
import java.util.List;
public class BoardsFragment extends Fragment{
private GridView gridView;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.activity_main, container, false);
gridView = (GridView) v.findViewById(R.id.gridview);
return v;
}
@Override
public void onResume(){
super.onResume();
gridView.setAdapter(new MyAdapter(getActivity()));
}
private class MyAdapter extends BaseAdapter
{
private List<Item> items = new ArrayList<Item>();
private LayoutInflater inflater;
public MyAdapter(Context context)
{
inflater = LayoutInflater.from(context);
items.add(new Item("APO Supreme", R.drawable.snb_apo_supreme));
items.add(new Item("Arbor Blacklist", R.drawable.snb_arbor_blacklist));
items.add(new Item("Arbor Draft", R.drawable.snb_arbor_draft));
items.add(new Item("Arbor Relapse", R.drawable.snb_arbor_relapse));
items.add(new Item("Capita Defenders of Awesome", R.drawable.snb_capita_defenders_of_awesome));
items.add(new Item("Capita Outsiders", R.drawable.snb_capita_outsiders));
items.add(new Item("Capita Ultrafear", R.drawable.snb_capita_ultrafear));
items.add(new Item("DC Focus", R.drawable.snb_dc_focus));
items.add(new Item("DC Mega", R.drawable.snb_dc_mega));
items.add(new Item("DC Tone", R.drawable.snb_dc_tone));
items.add(new Item("ROME Agent", R.drawable.snb_rome_agent));
items.add(new Item("ROME Agent Rocker", R.drawable.snb_rome_agent_rocker));
items.add(new Item("ROME Hammerhead", R.drawable.snb_rome_hammerhead));
}
@Override
public int getCount() {
return items.size();
}
@Override
public Object getItem(int i)
{
return items.get(i);
}
@Override
public long getItemId(int i)
{
return items.get(i).picId;
}
@Override
public View getView(int i, View view, ViewGroup viewGroup)
{
View v = view;
ImageView picture;
TextView name;
if(v == null)
{
v = inflater.inflate(R.layout.gridview_item, viewGroup, false);
v.setTag(R.id.picture, v.findViewById(R.id.picture));
v.setTag(R.id.text, v.findViewById(R.id.text));
}
picture = (ImageView)v.getTag(R.id.picture);
name = (TextView)v.getTag(R.id.text);
Item item = (Item)getItem(i);
picture.setImageResource(item.picId);
name.setText(item.name);
return v;
}
private class Item
{
final String name;
final int picId;
Item(String name, int drawableId)
{
this.name = name;
this.picId = drawableId;
}
}
}
}
任何想法onResume()
在这里有什么问题? Perhabs它与support.v4
库有关。可能那些库需要一些“特殊”覆盖onResume等?
感谢您的回答
修改
activity_main.xml中:
<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.view.ViewPager xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/pager"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.myapp.gridview.MainActivity" />
具有GridView的fragment_main.xml:
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<GridView
android:id="@+id/gridview"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:verticalSpacing="0dp"
android:horizontalSpacing="0dp"
android:stretchMode="columnWidth"
android:numColumns="2"
/>
</FrameLayout>
答案 0 :(得分:3)
任何想法onResume()在这里有什么问题?
没错。
Perhabs它与support.v4库有关。
NUllPointerException
与支持库无关
可能这些图书馆需要一些特殊的#34;覆盖onResume等?
库没有特殊的onResume
你有
View v = inflater.inflate(R.layout.activity_main, container, false);
我猜你在Activity
中正在夸大Fragment
布局。 gridView
为空。
应该是
View v = inflater.inflate(R.layout.fragment_main, container, false);
GridView
fragment_main.xml
答案 1 :(得分:0)
只有两种可能性。 gridView或getActivity()为null。您可以轻松地检测到这一点在这一行上设一个断点。调试您的应用。然后选择gridView并按cmd + shift + I组合以查看gridView的值(对getActivity()重复这些步骤)。
如果gridView为null,则可以在onResume()中初始化它。
如果getActivity()为null,则应覆盖onActivityCreated()并在其中设置gridView适配器。