安卓:findViewById返回即便是以后的setContentView空即便是、findViewById、setContentView

2023-09-12 10:38:05 作者:劫数

下面是我的code在LoginActivity:

Here's my code in LoginActivity:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_login);

    if (savedInstanceState == null) {
        getSupportFragmentManager().beginTransaction()
                .add(R.id.container, new PlaceholderFragment()).commit();
    }

    loginButton = (Button)findViewById(R.id.loginButton);

    loginButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            onLoginButtonClicked();
        }
    });

    // Check if there is a currently logged in user
    // and they are linked to a Facebook account.
    ParseUser currentUser = ParseUser.getCurrentUser();
    if ((currentUser != null) && ParseFacebookUtils.isLinked(currentUser)) {
        // Go to the main activity
        showHomeActivity();
    }
}

在fragment_login.xml

in fragment_login.xml

<Button
    android:id="@+id/loginButton"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_centerHorizontal="true"
    android:layout_centerVertical="true"
    android:text="@string/login_button" />

这是日志,当我运行应用程序:

And this is the log when i run the application:

E/AndroidRuntime(6647): FATAL EXCEPTION: main
E/AndroidRuntime(6647): Process: com.moostachestudio.drinkitapp, PID: 6647
E/AndroidRuntime(6647): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.moostachestudio.drinkitapp/com.moostachestudio.drinkitapp.LoginActivity}: java.lang.NullPointerException
E/AndroidRuntime(6647):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2195)
E/AndroidRuntime(6647):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
E/AndroidRuntime(6647):     at android.app.ActivityThread.access$800(ActivityThread.java:135)
E/AndroidRuntime(6647):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
E/AndroidRuntime(6647):     at android.os.Handler.dispatchMessage(Handler.java:102)
E/AndroidRuntime(6647):     at android.os.Looper.loop(Looper.java:136)
E/AndroidRuntime(6647):     at android.app.ActivityThread.main(ActivityThread.java:5017)
E/AndroidRuntime(6647):     at java.lang.reflect.Method.invokeNative(Native Method)
E/AndroidRuntime(6647):     at java.lang.reflect.Method.invoke(Method.java:515)
E/AndroidRuntime(6647):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
E/AndroidRuntime(6647):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
E/AndroidRuntime(6647):     at dalvik.system.NativeStart.main(Native Method)
E/AndroidRuntime(6647): Caused by: java.lang.NullPointerException
E/AndroidRuntime(6647):     at com.moostachestudio.drinkitapp.LoginActivity.onCreate(LoginActivity.java:45)
E/AndroidRuntime(6647):     at android.app.Activity.performCreate(Activity.java:5231)
E/AndroidRuntime(6647):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
E/AndroidRuntime(6647):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2159)
E/AndroidRuntime(6647):     ... 11 more

问题是,loginButton为NULL,但我不知道为什么,因为我所说的findViewById在以后的setContentView!

The problem is that "loginButton" is NULL but i don't know why, because i call the findViewById after the setContentView!

推荐答案

看来你的按钮是片段的布局,所以您对点击的方法可能是在片段( PlaceholderFragment ),而不是你的活动。您需要声明另一个类与片段扩展。

It seems that your Button is in fragment layout, so your on click method might be in your fragment (PlaceholderFragment) instead of your activity. You need to declare another Class extends with Fragment..

首先,创建一个名为 PlaceHolderFragment 新类。与片段扩展,并添加下面是这个code:

First, create a new Class named PlaceHolderFragment. Extend it with Fragment and add this code below to it:

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
    Bundle savedInstanceState) {
    View rootView = inflater.inflate(R.layout.fragment_login, container, false);
    // ...
    loginButton = (Button) rootView.findViewById(R.id.loginButton);
    loginButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            onLoginButtonClicked();
        }
    });
    // ...
    return rootView;
}  

// put your method onLoginButtonClicked here.

那么,对于您的布局活性 MainActivity (名为 activity_main )可能有一个的FrameLayout 像这样的ID集装箱:

Then, your layout activity for MainActivity (named activity_main) might have a FrameLayout with the id container like this:

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/container"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity" >

    <!-- other views -->

</FrameLayout>  

最后,你的 MainActivity 只会有:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    if (savedInstanceState == null) {
        // you add you fragment here in id container
        getSupportFragmentManager().beginTransaction()
             .add(R.id.container, new PlaceholderFragment()).commit();
    }

    // ... without loginButton
    // and some more stuff
}