NullPointerException 为什么?
NullPointerException why?
我需要一些帮助。以下应该是你所需要的。
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;
}
我不明白为什么它不起作用。
只是我只想在单击列表项后更改背景。这不是我的项目,我只是做了一些改变。如果您想要更多代码,请询问(我有一堆)。我不知道为什么它对我不起作用。
空指针在这里
final View lView = this.getView();
因为Clicker(container);
会在创建view.So之前先调用Change
@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;
}
我需要一些帮助。以下应该是你所需要的。
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;
}
我不明白为什么它不起作用。
只是我只想在单击列表项后更改背景。这不是我的项目,我只是做了一些改变。如果您想要更多代码,请询问(我有一堆)。我不知道为什么它对我不起作用。
空指针在这里
final View lView = this.getView();
因为Clicker(container);
会在创建view.So之前先调用Change
@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;
}