Monday, November 11, 2013

Android Studio java.lang.RuntimeException: Unable to start activity ComponentInfo [android help]


Android Studio java.lang.RuntimeException: Unable to start activity ComponentInfo



I'm moving now my project from Android Studio 0.2.x to 0.3.0 and I have quite strange problem.


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:



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:



xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".LoginActivity$PlaceholderFragment">

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

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

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




And my app crashes on lines:



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


etc. so it looks like I couldn't access to these elements (loginEditText, passwordEditText, loginButton) from xml files. Before AS 0.3.0 it was only one *.xml file for one Acitivity, but now on 0.3.0 it default generate two *.xml files. I know, that I could overcome this problem, removing one of *.xml file and PlaceholderFragment, but how to solve the problem with two default *.xml files?


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)


Read more

stackoverflow.comm



No comments:

Post a Comment

Google Voice on T-Mobile? [General]

Google Voice on T-Mobile? So I recently switched from a GNex on Verizon to a Moto X DE on T-Mobile. I had always used Google Voice for my v...