片段中的Onclick按钮方法

时间:2015-07-22 07:35:51

标签: java android

我在android中创建了简单的应用程序。我在导航栏中有一个用户档案。当在该抽屉中选择地址选项时打开该片段(示例:地址片段)。在那个布局中我放了保存按钮。单击该保存按钮,我想验证所有字段。但是,当我从抽屉中选择那个选项时,应用程序很快停止了。

我的代码在这里:

AddressFragment.java

import android.support.v4.app.Fragment;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.AutoCompleteTextView;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;


public class AddressFragment extends Fragment {
    EditText line2;
    Button addrsave;
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
							 Bundle savedInstanceState) {

		View view = inflater.inflate(R.layout.fragment_layout_address, container, false);

        addrsave = (Button) getActivity().findViewById(R.id.addrsave);

        // Get a reference to the AutoCompleteTextView in the layout
		AutoCompleteTextView textView = (AutoCompleteTextView) view.findViewById(R.id.city_autoCompleteTextView);
		// Get the string array
		String[] city = getResources().getStringArray(R.array.city);
		// Create the adapter and set it to the AutoCompleteTextView
		ArrayAdapter<String> adapter = new ArrayAdapter<String>(getActivity(), android.R.layout.simple_list_item_1, city);

		textView.setAdapter(adapter);
        addrsave.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String addr = line2.getText().toString();
                if (addr.equals("")) {
                    Toast.makeText(getActivity().getApplicationContext(), "Field cannot be left blank.", Toast.LENGTH_SHORT).show();
                }
            }
        });

	return view;

  }


}

我的logcat:

07-22 12:47:46.235    6355-6355/com.h2o E/AndroidRuntime﹕ FATAL EXCEPTION: main
    Process: com.h2o, PID: 6355
    java.lang.RuntimeException: Unable to start activity ComponentInfo{com.h2o/com.h2o.AccountActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.Button.setOnClickListener(android.view.View$OnClickListener)' on a null object reference
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2298)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2360)
            at android.app.ActivityThread.access$800(ActivityThread.java:144)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1278)
            at android.os.Handler.dispatchMessage(Handler.java:102)
            at android.os.Looper.loop(Looper.java:135)
            at android.app.ActivityThread.main(ActivityThread.java:5221)
            at java.lang.reflect.Method.invoke(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:372)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)
     Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.Button.setOnClickListener(android.view.View$OnClickListener)' on a null object reference
            at com.h2o.AddressFragment.onCreateView(AddressFragment.java:38)
            at android.support.v4.app.Fragment.performCreateView(Fragment.java:1789)
            at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:955)
            at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1138)
            at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:740)
            at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1501)
            at android.support.v4.app.FragmentActivity.onStart(FragmentActivity.java:551)
            at android.app.Instrumentation.callActivityOnStart(Instrumentation.java:1220)
            at android.app.Activity.performStart(Activity.java:5953)
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2261)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2360)
            at android.app.ActivityThread.access$800(ActivityThread.java:144)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1278)
            at android.os.Handler.dispatchMessage(Handler.java:102)
            at android.os.Looper.loop(Looper.java:135)
            at android.app.ActivityThread.main(ActivityThread.java:5221)
            at java.lang.reflect.Method.invoke(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:372)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)

请任何人帮忙!!

提前致谢!!!

4 个答案:

答案 0 :(得分:4)

更改此行

addrsave = (Button) getActivity().findViewById(R.id.addrsave);

addrsave = (Button) view.findViewById(R.id.addrsave);

您的R.layout.fragment_layout_address已分配到view,因此您必须使用view变量从布局中获取按钮。其次,初始化line2,一旦你解决了第一个问题,它将抛出NPE(空指针)的例外

答案 1 :(得分:4)

改变这个:

addrsave = (Button) getActivity().findViewById(R.id.addrsave);

到此:

addrsave = (Button) view.findViewById(R.id.addrsave);

问题是你在getActivity()上做了findViewById(),它给你一个null。然后在其上设置onClickListener将抛出NullPointerException。

希望它有所帮助! :)

答案 2 :(得分:2)

我无法看到您初始化line2的位置? 所以line2为null,可能导致异常

String addr = line2.getText().toString();

答案 3 :(得分:0)

我认为您已经在片段中添加了按钮,并且您正尝试从活动中检索它。因此,更改从getActivity()检索视图以查看如下所示

addrsave = (Button) getActivity().findViewById(R.id.addrsave);

将以上行替换为

addrsave = (Button) view.findViewById(R.id.addrsave); 

你错过了第38行的第2行视图初始化