Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/33.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 片段中元素的带有onClickListener的NullPointerException_Java_Android - Fatal编程技术网

Java 片段中元素的带有onClickListener的NullPointerException

Java 片段中元素的带有onClickListener的NullPointerException,java,android,Java,Android,我在找碎片工作上花了很长时间。其基础是在创建LoginActivity时默认显示登录屏幕(片段)。该片段有一个文本视图,单击该文本视图时,应该启动一个新片段(密码丢失表单)。从LoginActivity调用forgottenPassword.setOnClickListener时,我不断收到NullPointerException 我还没来得及知道还有没有别的办法,项目就崩溃了 我肯定在这里的某个地方有一些明显的新手错误 onCreate方法来自LoginActivity.java: prote

我在找碎片工作上花了很长时间。其基础是在创建
LoginActivity
时默认显示登录屏幕(片段)。该片段有一个文本视图,单击该文本视图时,应该启动一个新片段(密码丢失表单)。从LoginActivity调用
forgottenPassword.setOnClickListener
时,我不断收到NullPointerException

我还没来得及知道还有没有别的办法,项目就崩溃了

我肯定在这里的某个地方有一些明显的新手错误

onCreate
方法来自
LoginActivity.java

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    setContentView(R.layout.activity_login);

    if (savedInstanceState == null) {
        FragmentManager fragmentManager = getFragmentManager();
        int fragmentTransaction = fragmentManager.beginTransaction()
                .add(R.id.login_screen_fragment, new FragmentLogin())
                .commit();
    }


    // Listen for FORGOTTEN PASSWORD click event, open ForgottenPassword Fragment //
    final TextView forgottenPassword = (TextView) findViewById(R.id.button_lost_pass);

    forgottenPassword.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            // Do what I need this to do...
        }
    });
}
public class FragmentLogin extends Fragment {

    public FragmentLogin() {
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        final View view = inflater.inflate(R.layout.fragment_login, container, false);
        return view;
    }

    @Override
    public void onViewCreated(View view, Bundle savedInstanceState) {
        final Activity activity = getActivity();

    }
}
layout\u login.xml

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/login_screen_fragment"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".LoginActivity"
/>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.xxxx.xxx.FragmentLogin">

    <ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:fitsSystemWindows="true">

        <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:padding="20dp"
            android:orientation="vertical"
            android:layout_gravity="center"
            android:focusable="true"
            android:focusableInTouchMode="true">
            <TextView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text="I forgot my password"
                android:gravity="center"
                android:layout_marginTop="32dp"
                android:padding="8dp"
                android:textSize="16sp"
                android:id="@+id/login_forgot"
                />
        </LinearLayout>
    </ScrollView>
</FrameLayout>
fragment\u密码丢失.xml

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/login_screen_fragment"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".LoginActivity"
/>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.xxxx.xxx.FragmentLogin">

    <ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:fitsSystemWindows="true">

        <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:padding="20dp"
            android:orientation="vertical"
            android:layout_gravity="center"
            android:focusable="true"
            android:focusableInTouchMode="true">
            <TextView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text="I forgot my password"
                android:gravity="center"
                android:layout_marginTop="32dp"
                android:padding="8dp"
                android:textSize="16sp"
                android:id="@+id/login_forgot"
                />
        </LinearLayout>
    </ScrollView>
</FrameLayout>
FragmentLostPassword.java

public class FragmentForgottenPassword extends Fragment {

    @Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    return inflater.inflate(R.layout.fragment_forgotten_password, container, false);
    }
}
错误跟踪:

05-13 12:49:37.222 15163-15163/com.xxxxx.xxxxx E/AndroidRuntime: FATAL EXCEPTION: main
         Process: com.xxxxx.xxxxx, PID: 15163
         java.lang.RuntimeException: Unable to start activity ComponentInfo{com.xxxxx.xxxxx/com.xxxxx.xxxxx.LoginActivity}: 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:2325)
             at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2387)
             at android.app.ActivityThread.access$800(ActivityThread.java:151)
             at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1303)
             at android.os.Handler.dispatchMessage(Handler.java:102)
             at android.os.Looper.loop(Looper.java:135)
             at android.app.ActivityThread.main(ActivityThread.java:5254)
             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:903)
             at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)
          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.xxxxx.xxxxx.LoginActivity.onCreate(LoginActivity.java:51)
             at android.app.Activity.performCreate(Activity.java:5990)
             at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1106)
             at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2278)
             at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2387) 
             at android.app.ActivityThread.access$800(ActivityThread.java:151) 
             at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1303) 
             at android.os.Handler.dispatchMessage(Handler.java:102) 
             at android.os.Looper.loop(Looper.java:135) 
             at android.app.ActivityThread.main(ActivityThread.java:5254) 
             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:903) 
             at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698) 
05-13 12:50:28.592 15163-15173/com.xxxxx.xxxxx W/art: Suspending all threads took: 15.077ms
05-13 12:50:44.622 15163-15163/? I/Process: Sending signal. PID: 15163 SIG: 9

什么是activity_login.xml内容。在调用new Fragment的oncreate()方法中,我发现button button\u lost\u pass的id错误,但片段布局(布局\u lost\u密码)不包含id(R.id.button\u lost\u pass)

更改:

final TextView forgottenPassword = (TextView) findViewById(R.id.button_lost_pass);


实际上,当您分配forgetpassword时,您的片段可能无法访问,您可以对layout_login.xml使用以下内容:

<fragment
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/main_container"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:name="FragmentLogin"  
    tools:layout="@layout/fragment_login"
    />

注意:您可能必须定义FragmentLogin的正确路径。 这是使用fragment的新方法,它将自动加载登录片段,而无需在onCreate中写入任何内容

此外,它不是一个文本视图,而是一个按钮

最后,删除片段事务代码

另一个选择是将忘记密码按钮登录移动到FragmentLogin。因为基本上您尝试从活动中访问与片段相关的元素。一切都应该在你的碎片里。
您仍然可以使用getActivity().getFragmentManger()访问片段管理器,以加载丢失的密码布局。

您必须在片段的布局中找到视图。 在FragmentLogin类中使用以下命令:

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    final View view = inflater.inflate(R.layout.fragment_login, container, false);

    final TextView forgottenPassword = (TextView) view.findViewById(R.id.button_lost_pass);
    // Set your onClickListener here

    return view;
}

您可以发布您得到的错误跟踪吗?在您的XML中没有id为
button\u lost\u pass
的TextView在您的xmal R.id.button\u lost中这个id在哪里_pass@GunjanVerma-我看到并更正了XML中的引用,但同样的问题仍然存在。已添加。粘贴的内容太多了,忘记了一个。setContentView(R.layout.activity\u login);所以您将布局设置为activity_login.xml。但是你的按钮在fragment_登录中。所以你需要在片段中定义你的按钮,而不是在活动中。谢谢。我把文本视图和按钮放在一边,这一切归结为没有在片段中定义“android:name”。