我從Android Studio中的0.2.x現在正在我的項目0.3.0和我有很奇怪的問題。Android的工作室了java.lang.RuntimeException:無法啓動活動ComponentInfo
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之前,一個Acitivity只有一個* .xml文件,但現在在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)
所以在這一刻,我應該嘗試訪問loginEditText,passwordEditText和loginButton?如何知道,fragment_login.xml中的所有子項都已添加到視圖中? – Namek
最好將loginEditText,passwordEditText和loginButton的處理移動到Fragment的onCreateView()進行處理,您可以將Fragment視爲一個mini-Activity來定義其視圖和函數,因此它可以在不同的活動中重用後來。 – Questing
你可以定義一個LoginFragment extends Fragment來移動那裏的大部分代碼。 – Questing