I am newbie at developing android. And this is my first app I want to create. But I get some problem on this and don't know how to resolve it. Please help me. This error I got:
03-19 14:30:44.502 22661-22661/com.don.test E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.don.test, PID: 22661
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.don.test/com.don.test.MainActivity}: java.lang.NullPointerException
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2224)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2283)
at android.app.ActivityThread.access$800(ActivityThread.java:144)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1205)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5158)
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:732)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:566)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.NullPointerException
at com.don.test.MainActivity.onCreate(MainActivity.java:29)
at android.app.Activity.performCreate(Activity.java:5998)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2181)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2283)
at android.app.ActivityThread.access$800(ActivityThread.java:144)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1205)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5158)
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:732)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:566)
at dalvik.system.NativeStart.main(Native Method)
This is my MainActivity.java
public class MainActivity extends AppCompatActivity implements View.OnClickListener{
Button bLogout;
EditText etId, etNama, etUsername;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
etId = (EditText) findViewById(R.id.etId);
etNama = (EditText) findViewById(R.id.etNama);
etUsername = (EditText) findViewById(R.id.etUsername);
bLogout = (Button) findViewById(R.id.bLogout);
bLogout.setOnClickListener(this);
}
@Override
public void onClick(View v) {
switch (v.getId()){
case R.id.bLogout:
break;
}
}
}
I use my Samsung Galaxy SIII Mini as emulator. Can you help me with this error, thanks before
You need to set the content view for your Activity before using it's views:
Add
setContentView(R.layout.activity_main); // activity_main is an example, here you need to put the name of your layout
Right after
super.onCreate(savedInstanceState);
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.