我正在尝试使用intent [duplicate]填充listview

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了我正在尝试使用intent [duplicate]填充listview相关的知识,希望对你有一定的参考价值。

这个问题在这里已有答案:

我正在尝试让用户将我的应用程序加载到包含列表视图的主活动,然后转到第二个活动并输入他们的“名称”和“评论”,将其传递回主要活动并将其显示为吐司以及将其放在列表视图中。但是,当我尝试时,我的应用程序崩溃了。为什么?

activity_main.xml中

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/colorBackground"
    tools:context="com.example.MainActivity">


    <Button
        android:id="@+id/contact"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Contact Us"
        android:layout_marginRight="46dp"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        android:layout_marginTop="8dp"
        app:layout_constraintBottom_toBottomOf="parent"
        android:layout_marginBottom="8dp"
        app:layout_constraintVertical_bias="0.982"
        android:layout_marginEnd="46dp" />

    <Button
        android:id="@+id/comments"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Add Comments"
        android:layout_marginLeft="8dp"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toLeftOf="@+id/contact"
        android:layout_marginRight="8dp"
        app:layout_constraintBottom_toBottomOf="parent"
        android:layout_marginBottom="8dp"
        app:layout_constraintTop_toTopOf="parent"
        android:layout_marginTop="8dp"
        app:layout_constraintHorizontal_bias="0.418"
        app:layout_constraintVertical_bias="0.982"
        android:layout_marginStart="8dp"
        android:layout_marginEnd="8dp" />

    <ListView
        android:id="@+id/ListViewMain"
        android:layout_width="207dp"
        android:layout_height="243dp"
        android:layout_marginLeft="8dp"
        android:textColor="@android:color/white"
        app:layout_constraintLeft_toLeftOf="parent"
        android:layout_marginRight="8dp"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        android:layout_marginTop="8dp"
        app:layout_constraintBottom_toBottomOf="parent"
        android:layout_marginBottom="8dp"
        app:layout_constraintHorizontal_bias="0.503"
        app:layout_constraintVertical_bias="0.253" />

    <TextView
        android:id="@+id/textView3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginBottom="8dp"
        android:layout_marginLeft="8dp"
        android:layout_marginRight="8dp"
        android:layout_marginTop="8dp"
        android:text="Comments"
        android:textColor="@android:color/white"
        android:textStyle="bold"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0.062"
        app:layout_constraintHorizontal_bias="0.456" />

</android.support.constraint.ConstraintLayout>

main activity.Java

package com.example;

import android.content.Context;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {

    private Button comments;
    private Button contact;
    ListView lv;
    private String Name = "";
    private String Comments = "";
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

         initViews();
        initListeners();
        lv = (ListView) findViewById(R.id.ListViewMain);
        Bundle extras = getIntent().getExtras();
                if (extras != null)
                {
                    Name = extras.getString("NAME");
                    Comments = extras.getString("COMMENTS");

                }


            String[] values = new String[] { Name, Comments};

            ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
                    android.R.layout.simple_list_item_1, android.R.id.text1, values);

            lv.setAdapter(adapter);


    }

    private void initViews(){
        comments = (Button) findViewById(R.id.comments);
        contact = (Button) findViewById(R.id.contact);





    }

    private void initListeners(){
        comments.setOnClickListener(this);
        contact.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.comments:

                Context context = getApplicationContext();
                CharSequence text = "My name";
                int duration = Toast.LENGTH_SHORT;

                Toast toast = Toast.makeText(context, text, duration);
                toast.show();

                Intent intent2 = new Intent(getApplicationContext(), CommentsActivity.class);
                startActivity(intent2);

                break;
            case R.id.contact:
                Intent intent = new Intent(Intent.ACTION_SEND);
                intent.setType("text/plain");
                intent.putExtra(Intent.EXTRA_EMAIL, "myemail");
                intent.putExtra(Intent.EXTRA_SUBJECT, "Subject");
                intent.putExtra(Intent.EXTRA_TEXT, "Hello!");

                startActivity(Intent.createChooser(intent, "Send Email"));
                break;
        }
    }
}

