0

我现在将我的项目从 Android Studio 0.2.x 转移到 0.3.0,我有一个很奇怪的问题。

登录活动.java

public class LoginActivity extends Activity implements OnClickListener, TextWatcher {

    private EditText _loginEditText;
    private EditText _passwordEditText;
    private Button _loginButton;

    private String _login;
    private String _password;
    private String _result;

    private ProgressDialog _progressDialog;

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

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

        _loginEditText = (EditText) findViewById(R.id.loginEditText);
        _loginEditText.addTextChangedListener(this);

        _passwordEditText = (EditText) findViewById(R.id.passwordEditText);
        _passwordEditText.addTextChangedListener(this);

        _loginEditText.requestFocus();

        _loginButton = (Button) findViewById(R.id.loginButton);
        _loginButton.setOnClickListener(this);
    }

    @Override
    public void onConfigurationChanged(Configuration newConfig) {
        super.onConfigurationChanged(newConfig);
        setContentView(R.layout.activity_login);

        _loginEditText = (EditText) findViewById(R.id.loginEditText);
        _loginEditText.addTextChangedListener(this);
        _loginEditText.setText(_login);
        _loginEditText.setSelection(_loginEditText.getText().length());

        _passwordEditText = (EditText) findViewById(R.id.passwordEditText);
        _passwordEditText.addTextChangedListener(this);
        _passwordEditText.setText(_password);
        _passwordEditText.setSelection(_passwordEditText.getText().length());

        _loginEditText.requestFocus();

        _loginButton = (Button) findViewById(R.id.loginButton);
        _loginButton.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.loginButton: {
                //SMTH...
            }
        }
    }

    @Override
    public void afterTextChanged(Editable s) {
        if (_loginEditText.getText().hashCode() == s.hashCode()) {
            _loginEditText = (EditText) findViewById(R.id.loginEditText);
            _login = _loginEditText.getText().toString().trim();
        } else if (_passwordEditText.getText().hashCode() == s.hashCode()) {
            _passwordEditText = (EditText) findViewById(R.id.passwordEditText);
            _password = _passwordEditText.getText().toString().trim();
        }
    }

    @Override
    public void beforeTextChanged(CharSequence s, int start, int count, int after) {
        // TODO Auto-generated method stub
    }

    @Override
    public void onTextChanged(CharSequence s, int start, int before, int count) {
        // TODO Auto-generated method stub
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {

        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.login, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        switch (item.getItemId()) {
            case R.id.action_settings:
                return true;
        }
        return super.onOptionsItemSelected(item);
    }

    /**
     * A placeholder fragment containing a simple view.
     */
    public static class PlaceholderFragment extends Fragment {

        public PlaceholderFragment() {
        }

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

活动登录.xml:

<ScrollView 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=".LoginActivity"
    tools:ignore="MergeRootFrame" />

片段登录.xml:

<LinearLayout 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"
    android:orientation="vertical"
    tools:context=".LoginActivity$PlaceholderFragment">

    <EditText
        android:id="@+id/loginEditText"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:inputType="text"
        android:hint="@string/login_edittext_login" />

    <EditText
        android:id="@+id/passwordEditText"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:inputType="textPassword"
        android:hint="@string/login_edittext_password" />

    <Button
        android:id="@+id/loginButton"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="@string/login_button_login" />

</LinearLayout>

我的应用程序在线崩溃:

_loginEditText.addTextChangedListener(this);
_passwordEditText.addTextChangedListener(this);
_loginEditText.requestFocus();
_loginButton.setOnClickListener(this);

等等,所以看起来我无法从 xml 文件访问这些元素(loginEditText、passwordEditText、loginButton)。在 AS 0.3.0 之前,一个 Acitivity 只有一个 *.xml 文件,但现在在 0.3.0 上,它默认生成两个 *.xml 文件。我知道,我可以通过删除 *.xml 文件和 PlaceholderFragment 中的一个来克服这个问题,但是如何解决两个默认 *.xml 文件的问题?

日志猫:

java.lang.RuntimeException: Unable to start activity ComponentInfo{com.package.projectname/com.package.projectname.LoginActivity}: java.lang.NullPointerException
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2339)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2389)
            at android.app.ActivityThread.access$600(ActivityThread.java:153)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1269)
            at android.os.Handler.dispatchMessage(Handler.java:99)
            at android.os.Looper.loop(Looper.java:137)
            at android.app.ActivityThread.main(ActivityThread.java:5289)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:525)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:739)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:555)
            at dalvik.system.NativeStart.main(Native Method)
     Caused by: java.lang.NullPointerException
            at com.package.projectname.LoginActivity.onCreate(LoginActivity.java:51)
            at android.app.Activity.performCreate(Activity.java:5133)
            at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2293)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2389)
            at android.app.ActivityThread.access$600(ActivityThread.java:153)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1269)
            at android.os.Handler.dispatchMessage(Handler.java:99)
            at android.os.Looper.loop(Looper.java:137)
            at android.app.ActivityThread.main(ActivityThread.java:5289)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:525)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:739)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:555)
            at dalvik.system.NativeStart.main(Native Method)
4

2 回答 2

0

我正在处理同样的问题,但我设法让它工作:

  1. 然后将我的视图移动到 PlaceholderFragment
  2. 覆盖 onStart() 方法并在那里初始化视图。
  3. 使用 this.getActivity().findViewById() 获取 ID。

因此,在您的情况下,它将是这样的:

public static class PlaceholderFragment extends Fragment {
    EditText _loginEditText;
    EditText _passwordEditText;
    Button _loginButton;

    public PlaceholderFragment() {
    }

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

    @Override
    public void onStart() {
        super.onStart();
        _loginEditText = _loginEditText = (EditText) this.getActivity().findViewById(R.id.loginEditText);
        _loginEditText.addTextChangedListener(this);

        _passwordEditText = (EditText) this.getActivity().findViewById(R.id.passwordEditText);
        _passwordEditText.addTextChangedListener(this);
        .
        .
        .
    }
}
于 2013-11-01T09:06:17.807 回答
0

您似乎希望在 if (savedInstanceState == null) { getFragmentManager().beginTransaction() .add(R.id.container, new PlaceholderFragment()) .commit(); 之后片段已经添加到容器 ScrollView 中。}

但这不是一个同步方法,实际上 PlaceholderFragment 以及它的子 loginEditText/passwordEditText 甚至都没有被膨胀,更不用说被添加到容器视图中。

于 2013-10-21T15:56:34.230 回答