NullPointerException 为啥?
Posted
技术标签:
【中文标题】NullPointerException 为啥?【英文标题】:NullPointerException why?NullPointerException 为什么? 【发布时间】:2015-04-17 10:26:23 【问题描述】:我需要一些帮助。以下应该是你所需要的。
LogCat(更新)/
04-17 11:31:07.836: E/androidRuntime(1400): FATAL EXCEPTION: main
04-17 11:31:07.836: E/AndroidRuntime(1400): java.lang.NullPointerException
04-17 11:31:07.836: E/AndroidRuntime(1400): at pl.smarterp2.ZamowienieAdd$sec_Fragment_zp.Clicker(ZamowienieAdd.java:1450)
04-17 11:31:07.836: E/AndroidRuntime(1400): at pl.smarterp2.ZamowienieAdd$sec_Fragment_zp.onCreateView(ZamowienieAdd.java:1465)
04-17 11:31:07.836: E/AndroidRuntime(1400): at android.support.v4.app.Fragment.performCreateView(Fragment.java:1500)
04-17 11:31:07.836: E/AndroidRuntime(1400): at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:927)
04-17 11:31:07.836: E/AndroidRuntime(1400): at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1104)
04-17 11:31:07.836: E/AndroidRuntime(1400): at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:682)
04-17 11:31:07.836: E/AndroidRuntime(1400): at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1467)
04-17 11:31:07.836: E/AndroidRuntime(1400): at android.support.v4.app.FragmentManagerImpl.executePendingTransactions(FragmentManager.java:472)
04-17 11:31:07.836: E/AndroidRuntime(1400): at android.support.v4.app.FragmentPagerAdapter.finishUpdate(FragmentPagerAdapter.java:141)
04-17 11:31:07.836: E/AndroidRuntime(1400): at android.support.v4.view.ViewPager.populate(ViewPager.java:1068)
04-17 11:31:07.836: E/AndroidRuntime(1400): at android.support.v4.view.ViewPager.populate(ViewPager.java:914)
04-17 11:31:07.836: E/AndroidRuntime(1400): at android.support.v4.view.ViewPager.onMeasure(ViewPager.java:1436)
04-17 11:31:07.836: E/AndroidRuntime(1400): at android.view.View.measure(View.java:15172)
04-17 11:31:07.836: E/AndroidRuntime(1400): at android.widget.RelativeLayout.measureChildHorizontal(RelativeLayout.java:617)
04-17 11:31:07.836: E/AndroidRuntime(1400): at android.widget.RelativeLayout.onMeasure(RelativeLayout.java:399)
04-17 11:31:07.836: E/AndroidRuntime(1400): at android.view.View.measure(View.java:15172)
04-17 11:31:07.836: E/AndroidRuntime(1400): at android.widget.RelativeLayout.measureChildHorizontal(RelativeLayout.java:617)
04-17 11:31:07.836: E/AndroidRuntime(1400): at android.widget.RelativeLayout.onMeasure(RelativeLayout.java:399)
04-17 11:31:07.836: E/AndroidRuntime(1400): at android.view.View.measure(View.java:15172)
04-17 11:31:07.836: E/AndroidRuntime(1400): at android.support.v4.widget.DrawerLayout.onMeasure(DrawerLayout.java:651)
04-17 11:31:07.836: E/AndroidRuntime(1400): at android.view.View.measure(View.java:15172)
04-17 11:31:07.836: E/AndroidRuntime(1400): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4816)
04-17 11:31:07.836: E/AndroidRuntime(1400): at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
04-17 11:31:07.836: E/AndroidRuntime(1400): at android.view.View.measure(View.java:15172)
04-17 11:31:07.836: E/AndroidRuntime(1400): at android.widget.LinearLayout.measureVertical(LinearLayout.java:833)
04-17 11:31:07.836: E/AndroidRuntime(1400): at android.widget.LinearLayout.onMeasure(LinearLayout.java:574)
04-17 11:31:07.836: E/AndroidRuntime(1400): at android.view.View.measure(View.java:15172)
04-17 11:31:07.836: E/AndroidRuntime(1400): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4816)
04-17 11:31:07.836: E/AndroidRuntime(1400): at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
04-17 11:31:07.836: E/AndroidRuntime(1400): at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2148)
04-17 11:31:07.836: E/AndroidRuntime(1400): at android.view.View.measure(View.java:15172)
04-17 11:31:07.836: E/AndroidRuntime(1400): at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:1850)
04-17 11:31:07.836: E/AndroidRuntime(1400): at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1102)
04-17 11:31:07.836: E/AndroidRuntime(1400): at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1275)
04-17 11:31:07.836: E/AndroidRuntime(1400): at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1000)
04-17 11:31:07.836: E/AndroidRuntime(1400): at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:4214)
04-17 11:31:07.836: E/AndroidRuntime(1400): at android.view.Choreographer$CallbackRecord.run(Choreographer.java:725)
04-17 11:31:07.836: E/AndroidRuntime(1400): at android.view.Choreographer.doCallbacks(Choreographer.java:555)
04-17 11:31:07.836: E/AndroidRuntime(1400): at android.view.Choreographer.doFrame(Choreographer.java:525)
04-17 11:31:07.836: E/AndroidRuntime(1400): at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:711)
04-17 11:31:07.836: E/AndroidRuntime(1400): at android.os.Handler.handleCallback(Handler.java:615)
04-17 11:31:07.836: E/AndroidRuntime(1400): at android.os.Handler.dispatchMessage(Handler.java:92)
04-17 11:31:07.836: E/AndroidRuntime(1400): at android.os.Looper.loop(Looper.java:137)
04-17 11:31:07.836: E/AndroidRuntime(1400): at android.app.ActivityThread.main(ActivityThread.java:4745)
04-17 11:31:07.836: E/AndroidRuntime(1400): at java.lang.reflect.Method.invokeNative(Native Method)
04-17 11:31:07.836: E/AndroidRuntime(1400): at java.lang.reflect.Method.invoke(Method.java:511)
04-17 11:31:07.836: E/AndroidRuntime(1400): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
04-17 11:31:07.836: E/AndroidRuntime(1400): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
04-17 11:31:07.836: E/AndroidRuntime(1400): at dalvik.system.NativeStart.main(Native Method)
代码(更新)/
public static class sec_Fragment_zp extends Fragment
public static final String ARG_SECTION_NUMBER = "section_number";
public sec_Fragment_zp()
//ListView lv = setContentView();
public void Clicker()
View lView = this.getView();
final ListView lista_zam = (ListView) lView.findViewById(R.id.zamadd_lista);
lista_zam.setOnClickListener(new OnClickListener()
@Override
public void onClick(View v)
lista_zam.setBackgroundResource(R.drawable.zaplacono);
);
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
View myLayout = inflater.inflate(R.layout.activity_zamowienie_fragment2, container, false);
Clicker();
return myLayout;
我不明白为什么它不起作用。
我只想在单击列表项后更改背景。这不是我的项目,我只是在做一些改变。如果你想要更多的代码问(我有一堆)。我不知道为什么它对我不起作用。
【问题讨论】:
【参考方案1】:NullPointer 在这里
final View lView = this.getView();
因为Clicker(container);
会在创建视图之前先调用。所以改变
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
Clicker(container);
return inflater.inflate(R.layout.activity_zamowienie_fragment2, container, false);
到
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
View myLayout = inflater.inflate(R.layout.activity_zamowienie_fragment2, container, false);
Clicker(container);
return myLayout;
【讨论】:
改变了 lista_zam = (ListView)lView.findViewById(R.id.zamadd_lista);这里是空指针。顺便提一句。是 OnItemClickListener 还是 OnClickListener 有什么区别? 那么 lView 应该是最终的? 我改变了 lista_zam = (ListView)lView.findViewById(R.id.zamadd_lista);到 lista_zam = (ListView)v.findViewById(R.id.zamadd_lista); ......我移到下一行:lista_zam.setOnClickListener(new OnClickListener() now here is NullPointer. 检查您的日志并确保生成的日志是相同的还是不同的?顺便说一句,您在哪一行得到了空指针? @Filip 从Clicker
方法中删除参数并按照我的建议进行更改,然后它应该可以工作。以上是关于NullPointerException 为啥?的主要内容,如果未能解决你的问题,请参考以下文章
从 sdcard 将图像文件读入位图,为啥会出现 NullPointerException?