的Android Prorgram崩溃时我从autocompletetext尝试的getText()

2019-10-29 13:56发布

我想建立一个Android应用程序,让您输入文字,然后将文本传输到主屏幕。 它让我没有问题,建立自动完成,但每当我尝试调用的getText()函数,整个程序崩溃。 任何帮助,将不胜感激,谢谢

     public class TeamFragment extends DialogFragment {
TextView team1, team2, score1, score2;



    AutoCompleteTextView team1input;



public TeamFragment(View t1, View t2, View s1, View s2) {
    team1=(TextView) t1;
    team2=(TextView) t2;
    score1=(TextView) s1;
    score2=(TextView) s2;
}

public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.team_score_layout, container);
    //mEditText = (EditText) view.findViewById(R.id.txt_your_name);
    getDialog().setTitle("Enter Team Name");
    Button b = (Button) view.findViewById(R.id.teamButton);

    b.setOnClickListener(new OnClickListener() {
        public void onClick(View v) {
            team1input=(AutoCompleteTextView) v.findViewById(R.id.autoCompleteTeam1);
            team1input.getText();         //crashes here
            team1.setText("hi");
            dismiss();
        }
    });
    return view;
}


}

这里是logcat的,我似乎得到一个NullPointerException异常。

04-23 13:12:33.628: E/AndroidRuntime(902): FATAL EXCEPTION: main
04-23 13:12:33.628: E/AndroidRuntime(902): java.lang.NullPointerException
04-23 13:12:33.628: E/AndroidRuntime(902):  at com.example.ucsb.cs.cs185.dimberman.nba.TeamFragment$1.onClick(TeamFragment.java:42)
04-23 13:12:33.628: E/AndroidRuntime(902):  at android.view.View.performClick(View.java:2485)
04-23 13:12:33.628: E/AndroidRuntime(902):  at android.view.View$PerformClick.run(View.java:9080)
04-23 13:12:33.628: E/AndroidRuntime(902):  at android.os.Handler.handleCallback(Handler.java:587)
04-23 13:12:33.628: E/AndroidRuntime(902):  at android.os.Handler.dispatchMessage(Handler.java:92)
04-23 13:12:33.628: E/AndroidRuntime(902):  at android.os.Looper.loop(Looper.java:123)
04-23 13:12:33.628: E/AndroidRuntime(902):  at android.app.ActivityThread.main(ActivityThread.java:3683)
04-23 13:12:33.628: E/AndroidRuntime(902):  at java.lang.reflect.Method.invokeNative(Native Method)
04-23 13:12:33.628: E/AndroidRuntime(902):  at java.lang.reflect.Method.invoke(Method.java:507)
04-23 13:12:33.628: E/AndroidRuntime(902):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
04-23 13:12:33.628: E/AndroidRuntime(902):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
04-23 13:12:33.628: E/AndroidRuntime(902):  at dalvik.system.NativeStart.main(Native Method)

`

Answer 1:

使用team1input = (AutoCompleteTextView) view.findViewById(R.id.autoCompleteTeam1)onCreateView()方法和删除类似线中onClick()方法。

(假设你有AutoCompleteTextViewautoCompleteTeam1在定义ID team_score_layout.xml布局文件。)



Answer 2:

当你调用findViewById()一个视图类下找到该类的子视图。

当的onClick被触发, View v传递到onClick()是按钮。 如果您正试图检索AutoCompleteView不是按钮,然后调用的孩子v.findViewById()会给你NullPointerException ,因为视图不会按钮的孩子树下存在。



Answer 3:

只需更换

        team1input=(AutoCompleteTextView) v.findViewById(R.id.autoCompleteTeam1);

        team1input=(AutoCompleteTextView) view.findViewById(R.id.autoCompleteTeam1);


文章来源: Android Prorgram Crashes when i attempt to getText() from an autocompletetext