我尝试使用intent

时间:2017-12-22 19:33:36

标签: java android android-layout listview nullpointerexception

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

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>

MainActivity.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>

CommentsActivity.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   例外:主要                                                                              过程:com.example,PID:1253                                                                              java.lang.NullPointerException:尝试调用虚方法   &#39; java.lang.String java.lang.Object.toString()&#39;在null对象上   参考                                                                                  在   android.widget.ArrayAdapter.createViewFromResource(ArrayAdapter.java:445)                                                                                  在android.widget.ArrayAdapter.getView(ArrayAdapter.java:407)                                                                                  在android.widget.AbsListView.obtainView(AbsListView.java:2372)                                                                                  在android.widget.ListView.makeAndAddView(ListView.java:2052)                                                                                  在android.widget.ListView.fillDown(ListView.java:786)                                                                                  在android.widget.ListView.fillFromTop(ListView.java:847)                                                                                  在android.widget.ListView.layoutChildren(ListView.java:1826)                                                                                  在android.widget.AbsListView.onLayout(AbsListView.java:2171)                                                                                  在android.view.View.layout(View.java:19586)                                                                                  在android.view.ViewGroup.layout(ViewGroup.java:6053)                                                                                  在   android.support.constraint.ConstraintLayout.onLayout(ConstraintLayout.java:1197)                                                                                  在android.view.View.layout(View.java:19586)                                                                                  在android.view.ViewGroup.layout(ViewGroup.java:6053)                                                                                  在android.widget.FrameLayout.layoutChildren(FrameLayout.java:323)                                                                                  在android.widget.FrameLayout.onLayout(FrameLayout.java:261)                                                                                  在android.view.View.layout(View.java:19586)                                                                                  在android.view.ViewGroup.layout(ViewGroup.java:6053)                                                                                  在   android.support.v7.widget.ActionBarOverlayLayout.onLayout(ActionBarOverlayLayout.java:434)                                                                                  在android.view.View.layout(View.java:19586)                                                                                  在android.view.ViewGroup.layout(ViewGroup.java:6053)                                                                                  在android.widget.FrameLayout.layoutChildren(FrameLayout.java:323)                                                                                  在android.widget.FrameLayout.onLayout(FrameLayout.java:261)                                                                                  在android.view.View.layout(View.java:19586)                                                                                  在android.view.ViewGroup.layout(ViewGroup.java:6053)                                                                                  在android.widget.LinearLayout.setChildFrame(LinearLayout.java:1791)                                                                                  在android.widget.LinearLayout.layoutVertical(LinearLayout.java:1635)                                                                                  在android.widget.LinearLayout.onLayout(LinearLayout.java:1544)                                                                                  在android.view.View.layout(View.java:19586)                                                                                  在android.view.ViewGroup.layout(ViewGroup.java:6053)                                                                                  在android.widget.FrameLayout.layoutChildren(FrameLayout.java:323)                                                                                  在android.widget.FrameLayout.onLayout(FrameLayout.java:261)                                                                                  在com.android.internal.policy.DecorView.onLayout(DecorView.java:758)                                                                                  在android.view.View.layout(View.java:19586)                                                                                  在android.view.ViewGroup.layout(ViewGroup.java:6053)                                                                                  在android.view.ViewRootImpl.performLayout(ViewRootImpl.java:2484)                                                                                  在android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:2200)                                                                                  在android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1386)                                                                                  在   android.view.ViewRootImpl $ TraversalRunnable.run(ViewRootImpl.java:6733)                                                                                  在   android.view.Choreographer $ CallbackRecord.run(Choreographer.java:911)                                                                                  在android.view.Choreographer.doCallbacks(Choreographer.java:723)                                                                                  在android.view.Choreographer.doFrame(Choreographer.java:658)                                                                                  在   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)                                                                                  在android.app.ActivityThread.main(ActivityThread.java:6541)                                                                                  at java.lang.reflect.Method.invoke(Native Method)                                                                                  在   com.android.internal.os.Zygote $ MethodAndArgsCaller.run(Zygote.java:240)                                                                                  在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:767)

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

1 个答案:

答案 0 :(得分:0)

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