GetData把字符串放在android EditText

时间:2016-02-28 15:37:18

标签: android android-intent

我想将从应用程序B收到的URL放入我的应用程序,在EditText中。 但我收到空

这是我的代码,我试图获得意图:

    Intent intent = getIntent();
    mStreamUrl = intent.getDataString();
    FinalurlPrimit.setText(mStreamUrl);

布局:

   <android.support.design.widget.TextInputLayout
    android:id="@+id/input_torrent_url"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginLeft="16dp"
    android:layout_marginRight="16dp"
    android:layout_below="@+id/include"
    android:layout_alignParentEnd="true"
    android:layout_alignParentStart="true"
    android:layout_marginTop="70dp">

    <EditText
        android:id="@+id/bTorrentUrl"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:inputType="textUri"
        android:hint="@string/Text_magnet_sau_torrent_url"
        android:text="@+id/FinalurlPrimit"/>
</android.support.design.widget.TextInputLayout>

怎么能实现这个目标? 谢谢

编辑

日志:

02-28 16:54:09.900 5698-5698/ro.vrt.videoplayerstreaming E/AndroidRuntime: FATAL EXCEPTION: main
                                                                       Process: ro.vrt.videoplayerstreaming, PID: 5698
                                                                       java.lang.RuntimeException: Unable to start activity ComponentInfo{ro.vrt.videoplayerstreaming/ro.vrt.videoplayerstreaming.TorrentPlayer}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.TextView.setText(java.lang.CharSequence)' on a null object reference
                                                                           at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2416)
                                                                           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 'void android.widget.TextView.setText(java.lang.CharSequence)' on a null object reference
                                                                           at ro.vrt.videoplayerstreaming.TorrentPlayer.onCreate(TorrentPlayer.java:48)
                                                                           at android.app.Activity.performCreate(Activity.java:6251)
                                                                           at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1107)
                                                                           at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2369)
                                                                           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-28 16:54:11.988 5698-5698 / ro.vrt.videoplayerstreaming I / Process:发送信号。 PID:5698 SIG:9

如果删除android:text="@+id/FinalurlPrimit"。一切正常。

但我希望收到的网址出现在EditText

我无法访问应用程序B,应用程序B的信息收到正确。只是无法在EditText中显示

2 个答案:

答案 0 :(得分:0)

FirstActivity

String yourURL ="http://stackoverflow.com/";   
Intent i = new Intent(this, SecondActivity.class);
i.putExtra("URL",yourURL);
startActivity(i);

SecondActivity

Bundle extras = getIntent().getExtras();
if(extras !=null) {
    String value = extras.getString("URL");
    FinalurlPrimit.setText(value);
}

答案 1 :(得分:0)

请删除<link rel='https://api.w.org/' href='http://xxx/wp-json/' /> <link rel="canonical" href="http://xxx/" /> 。我认为你不需要它。在android:text="@+id/FinalurlPrimit"之间,当android: text上没有任何输入时,text用来显示EditText上显示的id。不需要OnCreate

EditText方法中,请务必初始化FinalurlPrimit= (EditText)findViewById(R.id.bTorrentUrl); ,如下所示

{{1}}