Android Studio java.lang.RuntimeException:无法启动活动ComponentInfo

时间:2013-10-21 15:25:04

标签: android xml android-studio placeholder

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

LoginActivity.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;
        }
    }
}

activity_login.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" />

fragment_login.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之前,只有一个* .xml文件用于一个Acitivity,但现在在0.3.0上它默认生成两个* .xml文件。我知道,我可以解决这个问题,删除* .xml文件和PlaceholderFragment中的一个,但如何用两个默认的* .xml文件解决问题?

logcat的:

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)

2 个答案:

答案 0 :(得分:0)

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

但这不是一个同步的方法,实际上,PlaceholderFragment及其子loginEditText / passwordEditText甚至没有被夸大,让我们一起被添加到容器视图中。

答案 1 :(得分:0)

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

  1. 将我的观点移至PlaceholderFragment然后
  2. 覆盖onStart()方法并初始化那里的视图。
  3. 使用this.getActivity()。findViewById()获取ID。
  4. 所以在你的情况下,它将是这样的:

    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);
            .
            .
            .
        }
    }