首先,我必须说我是Java编程的新手 这是我的问题:
我有以下课程:
public class NfcRfConfig {
boolean enabled;
Set<String> tech;
public NfcRfConfig() {
Log.i(tag, "Contructor");
enabled = false;
tech = new HashSet<String>();
}
public void setTech(String techno) {
tech.add(techno);
}
}
我在我的应用程序中使用它,来自片段:
Map<String, NfcRfConfig> mNfcRfConfig = new HashMap<String, NfcRfConfig>();
public View onCreateView(LayoutInflater inflater,
ViewGroup container,
Bundle savedInstanceState)
{
Log.d(tag, "onCreateView()");
mView = inflater.inflate(R.layout.fragment_rf_config, container, false);
mContext = getActivity();
Log.d(tag, "onCreateView() - preparing mNfcRfConfig");
//NfcRfConfig config = new NfcRfConfig();
mNfcRfConfig.put(NfcAdapterStExtensions.MODE_READER, new NfcRfConfig());
mNfcRfConfig.put(NfcAdapterStExtensions.MODE_CE, new NfcRfConfig());
mNfcRfConfig.put(NfcAdapterStExtensions.MODE_P2P_POLL, new NfcRfConfig());
mNfcRfConfig.put(NfcAdapterStExtensions.MODE_P2P_LISTEN, new NfcRfConfig());
}
然后我有一堆checkBox,当点击一个时,我想调用我的NfcRfConfig类的方法setTech():
case R.id.pollAReader:
Log.d(tag, "onClick() - pollAReader");
NfcRfConfig config1 = mNfcRfConfig.get(NfcAdapterStExtensions.MODE_READER);
if(box.isChecked() == true)//adds tech to list
{
//Disable other options
config1.setTech(NfcAdapterStExtensions.TECH_A);
}
else // remove tech from list
{
config1.removeTech(NfcAdapterStExtensions.TECH_A);
}
mNfcRfConfig.put(NfcAdapterStExtensions.MODE_READER, config1);
break;
但这是我在logcat中的内容:
D/STNFCINfo2 - RfConfig( 1619): onCreateView()
D/STNFCINfo2 - RfConfig( 1619): onCreateView() - preparing mNfcRfConfig
I/NfcRfConfig( 1619): Contructor
I/NfcRfConfig( 1619): Contructor
I/NfcRfConfig( 1619): Contructor
I/NfcRfConfig( 1619): Contructor
D/STNFCINfo2 - RfConfig( 1619): onClick() - entry
D/STNFCINfo2 - RfConfig( 1619): onClick() - pollAReader
I/NfcRfConfig( 1619): setTech(TECH_A)
D/AndroidRuntime( 1619): Shutting down VM
W/dalvikvm( 1619): threadid=1: thread exiting with uncaught exception (group=0x416bcb90)
E/AndroidRuntime( 1619): FATAL EXCEPTION: main
E/AndroidRuntime( 1619): Process: com.android.stnfcinfo2, PID: 1619
E/AndroidRuntime( 1619): java.lang.NullPointerException
E/AndroidRuntime( 1619): at com.st.android.nfc_extensions.NfcRfConfig.setTech(NfcRfConfig.java:31)
E/AndroidRuntime( 1619): at com.android.stnfcinfo2.FragmentRfConfig.onClick(FragmentRfConfig.java:140)
E/AndroidRuntime( 1619): at android.view.View.performClick(View.java:4424)
这个错误对您来说是否明显?