0
votes

In the application I'm trying to develop I have a layout featured_workshop_layout with CardViews with buttons. Each button with displays same layout but with different data based on each cards. I tried the below method but keep getting FATAL Exception.

check the button1 here (featured_workshop_layout)

<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout

android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<!-- android:background="#E1F5FE">
A CardView that contains a TextView -->
<android.support.v7.widget.CardView xmlns:card_view="http://schemas.android.com/apk/res-auto"
    android:id="@+id/card_view"
    android:layout_width="match_parent"
    android:layout_height="200dp"
    android:layout_gravity="center"
    android:layout_margin="8dp"
    android:padding="10dp"
    card_view:cardCornerRadius="4dp"
    card_view:cardElevation="4dp">

    <RelativeLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content">

        <TextView
            android:id="@+id/titleTextView1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignLeft="@+id/button1"
            android:layout_alignParentTop="true"
            android:layout_alignStart="@+id/button1"
            android:paddingBottom="16dp"
            android:paddingTop="24dp"
            android:text="Android 2.0"
            android:textAppearance="?android:attr/textAppearanceLarge"
            android:textSize="24sp" />

        <TextView
            android:id="@+id/contentTextView1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignLeft="@+id/titleTextView1"
            android:layout_alignStart="@+id/titleTextView1"
            android:layout_centerVertical="true"
            android:paddingBottom="16dp"
            android:paddingTop="16dp"
            android:text="Venue: SJT 311 \n Time: 29/10/2015 5-7pm \n Note: Bring charged laptop"
            android:textSize="14sp" />

        <Button
            android:id="@+id/button1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentBottom="true"
            android:layout_alignParentLeft="true"
            android:layout_alignParentStart="true"
            android:layout_marginLeft="23dp"
            android:layout_marginStart="23dp"
            android:background="#0000"
            android:onClick="showRegister"
            android:tag="SITEAndroid 2.0"
            android:padding="8dp"
            android:text="Register"
            android:textColor="#FFA000" />


    </RelativeLayout>
</android.support.v7.widget.CardView>

<android.support.v7.widget.CardView xmlns:card_view="http://schemas.android.com/apk/res-auto"
    android:id="@+id/card_view1"
    android:layout_width="match_parent"
    android:layout_height="200dp"
    android:layout_gravity="center"
    android:layout_margin="8dp"
    android:padding="10dp"
    card_view:cardCornerRadius="4dp"
    card_view:cardElevation="4dp">

    <RelativeLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content">

        <TextView
            android:id="@+id/titleTextView2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignLeft="@+id/button3"
            android:layout_alignParentTop="true"
            android:layout_alignStart="@+id/button3"
            android:paddingBottom="16dp"
            android:paddingTop="24dp"
            android:text="IEEE-SPS Core Committee Selections"
            android:textAppearance="?android:attr/textAppearanceLarge"
            android:textSize="24sp" />

        <TextView
            android:id="@+id/contentTextView2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignLeft="@+id/titleTextView2"
            android:layout_alignStart="@+id/titleTextView2"
            android:layout_centerVertical="true"
            android:paddingBottom="16dp"
            android:paddingTop="16dp"
            android:text="Venue: TT 724 \n Time: 30/10/2015 6-7.30pm"
            android:textSize="14sp" />

        <Button
            android:id="@+id/button3"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentBottom="true"
            android:layout_alignParentLeft="true"
            android:layout_alignParentStart="true"
            android:layout_marginLeft="24dp"
            android:layout_marginStart="24dp"
            android:background="#0000"
            android:onClick="showRegister"
            android:padding="8dp"
            android:text="Register"
            android:textColor="#FFA000" />


    </RelativeLayout>
</android.support.v7.widget.CardView>

<android.support.v7.widget.CardView xmlns:card_view="http://schemas.android.com/apk/res-auto"
    android:id="@+id/card_view2"
    android:layout_width="match_parent"
    android:layout_height="200dp"
    android:layout_gravity="center"
    android:layout_margin="8dp"
    android:padding="10dp"
    card_view:cardCornerRadius="4dp"
    card_view:cardElevation="4dp">

    <RelativeLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content">

        <TextView
            android:id="@+id/textView12"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignLeft="@+id/button2"
            android:layout_alignParentTop="true"
            android:layout_alignStart="@+id/button2"
            android:paddingBottom="16dp"
            android:paddingTop="24dp"
            android:text="IQ VIT DEXTER"
            android:textAppearance="?android:attr/textAppearanceLarge"
            android:textSize="24sp" />

        <TextView
            android:id="@+id/textView21"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignLeft="@+id/button2"
            android:layout_alignStart="@+id/button2"
            android:layout_centerVertical="true"
            android:paddingBottom="16dp"
            android:paddingTop="16dp"
            android:text="Venue: MB210\n Time: 31/10/2015 5.30pm\nContact: 9484356876"
            android:textSize="14sp" />

        <Button
            android:id="@+id/button2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentBottom="true"
            android:layout_alignParentLeft="true"
            android:layout_alignParentStart="true"
            android:layout_marginLeft="32dp"
            android:layout_marginStart="32dp"
            android:background="#0000"
            android:onClick="showRegister"
            android:padding="8dp"
            android:text="Register"
            android:textColor="#FFA000" />


    </RelativeLayout>
</android.support.v7.widget.CardView>

MainActivity.java

public void showRegister(View view)
{   String site = "SITE";
    String taginfo =  view.getTag().toString();
    System.out.println(taginfo);
    //check tag content
    if(taginfo.contains(site)) {
        System.out.println("tag has keyword site");
        RegistrationForm.schoolName.setText(site);
    }

    startActivity(new Intent(MainActivity.this,
            RegistrationForm.class));

}

RegistrationForm.java

public class RegistrationForm extends AppCompatActivity {

EditText rno;
EditText name;
TextView displayTemp;
MyDBHandler dbHandler;
public static TextView schoolName;
public static TextView workshopName;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.registration_form);
    rno = (EditText)findViewById(R.id.edit_rno);
    name =(EditText)findViewById(R.id.edit_name);
    schoolName = (TextView) findViewById(R.id.schoolName);
    workshopName = (TextView) findViewById(R.id.workshopName);
    displayTemp = (TextView)findViewById(R.id.textView3);
    dbHandler = new MyDBHandler(this,null,null,1);
    displayText();

}

I have declared TextView as public in RegistrationForm.java, where i want to display the details. When accessing it from MainActivity to change the value, I get the following exception.

FATAL EXCEPTION: main Process: com.example.intodarkness.vitworkshop, PID: 9770 java.lang.IllegalStateException: Could not execute method of the activity at android.view.View$1.onClick(View.java:4025) at android.view.View.performClick(View.java:4785) at android.view.View$PerformClick.run(View.java:19884) at android.os.Handler.handleCallback(Handler.java:739) at android.os.Handler.dispatchMessage(Handler.java:95) at android.os.Looper.loop(Looper.java:135) at android.app.ActivityThread.main(ActivityThread.java:5343) at java.lang.reflect.Method.invoke(Native Method) at java.lang.reflect.Method.invoke(Method.java:372) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:905) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:700) Caused by: java.lang.reflect.InvocationTargetException at java.lang.reflect.Method.invoke(Native Method) at java.lang.reflect.Method.invoke(Method.java:372) at android.view.View$1.onClick(View.java:4020) at android.view.View.performClick(View.java:4785)  at android.view.View$PerformClick.run(View.java:19884)  at android.os.Handler.handleCallback(Handler.java:739)  at android.os.Handler.dispatchMessage(Handler.java:95)  at android.os.Looper.loop(Looper.java:135)  at android.app.ActivityThread.main(ActivityThread.java:5343)  at java.lang.reflect.Method.invoke(Native Method)  at java.lang.reflect.Method.invoke(Method.java:372)  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:905)  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:700)  Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.TextView.setText(java.lang.CharSequence)' on a null object reference at com.example.intodarkness.vitworkshop.MainActivity.showRegister(MainActivity.java:52) at java.lang.reflect.Method.invoke(Native Method)  at java.lang.reflect.Method.invoke(Method.java:372)  at android.view.View$1.onClick(View.java:4020)  at android.view.View.performClick(View.java:4785)  at android.view.View$PerformClick.run(View.java:19884)  at android.os.Handler.handleCallback(Handler.java:739)  at android.os.Handler.dispatchMessage(Handler.java:95)  at android.os.Looper.loop(Looper.java:135)  at android.app.ActivityThread.main(ActivityThread.java:5343)  at java.lang.reflect.Method.invoke(Native Method)  at java.lang.reflect.Method.invoke(Method.java:372)  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:905)  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:700) 

3

3 Answers

1
votes

I guess your problem is here:

if(taginfo.contains(site)) {
    System.out.println("tag has keyword site");
    RegistrationForm.schoolName.setText(site);
}

startActivity(new Intent(MainActivity.this,
        RegistrationForm.class));

I would recommend your to send the param from one activity to another using Intent.putExtra() instead of static variable.

Here https://stackoverflow.com/a/3510771/2219237 you can find an example how to do it.

1
votes

If you want to change the value of textview in Activity2 Then while going from Activity1 to Activity2 just set the value in Intent.putExtra() and start the activity and in onCreate() method of Activity2, get the intent value by calling getIntent.getExtras().getString('your_key'); and change the textview value.

0
votes

Solved it. Instead of trying to access the required fields from MainActicity, I accessed the required details from CardView in MainActivity and did rest of the operations in Registration form where I was able to access TextViews of registration_form.

I used System.out.println() statements to find the order of execution where I found out Activity was called only after setText statements which is why I got the nullException error.