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
?