使EditText可在Activity之间调用

时间:2016-01-10 19:47:50

标签: android android-activity android-preferences

我试图将EditText从一个活动调用到另一个活动但是当我的应用程序崩溃时。

我尝试使用addPreferencesFromResource方法使用' import android.preference.PreferenceActivity'并导入android.preference.PreferenceFragment'但是" addPreferencesFromResource'保持红色。

如何让它可以调用?

settings_layout

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:weightSum="1"
        android:orientation="vertical"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="right"
        android:layout_alignParentLeft="true"
        android:layout_marginLeft="0dp"
        android:layout_alignParentTop="true"
        android:layout_marginTop="0dp"
        android:id="@+id/linearLayout">

    <TextView
        android:layout_width="127dp"
        android:layout_height="wrap_content"
        android:text="website A"
        android:id="@+id/textView3"
        android:layout_weight="0.07"
        android:layout_marginTop="115dp"
        android:layout_below="@+id/linearLayout"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true" />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="back"
        android:onClick="showZaire"
        android:id="@+id/button5"
        android:layout_gravity="center_horizontal"
        android:layout_alignParentBottom="true"
        android:layout_toRightOf="@+id/textView3"
        android:layout_toEndOf="@+id/textView3" />

    <EditText
        android:layout_width="200dp"
        android:layout_height="wrap_content"
        android:id="@+id/webA"
        android:layout_alignBottom="@+id/textView3"
        android:layout_toRightOf="@+id/textView3"
        android:layout_toEndOf="@+id/textView3" />

    <TextView
        android:layout_width="127dp"
        android:layout_height="wrap_content"
        android:text="website B"
        android:id="@+id/textView2"
        android:layout_weight="0.07"
        android:layout_below="@+id/textView3"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:layout_marginTop="34dp" />

    <EditText
        android:layout_width="200dp"
        android:layout_height="wrap_content"
        android:id="@+id/webB"
        android:layout_alignBottom="@+id/textView2"
        android:layout_toRightOf="@+id/textView2"
        android:layout_toEndOf="@+id/textView2" />
</LinearLayout

第二项活动

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import android.app.Activity;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.text.Editable;
import android.text.method.ScrollingMovementMethod;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

public class secondActivity extends Activity {
    EditText webs = (EditText)findViewById(R.id.webA);// this is what im trying to call from settings
    String web = webs.getText().toString();
    TextView titleText;
    String title = "";
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.seccond_layout);
        Button button = (Button) findViewById(R.id.refresh);
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                restartActivity(seccondActivity.this);
            }
        });

        //initialize variables
        titleText = (TextView) findViewById(R.id.titleText);
        ;


        //run on new thread because we cannot do network operation on main thread
        new Thread(new Runnable() {

            @Override
            public void run() {
                try {

                    //get the Document object from the site. Enter the link of site you want to fetch
                    Document document = Jsoup.connect(web).get(); // this is the website string

                    //Get the title of blog using title tag
                    title = document.select("h6").text().toString();
                    //set the title of text view
                    //Run this on ui thread because another thread cannot touch the views of main thread
                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {

                            //set both the text views
                            titleText.setText(title);
                            titleText.setMovementMethod(new ScrollingMovementMethod());
                        }
                    });
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }).start();
    }


    public void showZaire(View view) {
        String button_test;
        button_test = ((Button) view).getText().toString();
        if (button_test.equals("Home")) {
            Intent intent1 = new Intent(this, MainActivity.class);
            startActivity(intent1);
        } else if (button_test.equals("Directions")) {
            Intent intent = new Intent(this, ThirdActivity.class);
            startActivity(intent);
            }
        }
}

settingsActivity

import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.preference.PreferenceFragment;
import android.text.Editable;
import android.text.Layout;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

/**
 * Created by Shane on 10/01/2016.
 */
public class SettingsActivity extends Activity {
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.settings_layout);}


    public void showZaire(View view) {
        String button_test;
        button_test = ((Button) view).getText().toString();
        if (button_test.equals("Back")) {
            Intent intent1 = new Intent(this, MainActivity.class);
            startActivity(intent1);
        } else if (button_test.equals("Help")) {
            Intent intent2 = new Intent(this, Help.class);
            startActivity(intent2);
        }
    }

}

0 个答案:

没有答案