如何在片段中启动TextView?

时间:2018-06-22 13:12:34

标签: java android nullpointerexception

我不熟悉片段,并具有以下代码:

<gap:plugin name="phonegap-plugin-barcodescanner" source="npm" />

<preference name="android-build-tool" value="gradle" />

<config-file platform="ios" target="*-Info.plist" parent="NSCameraUsageDescription">
    <string>It is used for scaning QR code.</string>
</config-file>

<edit-config platform="ios" target="*-Info.plist" parent="NSCameraUsageDescription" mode="merge">
    <string>It is used for scaning QR code.</string>
</edit-config>

<config-file platform="ios" target="*-Info.plist" parent="NSPhotoLibraryUsageDescription">
    <string>It is used for scanning QR code</string>
</config-file>

<edit-config platform="ios" target="*-Info.plist" parent="NSPhotoLibraryUsageDescription" mode="merge">
    <string>It is used for scanning QR code</string>
</edit-config>

不幸的是,这给了我NullPointerException。似乎TextView对象没有启动,即使我认为应该启动。

  

“ java.lang.NullPointerException:尝试调用虚拟方法   '无效android.widget.TextView.append(java.lang.CharSequence)'在   空对象引用”

有人知道怎么了吗?

4 个答案:

答案 0 :(得分:1)

以此替换您的代码

No agent found in pool Default which satisfies the specified demands:
msbuild
visualstudio
DotNetFramework
Agent.Version -gtVersion 1.98.1

答案 1 :(得分:1)

您应该这样做:

需要将视图传递给您的widgets而不是activity,并且您inflating最终将视图带到顶部。

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState){
  View v = inflater.inflate(R.layout.assign_pattern_tab_1, container, false);
 nameAndNumberText = (TextView) v.findViewById(R.id.numberToNameView);
nameToNumberMapping = ContactNameAndNumberUtility
        .getNameToPhoneNumberMapping(getActivity()
        .getApplicationContext());

for (Map.Entry<String, String> item : nameToNumberMapping.entrySet()) {
    String key = item.getKey();
    String value = item.getValue();

    nameAndNumberText.append(key);
    nameAndNumberText.append(value);
}
return v;

答案 2 :(得分:1)

您需要先创建一个视图

View view = inflater.inflate(R.layout.assign_pattern_tab_1, container,false); 
nameAndNumberText = (TextView) view.findViewById(R.id.numberToNameView);
        nameToNumberMapping = ContactNameAndNumberUtility
                .getNameToPhoneNumberMapping(getActivity()
                .getApplicationContext());

        for (Map.Entry<String, String> item : nameToNumberMapping.entrySet()) {
            String key = item.getKey();
            String value = item.getValue();

            nameAndNumberText.append(key);
            nameAndNumberText.append(value);
        }
        return inflater.inflate(R.layout.assign_pattern_tab_1, container, false);

答案 3 :(得分:1)

您应该这样做:-

public class TripleTab1 extends Fragment {

private TextView nameAndNumberText;
private Map<String, String> nameToNumberMapping;

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState){
View v = inflater.inflate(R.layout.assign_pattern_tab_1, container, false);
    nameAndNumberText = (TextView) v.findViewById(R.id.numberToNameView);
    nameToNumberMapping = ContactNameAndNumberUtility
            .getNameToPhoneNumberMapping(getActivity()
            .getApplicationContext());

    for (Map.Entry<String, String> item : nameToNumberMapping.entrySet()) {
        String key = item.getKey();
        String value = item.getValue();

        nameAndNumberText.append(key);
        nameAndNumberText.append(value);
    }
    return v;

}