1

I can't get the Toast and the startActivity to work in the VLC.java class. Both statements work if they are put in the TerminalFragment.java class.

Android Studio reports no problems but when I run the app it crashes.

I've tried about every possible getActivity, startActivity and Context permutation but nothing works. How can I get this to work?

TerminalFragment.java

receiveText.append(toCaretString(msg, newline.length() != 0));            
        Task(msg);
        // If I put the Intent and startActivity(play) here it works fine
    }
}

public void Task(String tsk) {
//  String id = tsk.substring(0, 2);
    String id = "01";
    String[] smallString = StringUtils.substringsBetween(tsk, ";", ";");

    switch (id)
    {
    // VLC
    case "01":
       VLC myObj = new VLC();
        myObj.RadioStream(smallString);
        break;
    case "02":
        Toast.makeText(getActivity(), id, Toast.LENGTH_SHORT).show();
        break;
    default:
       Toast.makeText(getActivity(), "default", Toast.LENGTH_SHORT).show();
        break;
    }
}

VLC.java

package com.android_usb_gateway;

import android.content.Intent;
import android.net.Uri;
import android.widget.Toast;

public class VLC extends MainActivity {

public void RadioStream(String[] args) {

    // Can not get the Toast to work
    Toast.makeText(getApplicationContext(), "hello", Toast.LENGTH_SHORT).show();

    // Get the name and url
    String url = args[2];
    String name = args[3];
    String AUDIO_WILD = args[4];
    String TITLE = args[5];

    // The intent and startActivity(play) work fine if they are in   TerminalFrament.java
    Intent play = new Intent(Intent.ACTION_VIEW);
    Uri uri = Uri.parse(url);
    play.setPackage(MainActivity.app);
    play.setDataAndType(uri, AUDIO_WILD);
    play.putExtra(TITLE, name);

    // Can not get this to work
    startActivity(play);
  }
}
  • I think its because you don't have an `onCreate()` method in your vlc activity. – danial iranpour Jun 20 '21 at 08:51
  • Does this answer your question? [Unfortunately MyApp has stopped. How can I solve this?](https://stackoverflow.com/questions/23353173/unfortunately-myapp-has-stopped-how-can-i-solve-this) – Henry Twist Jun 20 '21 at 08:55
  • Unfortunately the only way to debug the app is via Toast messages. The app is an USB gateway and is connected to my Arduino and can not be connected to my PC at the same time. – stackuser99 Jun 20 '21 at 12:22

2 Answers2

0

I just tested the above VLC class and the Toast in RadioStream method works

I think the problem has to do with how you call the method in onCreate.

Make sure you're not doing

new VLC().RadioStream()

as the new VLC Object will have no Context attached and you'll get a null point pointer exception with the call to getApplicationContext()

Mab
  • 412
  • 3
  • 12
  • I am new to Android, but it is my understanding that there is only one onCreate in the App. I have this in MainActivity as follows: – stackuser99 Jun 20 '21 at 12:25
  • @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); Toolbar toolbar = findViewById(R.id.toolbar); setSupportActionBar(toolbar); getSupportFragmentManager().addOnBackStackChangedListener(this); if (savedInstanceState == null) getSupportFragmentManager().beginTransaction().add(R.id.fragment, new DevicesFragment(), "devices").commit(); else onBackStackChanged(); } – stackuser99 Jun 20 '21 at 12:27
  • Where are you calling **RadioStream()**? I thought your question claim that **Toast** only work in Fragment and not in VLC activity class. Where in the activity that *Toast* doesn't work – Mab Jun 20 '21 at 12:33
  • RadioStream() is called in switch statement 01 in TerminalFragment. If I move the Toast before the call in the switch statement then the Toast works fine. If I replace the task(msg) call in TerminalFragment with the intent and the startactivity() from VLC.java it too works fine. – stackuser99 Jun 21 '21 at 07:47
  • Let's move this to *chats*, seems your question isn't self describing. I would be willing to help - Join here https://chat.stackoverflow.com/rooms/234000/android-toast-and-startactivity-work-in-fragment-but-not-class – Mab Jun 21 '21 at 08:50
  • Thank you. Apparently I can not participate in this chat as I do not have 20 points – stackuser99 Jun 21 '21 at 12:00
  • You can *now*, you now have 21 rep – Mab Jun 21 '21 at 12:20
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/234023/discussion-between-stackuser99-and-mab). – stackuser99 Jun 21 '21 at 12:25
  • I have tried ((VLC) getActivity()).RadioStream(); suggested by Mab (thank you) but this too crashes the app. The only way to call the VLC class is by creating an object but his does not allow me to execute the Toast in the class as it has no context. It looks like you can not call a class which has no UI interaction. Does anyone else have a suggestion? – stackuser99 Jun 24 '21 at 12:49
  • your question is incomplete and doesn't fully describe the problem, if you could create a github repo, so I could test the app – Mab Jun 24 '21 at 15:42
  • Hi Mab. As requested I have create a Github space. https://github.com/buguser1234/Android_USB_Gateway – stackuser99 Jun 26 '21 at 10:25
  • I have zipped the project. Here it is https://www.udrop.com/5Hrc/Android_USB_Gateway-main.zip – stackuser99 Jun 26 '21 at 13:48
  • In the VLC class, you left **getActivity()** as a means for obtaining the Context in the **RadioStream** method. That shouldn't even compile as Activity don't have such method. And moreover, the VLC activity class is useless and it's even an error. It isn't the activity associated with the Fragment so you're even casting to the wrong type. – Mab Jun 27 '21 at 16:11
  • I don't understand the Role of the TerminalTerminal fragment in the app, and which activity it's associated to – Mab Jun 28 '21 at 06:43
  • The TerminalFragment shows a screen with the input it receives from the USB connection. It also has a text input to send messages back to the USB connection. It is connected to MainActivity. The VLC class should do nothing more than to create an Intent and start and External App. That is why if I call VLC as an object it works but it does not have a context. I do not understand why what is really a subroutine needs a context. – stackuser99 Jun 28 '21 at 08:55
  • The idea is that when TerminalFragment gets the input from the USB it invokes the VLC class and executes the StartActivity. It would be nice if VLC returns a return code to the TerminalFragment which it then passes to the USB. However at this stage that is not important as I can do this later as I get more experienced with Android – stackuser99 Jun 28 '21 at 09:03
  • I read that the VLC class should not be an activity as it is not linked to an UI. This brings me back to calling it as an object. How do I get a Toast and StartActivity to work from an object. Ultimately the App will have no UI interaction at all. It should just run in the background like a started task. – stackuser99 Jun 28 '21 at 10:49
  • To create a Toast message, a context is required. A Fragment (*TerminalFragment*) must be attached to an Activity. So simply copy the **RadioStream()**method to the Activity that Houses the Fragment – Mab Jun 28 '21 at 13:45
  • Then simply in fragment do: ```*WhatEverActivity* activity= (*WhatEverActivity*) getActivity()```. Then do ```activity.RadioStream(); ``` – Mab Jun 28 '21 at 13:48
  • Have a nice time – Mab Jun 28 '21 at 13:48
0

I managed to get it working as follows:

TerminalFragment.java

VlcObject obj = new VlcObject(this);
obj.RadioStream(smallString);

VlcObject.java

public class VlcObject {
    TerminalFragment c;

    public VlcObject(TerminalFragment c) {
        this.c = c;
    }

    public void RadioStream(String[] args) {

    Intent play = new Intent(Intent.ACTION_VIEW);

    c.startActivity(play);
    }
}
Wai Ha Lee
  • 8,598
  • 83
  • 57
  • 92