0

  I try to extract from "EditText" in "string" in the same activity but fail.

cod:

        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();
            }
        }

log:

    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

If you deleteString

mStreamUrl = ((EditText)findViewById(R.id.bTorrentUrl)).getText().toString();

and put

private String mStreamUrl = "xxxx";

everything work fine.

Thank you

diaconu liviu
  • 1,032
  • 2
  • 13
  • 30

3 Answers3

1

You should use the findViewById() method after you have called the setContentView(). So move the line in the onCreate of your activity after setContentView.

Also check that the id of the EditText exists in the activity layout

Farhad
  • 12,178
  • 5
  • 32
  • 60
1

Change this:

String mStreamUrl = ((EditText) findViewById(R.id.bTorrentUrl)).getText().toString();

to this:

String mStreamUrl;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_t);
    mStreamUrl = ((EditText) findViewById(R.id.bTorrentUrl)).getText().toString();
    // ...
    }

You can't find the view before the layout is inflated. Even then, however, the string will be whatever value you defined in the layout. You probably want to wait until the user clicks a button before you get the text from the widget.

dsh
  • 12,037
  • 3
  • 33
  • 51
0

When you use findViewById you always risk a NullPointerException because of the way Android loads it's views. What I would suggest if to see if your view is non-null then cast it to a string (getText().toString()) in a separate statement. Update : remember to call this after setContentView()

Also remember that findViewById will only return widgets contained by the view you set on setContentView. If the button is placed on other view, your code will never work.

Andy
  • 479
  • 7
  • 20