【发布时间】:2026-01-11 20:30:01
【问题描述】:
我无法让 Toast 和 startActivity 在 VLC.java 类中工作。如果将它们放在 TerminalFragment.java 类中,这两个语句都可以工作。
Android Studio 报告没有问题,但是当我运行应用程序时它崩溃了。
我已经尝试了所有可能的 getActivity、startActivity 和 Context 排列,但没有任何效果。我怎样才能让它工作?
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);
}
}
【问题讨论】:
-
我认为这是因为您的 vlc 活动中没有
onCreate()方法。 -
不幸的是,调试应用程序的唯一方法是通过 Toast 消息。该应用程序是一个USB网关,连接到我的Arduino,不能同时连接到我的PC。
标签: java android class fragment toast