簡體   English   中英

如何使活動A中的edittext值出現在活動B中的列表視圖中?

[英]How to make edittext value in activity A appear in a listview in activity B?

我在smashlist.setAdapter(Lincadapter);收到一個nullpointer錯誤。 在活動B中。

我確保ID都正確,因此問題不存在。 我在做什么錯,我該如何解決?

活動A->

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    setContentView(R.layout.popwindow);

    DisplayMetrics dm = new DisplayMetrics();
    getWindowManager().getDefaultDisplay().getMetrics(dm);

    int width = dm.widthPixels;
    int height = dm.heightPixels;

    getWindow().setLayout((int)(width*.8), (int)(height*.7));

}

public void enter(View view){


    Intent i = new Intent(Pop.this, MainActivity.class);

    final EditText name   = (EditText)findViewById(R.id.name);
    final EditText age   = (EditText)findViewById(R.id.age);
    final EditText ethnicity   = (EditText)findViewById(R.id.ethnicity);
    final EditText hair   = (EditText)findViewById(R.id.hair);
    final EditText date   = (EditText)findViewById(R.id.date);
    final EditText extras   = (EditText)findViewById(R.id.extras);

    String nameinput = name.getText().toString();
    String ageinput = age.getText().toString();
    String ethnicityinput = ethnicity.getText().toString();
    String hairinput = hair.getText().toString();
    String dateinput = date.getText().toString();
    String extrasinput = extras.getText().toString();

    i.putExtra("name", nameinput);
    i.putExtra("age", ageinput);
    i.putExtra("ethnicity", ethnicityinput);
    i.putExtra("hair", hairinput);
    i.putExtra("date", dateinput);
    i.putExtra("extras", extrasinput);


    startActivity(i);
};

}

活動A XML->

<EditText
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:hint="Name"
    android:maxLines="1"
    android:maxLength="25"
    android:singleLine="true"
    android:capitalize="words"
    android:layout_weight="1"
    android:id="@+id/name"/>
<EditText
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:hint="Age"
    android:inputType="number"
    android:maxLength="2"
    android:layout_weight="1"
    android:id="@+id/age"/>
<EditText
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:hint="Ethnicity"
    android:maxLines="1"
    android:maxLength="25"
    android:singleLine="true"
    android:capitalize="words"
    android:layout_weight="1"
    android:id="@+id/ethnicity"/>
<EditText
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:hint="Hair color"
    android:maxLines="1"
    android:maxLength="25"
    android:singleLine="true"
    android:capitalize="sentences"
    android:layout_weight="1"
    android:id="@+id/hair"/>

<EditText
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:inputType="date"
    android:ems="10"
    android:hint="Date"
    android:maxLines="1"
    android:maxLength="10"
    android:singleLine="true"
    android:layout_weight="1"
    android:id="@+id/date"/>

<EditText
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:hint="Extras"
    android:maxLines="3"
    android:maxLength="200"
    android:capitalize="sentences"
    android:layout_weight="1"
    android:id="@+id/extras"/>


<ImageView
    android:layout_width="50dp"
    android:layout_height="50dp"
    android:src="@drawable/tickbutton"
    android:clickable="true"
    android:layout_gravity="right"
    android:layout_weight="1"
    android:onClick="enter"/>

活動B->

@Override
protected void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    if (savedInstanceState == null) {
        getSupportFragmentManager().beginTransaction()
                .add(R.id.container, new PlaceholderFragment())
                .commit();


    }

    Bundle smashdata = getIntent().getExtras();
    if (smashdata == null){
        return;
    }

    String nameinput = smashdata.getString("name");
    String ageinput = smashdata.getString("age");
    String ethnicityinput = smashdata.getString("ethnicity");
    String hairinput = smashdata.getString("hair");
    String dateinput = smashdata.getString("date");
    String extrasinput = smashdata.getString("extras");

    final ListView smashlist = (ListView) findViewById(R.id.smashlist);
    String[] info = {nameinput, ageinput, ethnicityinput, hairinput, dateinput, extrasinput};
    ListAdapter lincadapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, info);
    smashlist.setAdapter(lincadapter);

    smashlist.setOnItemClickListener(
            new AdapterView.OnItemClickListener() {
                @Override
                public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

                }
            }
    );

活動B XML->

<TextView android:text="I've Smashed 0 Girls"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gravity="center"
    android:textSize="30sp"
    android:textColor="#000000"
    />

<ListView
    android:id="@+id/smashlist"
    android:layout_width="wrap_content"
    android:layout_height="272dp"
    android:layout_weight="1.00"
    android:background="#ff0000"/>

<ImageView
    android:layout_width="75dp"
    android:layout_height="75dp"
    android:clickable="true"
    android:src="@drawable/plusbutton"
    android:layout_gravity="center_horizontal"
    android:onClick="add"/>

崩潰日志--->

 at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2295)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2349)
        at android.app.ActivityThread.access$700(ActivityThread.java:159)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1316)
        at android.os.Handler.dispatchMessage(Handler.java:99)
        at android.os.Looper.loop(Looper.java:176)
        at android.app.ActivityThread.main(ActivityThread.java:5419)
        at java.lang.reflect.Method.invokeNative(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:525)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1046)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:862)
        at dalvik.system.NativeStart.main(Native Method)
 Caused by: java.lang.NullPointerException
        at com.kongapps.smashlog.MainActivity.onCreate(MainActivity.java:52)
        at android.app.Activity.performCreate(Activity.java:5372)
        at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1104)
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2257)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2349)
        at android.app.ActivityThread.access$700(ActivityThread.java:159)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1316)
        at android.os.Handler.dispatchMessage(Handler.java:99)
        at android.os.Looper.loop(Looper.java:176)
        at android.app.ActivityThread.main(ActivityThread.java:5419)
        at java.lang.reflect.Method.invokeNative(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:525)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1046)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:862)
        at dalvik.system.NativeStart.main(Native Method)

我認為錯誤是這樣的:

活動A

i.putExtra("date", dateinput);

接着

活動B

String dateinput = smashdata.getString("datename");

您對該變量的調用方式有所不同。

我的建議是使用靜態常量避免此類錯誤。 :)

問候。

似乎您的字符串數組有問題,請檢查字符串的內部是否確實具有其他值,否則將得到Null錯誤

String[] info = {nameinput, ageinput, ethnicityinput, hairinput, dateinput, extrasinput};

現在以這種方式編寫您的信息數組以驗證其是否有效

String[] info = new String[]{"nameinput", "ageinput", "ethnicityinput", "hairinput", "dateinput", "extrasinput"};

在活動中創建靜態ArrayList,將所有值添加通過ListView適配器傳遞此數組

例:

 ArrayList<String>_array = new ArrayList<>();
    _array.add(nameinput);

在B活動中

ListAdapter lincadapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, A._array)

String [] info = new String [6]; 然后在info = {your values}中添加值; 這可能會解決您的空指針異常

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM