Click here to Skip to main content
15,881,938 members
Please Sign up or sign in to vote.
1.44/5 (2 votes)
See more:
i have this probleme it shows me this erreur


07-27 10:15:09.177 4518-4518/com.example.android.projectdestage E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.android.projectdestage, PID: 4518
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.android.projectdestage/com.example.android.projectdestage.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.view.View.setOnClickListener(android.view.View$OnClickListener)' on a null object reference
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2984)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3045)
at android.app.ActivityThread.-wrap14(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1642)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6776)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1518)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1408)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.view.View.setOnClickListener(android.view.View$OnClickListener)' on a null object reference
at com.example.android.projectdestage.MainActivity.init2(MainActivity.java:58)
at com.example.android.projectdestage.MainActivity.onCreate(MainActivity.java:38)
at android.app.Activity.performCreate(Activity.java:6956)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1126)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2927)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3045) 
at android.app.ActivityThread.-wrap14(ActivityThread.java) 
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1642) 
at android.os.Handler.dispatchMessage(Handler.java:102) 
at android.os.Looper.loop(Looper.java:154) 
at android.app.ActivityThread.main(ActivityThread.java:6776) 
at java.lang.reflect.Method.invoke(Native Method) 
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1518) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1408) 


please any help i don t know how to fixe it

What I have tried:

Java
import android.app.Dialog;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

import com.google.android.gms.common.ConnectionResult;
import com.google.android.gms.common.GoogleApiAvailability;

public class MainActivity extends AppCompatActivity


{

private Button sms;
private Button btnMap;


    private static final String TAG = "MainActivity";

    private static final int ERROR_DIALOG_REQUEST = 9001;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
       this.setContentView(R.layout.activity_main);



        if(isServicesOK()){
            init();
            init2();
        }
    }


    private void init(){
        Button btnMap = (Button) findViewById(R.id.btnMap);
        btnMap.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(MainActivity.this, MapActivity.class);
                startActivity(intent);

            }
        });
    }

    private void init2()
    {
        Button sms = (Button) findViewById(R.id.sms);
        sms.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent message = new Intent(MainActivity.this, MainActivity2.class);
                startActivity(message);
            }
        });
    }


  




    public boolean isServicesOK()
    {
        Log.d(TAG, "isServicesOK: checking google services version");

        int available = GoogleApiAvailability.getInstance().isGooglePlayServicesAvailable(MainActivity.this);

        if(available == ConnectionResult.SUCCESS)
        {
            // koulchi mli7 map request
            Log.d(TAG, "isServicesOK: Google Play Services is working");
            return true;
        }
        else if(GoogleApiAvailability.getInstance().isUserResolvableError(available))
        {
            //test d erreur
            Log.d(TAG, "isServicesOK: an error occured but we can fix it");
            Dialog dialog = GoogleApiAvailability.getInstance().getErrorDialog(MainActivity.this, available, ERROR_DIALOG_REQUEST);
            dialog.show();
        }else
            {
            Toast.makeText(this, "You can't make map requests", Toast.LENGTH_SHORT).show();
            }
        return false;
    }



}
Posted
Updated 23-Jul-20 22:41pm
Comments
David Crow 27-Jul-18 9:37am    
sms is null.
_Starbug_ 30-Jul-18 3:04am    
error is in init2() method.and in line58.

Look at the error, it is clearly telling you that an object is null, which you are trying to use to set an OnClick listener event. So use your debugger to see why. Or, you could just check the Java code and the XML to see why they do not match. There are only two instances in your program, so one of them must be the problem.
 
Share this answer
 
I dont usually post because I also get things very wrong. It looks like you're trying to call inside an anonymous class. Making your Button Object final may sort out the problem.

I have a similar issue where there is no error in the code or the xml file.

Try this:

private final Button sms;
private final Button btnMap;

private void init(){
btnMap = (Button) findViewById(R.id.btnMap);
btnMap.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent = new Intent(MainActivity.this, MapActivity.class);
startActivity(intent);

}
});
}

private void init2()
{
sms = (Button) findViewById(R.id.sms);
sms.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent message = new Intent(MainActivity.this, MainActivity2.class);
startActivity(message);
}
});
 
Share this answer
 
Comments
David Crow 24-Jul-20 8:30am    
Whether sms is final or not, that does not change that findViewById() is returning null.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900