Reputation: 488
when i try to luch my application, this error message appear, please help:
this is the error message:
02-21 10:46:40.551 1092-1092/com.example.myapplication3.app E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: com.example.myapplication3.app, PID: 1092
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.myapplication3.app/com.example.myapplication3.app.MainActivity2}: java.lang.NullPointerException
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2176)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2226)
at android.app.ActivityThread.access$700(ActivityThread.java:135)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1397)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:4998)
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:777)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:593)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.NullPointerException
at com.example.myapplication3.app.MainActivity2.onCreate(MainActivity2.java:27)
at android.app.Activity.performCreate(Activity.java:5243)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2140)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2226)
at android.app.ActivityThread.access$700(ActivityThread.java:135)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1397)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:4998)
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:777)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:593)
at dalvik.system.NativeStart.main(Native Method)
the line of java code that is associated to this was an error is: ((Button)findViewById(R.id.monBouton)).setOnClickListener(....)
this my code java:
package com.example.myapplication3.app;
public class MainActivity2 extends ActionBarActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
if (savedInstanceState == null) {
getSupportFragmentManager().beginTransaction()
.add(R.id.container, new PlaceholderFragment())
.commit();
}
((Button)findViewById(R.id.monBouton)).setOnClickListener(
new OnClickListener() {
@Override
public void onClick(View v) {
EditText texte = ((EditText)findViewById(R.id.monEditText));
String nom = texte.getText().toString();
Toast.makeText(MainActivity2.this, nom, Toast.LENGTH_SHORT).show();
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main_activity2, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
if (id == 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_main2, container, false);
return rootView;
}
}
}
code XML
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<EditText
android:id="@+id/monEditText"
android:layout_height="wrap_content"
android:hint="Taper votre nom"
android:layout_width="fill_parent">
</EditText>
<!--
Le Bouton qui permettra à l’utilisateur
de valider sa saisie
-->
<Button
android:id="@+id/monBouton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Valider"
android:layout_gravity="center_horizontal">
</Button>
</LinearLayout>
Upvotes: 0
Views: 2547
Reputation: 51411
You are getting a NullpointerException
because your layout file does not contain a View (most likely a FrameLayout
) with the id R.id.container.
So this piece of code will crash:
getSupportFragmentManager().beginTransaction()
.add(R.id.container, new PlaceholderFragment())
.commit();
Because "R.id.container" does not exist in the layout file. Put a FrameLayout
with the id "container" in your layout file. For example like that:
<FrameLayout
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent" >
</FrameLayout>
Upvotes: 1
Reputation: 7860
Use:
Button mButton = (Button)findViewById(R.id.monBouton);
Then:
mButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//Code here
}
});
Upvotes: 0