activity_comments.xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/colorBackground"
    tools:context="com.example.CommentsActivity">

    <TextView
        android:id="@+id/textView2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginBottom="8dp"
        android:layout_marginTop="8dp"
        android:text="Comments:"
        android:textColor="@android:color/white"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0.378"
        android:layout_marginLeft="8dp"
        app:layout_constraintLeft_toLeftOf="parent"
        android:layout_marginRight="8dp"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintHorizontal_bias="0.087" />

    <EditText
        android:id="@+id/COMMENTS"
        android:layout_width="316dp"
        android:layout_height="53dp"
        android:layout_marginBottom="8dp"
        android:layout_marginLeft="8dp"
        android:layout_marginRight="8dp"
        android:layout_marginTop="8dp"
        android:inputType="text"
        android:textColor="@android:color/white"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0.432" />

    <EditText
        android:id="@+id/NAME"
        android:layout_width="316dp"
        android:layout_height="53dp"
        android:layout_marginBottom="8dp"
        android:layout_marginLeft="8dp"
        android:layout_marginRight="8dp"
        android:layout_marginTop="8dp"
        android:inputType="text"
        android:textColor="@android:color/white"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0.174" />

    <Button
        android:id="@+id/submit"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Submit"
        app:layout_constraintTop_toTopOf="parent"
        android:layout_marginTop="8dp"
        app:layout_constraintBottom_toBottomOf="parent"
        android:layout_marginBottom="8dp"
        android:layout_marginRight="8dp"
        app:layout_constraintRight_toRightOf="parent"
        android:layout_marginLeft="8dp"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintVertical_bias="0.894" />

    <TextView
        android:id="@+id/textView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Name:"
        android:textColor="@android:color/white"
        app:layout_constraintTop_toTopOf="parent"
        android:layout_marginTop="8dp"
        app:layout_constraintBottom_toBottomOf="parent"
        android:layout_marginBottom="8dp"
        app:layout_constraintVertical_bias="0.14"
        android:layout_marginLeft="8dp"
        app:layout_constraintLeft_toLeftOf="parent"
        android:layout_marginRight="8dp"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintHorizontal_bias="0.079" />

</android.support.constraint.ConstraintLayout>

comments activity.Java

package com.example;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class CommentsActivity extends AppCompatActivity implements View.OnClickListener {

    private Button submit;
    private EditText edNAME;
    private EditText edCOMMENTS;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_comments);
        initViews();
        initListeners();
         edNAME  = (EditText)findViewById(R.id.NAME);
        edCOMMENTS  = (EditText)findViewById(R.id.COMMENTS);

    }

    private void initListeners(){
        submit.setOnClickListener(this);

    }

    private void initViews(){
        submit = (Button) findViewById(R.id.submit);

    }

    @Override
    public void onClick(View v) {

        switch (v.getId()) {
            case R.id.submit:



                CharSequence text = edNAME.getText().toString() + " - "  + edCOMMENTS.getText().toString();
                int duration = Toast.LENGTH_SHORT;
                Toast toast = Toast.makeText(getApplicationContext(), text, duration);
                toast.show();

                Intent intent = new Intent(getBaseContext(), MainActivity.class);
                intent.putExtra("NAME",  edNAME.getText());
                intent.putExtra("COMMENTS",  edCOMMENTS.getText());
                startActivity(intent);

                break;
        }

    }
}

错误信息:

