Toast 和 startActivity 在片段中工作但不是 class
Toast and startActivity work in fragment but not class
我无法让 Toast 和 startActivity 在 VLC.java class 中工作。如果将它们放在 TerminalFragment.java class.
中,这两个语句都有效
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);
}
}
我刚刚测试了上面的VLCclass和RadioStream中的Toast ] 方法有效
我认为问题与您如何调用 onCreate 中的方法有关。
确保你没有在做
new VLC().RadioStream()
因为新的 VLC 对象将没有附加上下文,您将在调用 getApplicationContext()
时得到一个空点指针异常
我设法让它工作如下:
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);
}
}
我无法让 Toast 和 startActivity 在 VLC.java class 中工作。如果将它们放在 TerminalFragment.java class.
中,这两个语句都有效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);
}
}
我刚刚测试了上面的VLCclass和RadioStream中的Toast ] 方法有效
我认为问题与您如何调用 onCreate 中的方法有关。
确保你没有在做
new VLC().RadioStream()
因为新的 VLC 对象将没有附加上下文,您将在调用 getApplicationContext()
时得到一个空点指针异常我设法让它工作如下:
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);
}
}