0

我正在尝试构建一个允许您输入文本的 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,我似乎得到了一个空指针异常。

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)

`

4

3 回答 3

4

team1input = (AutoCompleteTextView) view.findViewById(R.id.autoCompleteTeam1)在方法中使用onCreateView()并删除方法中类似的行onClick()

(假设您在布局文件中定义了AutoCompleteTextViewwith autoCompleteTeam1id 。)team_score_layout.xml

于 2013-04-23T20:30:38.220 回答
4

当您在视图类下调用 findViewById() 时,它会找到该类的子视图。

当 onClick 被触发时,View v传入onClick()的是按钮。如果您尝试检索的 AutoCompleteView 不是该按钮的子项,则调用v.findViewById()将为您提供NullPointerException,因为该视图在按钮的子项树下不存在。

于 2013-04-23T21:10:11.430 回答
0

只需更换

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

        team1input=(AutoCompleteTextView) view.findViewById(R.id.autoCompleteTeam1);
于 2013-04-23T21:30:27.537 回答