12-22 14:31:58.765 1253-1253 / com.example E / AndroidRuntime:FATAL EXCEPTION:main进程:com.example,PID:1253 java.lang.NullPointerException:尝试调用虚方法'java.lang.String java android.widget.AbsListView上的android.widget.ArrayAdpter.ArrayAdapter.getView(ArrayAdapter.java:407)上android.widget.ArrayAdapter.createViewFromResource(ArrayAdapter.java:445)的空对象引用上的.lang.Object.toString()' .obtainView(AbsListView.java:2372)位于android.widget.List.fillFromTop(ListView。)的android.widget.ListView.makeAndAddView(ListView.java:2052)android.widget.ListView.fillDown(ListView.java:786)。 java:847)在android.widget.AtListView.onLayout(AbsListView.java:2171)的android.widget.List.layoutChildren(ListView.java:1826)android.view.View.layout(View.java:19586)at android.view.Viewout.layout(ViewGroup.java:6053)在android.view.conout上的android.sview.constraint.ConstraintLayout.onLayout(ConstraintLayout.java:1197)android.view.layout(View.java:19586) .VI ewGroup.layout(ViewGroup.java:6053)在Android.widget.FrameLayout.layout中的android.widget.FrameLayout.layoutChildren(FrameLayout.java:323)处于android.view.View.layout(ViewLayout.java:261) .java:19586)在android.view.View.layout(View。的Android.support.v7.widget.ActionBarOverlayLayout.onLayout(ActionBarOverlayLayout.java:434)的android.view.ViewGroup.layout(ViewGroup.java:6053)。 java:19586)在Android.widget.FrameLayout.onLayout(FrameLayout.java:261)的android.widget.FrameLayout.layoutChildren(FrameLayout.java:323)的android.view.ViewGroup.layout(ViewGroup.java:6053) Android.widget.LinarLayout上的android.view.View.layout(View.java:19586)在android.widget.LinearLayout.setChildFrame(LinearLayout.java:1791)的android.view.ViewGroup.layout(ViewGroup.java:6053) .layoutVertical(LinearLayout.java:1635)位于android.view.View.Loutout(DirectLayout.java:1544),位于android.view.View.layout(View.java:19586),位于android.view.ViewGroup.layout(ViewGroup。 java:6053)在android.widg在android.view的com.android.internal.policy.DecorView.onLayout(DecorView.java:758)的android.widget.FrameLayout.onLayout(FrameLayout.java:261)上的et.FrameLayout.layoutChildren(FrameLayout.java:323) .view.layout(View.java:19586)位于android.view.ViewRootImpl.performTraversals的android.view.View.Linout(ViewRootImpl.java:2484)上的android.view.ViewGroup.layout(ViewGroup.java:6053) ViewRootImpl.java:2200)在android.view.ViewRootImpl上的android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1386)$ traversalRunnable.run(ViewRootImpl.java:6733)在android.view.Choreographer $ CallbackRecord.run(编舞) .java:911)在Android.view.Choreographer(Page 4)上的android.view.doCallbacks(Choreographer.java:723),android.view.Choreographer $ FrameDisplayEventReceiver.run(Choreographer.java: 897)在android.os.Handler.handleCallback(Handler.java:789)的android.os.Handler.dispatchMessage(Handler.java:98)在android.os.Looper.loop(Looper.java:164)at android.app.ActivityThread.main(ActivityThread.java:6541)at java.lang.reflect.Method.invoke(Native Method)at com.android.internal.os.Zygote $ MethodAndArgsCaller.run(Zygote.java:240)at at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:767)

                                                                       [ 12-22 14:31:58.766  1253: 1347 D/         ]
                                                                       SurfaceInterface::setAsyncMode: set async mode 1
答案

ArrayAdapter中传递的数组中必须至少有一个空值。在初始化Name之前,请在Comments类中记录变量MainActivityArrayAdapter的值。

以上是关于我正在尝试使用intent [duplicate]填充listview的主要内容,如果未能解决你的问题,请参考以下文章

每当我想从我的班级运行 Intent 时,NPE [重复]

调用前台服务时应用程序崩溃[重复]

应用程序在“startActivity(intent);”上崩溃[复制]

[Symbol(code)]: 'CLIENT_MISSING_INTENTS' (Discord.js) [duplicate]

清理Intent文件URI

尝试使用 Intent 获取字符串时未设置 .child()