我尝试从同一活动中的“string”中的“EditText”中提取但失败。
鳕鱼:
String mStreamUrl = ((EditText) findViewById(R.id.bTorrentUrl)).getText().toString();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_t);
String action = getIntent().getAction();
Uri data = getIntent().getData();
if (action != null && action.equals(Intent.ACTION_VIEW) && data != null) {
try {
mStreamUrl = URLDecoder.decode(data.toString(), "utf-8");
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
}
日志:
hutting down VM
02-18 18:55:49.326 5809-5809/ro.vrt.videoplayerstreaming E/AndroidRuntime: FATAL EXCEPTION: main
Process: ro.vrt.videoplayerstreaming, PID: 5809
java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{ro.vrt.videoplayerstreaming/ro.vrt.videoplayerstreaming.TorrentPlayer}: java.lang.NullPointerException: Attempt to invoke virtual method 'android.view.View android.view.Window.findViewById(int)' on a null object reference
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2327)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476)
at android.app.ActivityThread.-wrap11(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5417)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'android.view.View android.view.Window.findViewById(int)' on a null object reference
at android.app.Activity.findViewById(Activity.java:2096)
at ro.vrt.videoplayerstreaming.TorrentPlayer.<init>(TorrentPlayer.java:34)
at java.lang.Class.newInstance(Native Method)
at android.app.Instrumentation.newActivity(Instrumentation.java:1067)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2317)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476)
at android.app.ActivityThread.-wrap11(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5417)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
02-18 18:55:53.763 5809-5809/ro.vrt.videoplayerstreaming I/Process: Sending signal. PID: 5809 SIG: 9
如果你删除字符串
mStreamUrl =((EditText)findViewById(R.id.bTorrentUrl))。getText()。toString();
并放
private String mStreamUrl =“xxxx”;
一切正常。
谢谢
答案 0 :(得分:1)
您应该使用 findViewById()方法 后调用 setContentView()。因此,在setContentView之后移动活动的onCreate中的行。
同时检查活动布局
中是否存在EditText的ID答案 1 :(得分:1)
改变这个:
String mStreamUrl = ((EditText) findViewById(R.id.bTorrentUrl)).getText().toString();
到此:
String mStreamUrl;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_t);
mStreamUrl = ((EditText) findViewById(R.id.bTorrentUrl)).getText().toString();
// ...
}
在布局膨胀之前,您无法找到该视图。但即便如此,字符串也将是您在布局中定义的任何值。您可能希望等到用户在从窗口小部件获取文本之前单击按钮。
答案 2 :(得分:0)
当你使用findViewById时,你总是冒着NullPointerException的风险,因为Android加载了它的视图。我建议如果你的视图是非null,然后在一个单独的语句中将它转换为字符串(getText()。toString())。 更新:记得在setContentView()
之后调用它另外请记住,findViewById只会返回您在setContentView上设置的视图所包含的小部件。如果按钮放在其他视图上,则代码将永远无法运行。