0

I have been writing a fairly big project and it is rather messy but I'm just having troubles with a small portion of it, so hopefully I won't leave anything out.

StartingActivity.java:

    @Override
protected void onCreate(Bundle savedInstanceState) {
    setContentView(R.layout.activity_starting);
    super.onCreate(savedInstanceState);
    if (ActivityCompat.checkSelfPermission(this, Manifest.permission.READ_CONTACTS)
            == PackageManager.PERMISSION_GRANTED) {
    } else
    {
        requestLocationPermission();
    }
    haveAccess();
}

private void haveAccess()
{
    Friends.readContacts(getContentResolver());

    stringArrayList = new ArrayList<Contacts>();
    int i = 0;
    while (Friends.getContacts(i).getName() != null)
    {
        stringArrayList.add(Friends.getContacts(i));
        i++;
    }
    adapter = new ColorArrayAdapter(this, R.layout.adapter_view_layout, stringArrayList);
    ListView listView = (ListView) findViewById(R.id.listView);
    listView.setAdapter((ListAdapter) adapter);

    Bundle extras = getIntent().getExtras();

    if (extras != null) {
        String message = extras.getString("message");
        Toast.makeText(StartingActivity.this, message, Toast.LENGTH_SHORT).show();
    }
    listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            receiver = Friends.ListofContacts[position].getNumber();
            checkIfOnline(position, receiver);
        }
    });

}
private void checkIfOnline(final int position, final String receiver)
{
    mDatabase.addValueEventListener(new ValueEventListener(){
        @Override
        public final void onDataChange(DataSnapshot snapshot) {
            if (snapshot.child(Friends.ListofContacts[position].getNumber()).hasChild("Activity")) {
                if (snapshot.child(Friends.ListofContacts[position].getNumber()).child("Activity").getValue(boolean.class)) {
                    ModalCall modalCall = new ModalCall();
                    modalCall.setCalledName(Friends.ListofContacts[position].getName());
                    modalCall.setmActionListener(StartingActivity.this);
                    modalCall.show(getSupportFragmentManager(), "modalMenu");
                } else {
                    //Toast.makeText(StartingActivity.this, "Friend is currently offline", Toast.LENGTH_SHORT).show();
                }
            } else {
                //Toast.makeText(StartingActivity.this, "This contact has not yet installed CatChat", Toast.LENGTH_SHORT).show();
            }
        }
        @Override
        public void onCancelled(@NonNull DatabaseError databaseError) {
            //do nothing
        }
    });
}

    @Override
public void onButtonClick(int id) {
    if (id == R.id.call) {

        // generate a token for this call
        String token = GenerateToken.generateProvisionToken("715b8b2142ee4385b37a8c0b4b752a75", "user1" + "@" + "696a11.vidyo.io", "300", "");
        String path = "Call_" + receiver;
        //createCallInDB(caller, receiver, token, path);

        Intent intent = new Intent(getBaseContext(), MainActivity.class);
        // pass two parameters to next activity
        intent.putExtra("token", token);
        intent.putExtra("path", path);
        startActivity(intent); // <-----------------------------Crash
    }
    else if (id == R.id.cancel) {
        // do nothing
    }
}

I'm trying to create a new activity and go from my current activity to MainActivity() and this has previously worked but I changed a single ListView to a LinearLayout with a textview and an image view.

The new layout version:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:weightSum="100"
tools:context="com.vidyo.vidyoconnector.StartingActivity">

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="60dp"
    android:layout_weight="50"
    android:weightSum="100"
    android:orientation="horizontal">

    <ImageView
        android:id="@+id/image"
        android:layout_width="40dp"
        android:layout_height="40dp"
        android:layout_marginStart="10dp"
        android:layout_marginTop="10dp"
        android:layout_marginBottom="10dp" />

    <TextView
        android:id="@+id/textView1"
        android:layout_width="50dp"
        android:layout_height="60dp"
        android:layout_weight="80"
        android:layout_marginStart="10dp"
        android:gravity="fill_vertical"
        android:text="TextView1"
        android:layout_gravity="start"
        android:textStyle="bold"
        android:textSize="25dp"/>


</LinearLayout>

The layout file:

<?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:id="@+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:backgroundTint="@color/ic_launcher_background"
tools:context="com.vidyo.vidyoconnector.StartingActivity">


<ListView
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_alignParentTop="true"
    android:layout_alignParentStart="true"
    android:id="@+id/listView"
    android:background="@color/ic_launcher_background"/>

I use a custom list adapter to set the values in the list. Now here comes my problem: after changing from a simple ListView to the LinearLayout it stopped working. As soon as I just try to enter the new activity it crashes. Did I do anything wrong?

MainActivity.java (the class being called):

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        mLogger.Log("onCreate");
        super.onCreate(savedInstanceState);
    }

Log messages:

2020-01-24 20:50:40.027 7167-7167/com.vidyo.vidyoconnector
D/AndroidRuntime: Shutting down VM 2020-01-24 20:50:40.028
7167-7167/com.vidyo.vidyoconnector E/AndroidRuntime: FATAL EXCEPTION:
main
    Process: com.vidyo.vidyoconnector, PID: 7167
    java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.TextView.setText(java.lang.CharSequence)' on a
null object reference
        at com.vidyo.vidyoconnector.MainActivity$3.run(MainActivity.java:503)
        at android.app.Activity.runOnUiThread(Activity.java:6282)
        at com.vidyo.vidyoconnector.MainActivity.changeState(MainActivity.java:499)
        at com.vidyo.vidyoconnector.MainActivity.startConnect(MainActivity.java:610)
        at com.vidyo.vidyoconnector.MainActivity.onStart(MainActivity.java:324)
        at android.app.Instrumentation.callActivityOnStart(Instrumentation.java:1391)
        at android.app.Activity.performStart(Activity.java:7157)
        at android.app.ActivityThread.handleStartActivity(ActivityThread.java:2937)
        at android.app.servertransaction.TransactionExecutor.performLifecycleSequence(TransactionExecutor.java:180)
        at android.app.servertransaction.TransactionExecutor.cycleToPath(TransactionExecutor.java:165)
        at android.app.servertransaction.TransactionExecutor.executeLifecycleState(TransactionExecutor.java:142)
        at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:70)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1808)
        at android.os.Handler.dispatchMessage(Handler.java:106)
        at android.os.Looper.loop(Looper.java:193)
        at android.app.ActivityThread.main(ActivityThread.java:6669)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858)
Luca Murra
  • 1,848
  • 14
  • 24

1 Answers1

0

TextView.setText(java.lang.CharSequence)' on a null object reference is because findviewbyid is not matching with textView1 on your xml.

Make sure your ids match and ensure you are using same layout in adapter where textView1 is declared.

Junaid
  • 1,301
  • 1
  • 15
  • 21