BraveHeart BraveHeart - 1 month ago 7
Android Question

Calling one of my Main Activity Method From another Class Returns an Error

MAIN ACTIVITY

public class MainActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Intent intent=new Intent(getApplicationContext(),SecondActivity.class);
startActivity(intent);
}


public void function(String data) {
EditText editText=(EditText)findViewById(R.id.EditText);
editText.setText(data);

}


}

here is my SECOND ACTIVITY

public class SecondActivity extends Activity {
MainActivity mainActivity=new MainActivity();
@Override
public void onCreate(Bundle save){
super.onCreate(save);
setContentView(R.layout.layout2);
Button button=(Button)findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String data="SUCCESS!";
mainActivity.function(data);
}
});

}


}

Here is the Error

10-07 20:56:47.678 11920-11920/wifiexplorer.braveheart.com.classtest E/AndroidRuntime: FATAL EXCEPTION: main
Process: wifiexplorer.braveheart.com.classtest, PID: 11920
java.lang.NullPointerException: Attempt to invoke virtual method 'android.view.Window$Callback android.view.Window.getCallback()' on a null object reference
at android.support.v7.app.AppCompatDelegateImplBase.<init>(AppCompatDelegateImplBase.java:68)
at android.support.v7.app.AppCompatDelegateImplV7.<init>(AppCompatDelegateImplV7.java:145)
at android.support.v7.app.AppCompatDelegateImplV11.<init>(AppCompatDelegateImplV11.java:28)
at android.support.v7.app.AppCompatDelegateImplV14.<init>(AppCompatDelegateImplV14.java:41)
at android.support.v7.app.AppCompatDelegateImplV23.<init>(AppCompatDelegateImplV23.java:29)
at android.support.v7.app.AppCompatDelegate.create(AppCompatDelegate.java:186)
at android.support.v7.app.AppCompatDelegate.create(AppCompatDelegate.java:170)
at android.support.v7.app.AppCompatActivity.getDelegate(AppCompatActivity.java:502)
at android.support.v7.app.AppCompatActivity.findViewById(AppCompatActivity.java:174)
at wifiexplorer.braveheart.com.classtest.MainActivity.function(MainActivity.java:20)
at wifiexplorer.braveheart.com.classtest.SecondActivity$1.onClick(SecondActivity.java:22)
at android.view.View.performClick(View.java:5277)
at android.view.View$PerformClick.run(View.java:21711)
at android.os.Handler.handleCallback(Handler.java:815)
at android.os.Handler.dispatchMessage(Handler.java:104)
at android.os.Looper.loop(Looper.java:207)
at android.app.ActivityThread.main(ActivityThread.java:5896)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:789)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:679)


I am new to android Programming, here i just use an intent to call my second Activity.

Answer

Use this documentation "Getting a Result from an Activity"

You want to update your ui element. Good practice in your case is use startActivityForResult. Example code in your case:

 public class MainActivity extends AppCompatActivity {

    public static final String STRING_RESULT_DATA_KEY = "result_data";

    private static final int REQUEST_CODE_SECOND_ACTIVITY = 101;

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

        Intent intent = new Intent(this, SecondActivity.class);
        startActivityForResult(intent, REQUEST_CODE_SECOND_ACTIVITY);
    }

    public void function(String data) {
        EditText editText=(EditText)findViewById(R.id.EditText);
        editText.setText(data);

    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        // Check which request we're responding to
        if (requestCode == REQUEST_CODE_SECOND_ACTIVITY) {
            // Make sure the request was successful
            if (resultCode == RESULT_OK) {
                String dataString = data.getStringExtra(STRING_RESULT_DATA_KEY);
                function(dataString);
            }
        }
    }
}

SecondActivity:

public class SecondActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.second_activity);
        Button button = (Button) findViewById(R.id.button);
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String data = "SUCCESS!";
                Intent intent = new Intent();
                intent.putExtra(MainActivity.STRING_RESULT_DATA_KEY, data);
                setResult(RESULT_OK, intent);
                finish();
            }
        });
    }
}
Comments