-2

i have main activity:

public class MainActivity extends AppCompatActivity {

    Button btnadd;

    int a1 = 10;

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

        btnadd = (Button) findViewById(R.id.btnadd);

        btnadd.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                final SecondAct sa = new SecondAct();

                sa.ttl(a1);


            }
        });
    }
}

and i have other activity:

public class SecondAct extends Activity {

    public TextView txt2;

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

        txt2 = (TextView) findViewById(R.id.txt2);
    }

    public void numsum(int no)
    { 
        txt2.setText(String.valueOf(no));
    }
}

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
>


    <Button
        android:id="@+id/btnadd"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="addd"></Button>

    <LinearLayout
        android:layout_below="@+id/btnadd"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <include layout="@layout/second"></include>
    </LinearLayout>
</RelativeLayout>

When i click on button from main activity to set textview text of second activity then it gives me error.

Error:

FATAL EXCEPTION: main
                                                                        Process: com.example.sumdemo, PID: 13809
                                                                        java.lang.NullPointerException


                               at com.example.sumdemo.MainActivity$1.onClick(MainActivity.java:28)
                                                                        at android.view.View.performClick(View.java:4463)
                                                                        at android.view.View$PerformClick.run(View.java:18770)
                                                                        at android.os.Handler.handleCallback(Handler.java:808)
                                                                        at android.os.Handler.dispatchMessage(Handler.java:103)
                                                                        at android.os.Looper.loop(Looper.java:193)
                                                                        at android.app.ActivityThread.main(ActivityThread.java:5333)
                                                                        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:824)
                                                                        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:640)
                                                                        at dalvik.system.NativeStart.main(Native Method)

How can i set second activity value from main activity without passing data through intent ?

4 Answers4

2
 final SecondAct sa = new SecondAct();
 sa.ttl(a1);

never use the new operator on a class that extends Activity. You have to use startActivity to start SecondAct, and provide additional info through the Intent object

Blackbelt
  • 156,034
  • 29
  • 297
  • 305
0

Use Application class with a static parameter.

That is

class MyApp extends Application{
    public static TextView tv;
}

in the first activity:

usemyapp.tv = (TextView) findViewById(...);

in oreder to have an access from somewhere use this:

if (MyApp.tv != null)
    MyApp.tv.setText("a new text");

do not forget to wrap it in mainUiThread by using a handler.

in AndroidMainfest.xml file use this:

<application
    android:name=".MyApp" <<<<<<<<<<<<<<<<<<<<<<<this the name of the class
    >

Futhermore, do not create an activity class by a constructor. this is a very very bad idea:) You have this error because you have created the class but onCreate() method has not called and has not attached to Android UI process.

K Neeraj Lal
  • 6,768
  • 3
  • 24
  • 33
Vyacheslav
  • 26,359
  • 19
  • 112
  • 194
0

Use this question as guide.

How to manage `startActivityForResult` on Android?

And don't try to change one activity content from another activity.

Community
  • 1
  • 1
Alpha
  • 1,754
  • 3
  • 21
  • 39
0

When you create an Activity object through its constructor, you can't call its life cycle methods (e.g onCreate) so your activity doesn't have a View and you can't access your views. you must use startActivity and let android handle creating Activity object and call its life cycle method. you should change its view after inflating view. for your case, you can pass extra data via Intents and use that data in your second Activity

Mohammad Rahchamani
  • 5,002
  • 1
  • 26
  • 36