0
Whenever I try to use setOnClickListener the application to, I have tried several methods to try to fix this problem, but I could not. the Login button is properly set in R, also in the layout, the problem is always with setOnClickListener.
Java activity.:
package myapplication.app;
import android.support.v7.app.ActionBarActivity;
import android.support.v4.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
public class Activity extends ActionBarActivity {
Button login;
@Override
protected void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity);
if (savedInstanceState == null){
getSupportFragmentManager().beginTransaction().add(R.id.container, new PlaceholderFragment()).commit();
}
login = (Button) findViewById(R.id.entrar);
login.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
setContentView(R.layout.principal);
}
});
}
Log:
02-18 17:39:27.356 1614-1614/myapplication.app E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: myapplication.app, PID: 1614
java.lang.RuntimeException: Unable to start activity ComponentInfo{myapplication.app/myapplication.app.Activity}: java.lang.NullPointerException
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2195)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
at android.app.ActivityThread.access$800(ActivityThread.java:135)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5017)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.NullPointerException
at myapplication.app.Activity.onCreate(Activity.java:30)
at android.app.Activity.performCreate(Activity.java:5231)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2159)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
at android.app.ActivityThread.access$800(ActivityThread.java:135)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5017)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
at dalvik.system.NativeStart.main(Native Method)
The button
R.id.entrar
is in Fragment or Activity xml?– Igor Castañeda Ferreira
It’s in the Fragment. I found this message in Exception: "Invalid element:Androidlightfield:"
– Rafael Alexandre
Another @Raphael thing, since you are using fragments, your navigation should be done using the Fragment!
– Igor Castañeda Ferreira
Can I only use Activity? @Igorcastañedaferreira
– Rafael Alexandre
Of course. Just remove the block that is inside the
if (savedInstanceState == null)
. But the navigation by Fragment is a standard that helps make apps that will work on smatphones and tablets. And the correct way to navigate between Acitivities you can see in this link– Igor Castañeda Ferreira