无法使用特定对象

时间:2017-11-04 20:22:56

标签: java android xml android-layout

我正在使用一个简单的Messenger应用程序(学习)。

我可以成功登录并查看信息中心活动。但是当Dashboard Activity尝试初始化一些"伪消息"时,我得到一个NullPointer异常。

activity_dashboard.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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:orientation="vertical"
    tools:context="com.example.messenger.DashboardActivity">

    <ScrollView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_weight="1">

        <LinearLayout
            android:id="@+id/containerLayout"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:background="@color/dirtyBlue">

        </LinearLayout>
    </ScrollView>

</LinearLayout>

这是我想为每条消息初始化的消息布局,并将它们放在scrollview的线性布局中

message.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:padding="5dp"
    android:layout_weight="1"
    android:background="@color/dirtyWhite"
    android:onClick="openMsg">

        <TextView
            android:id="@+id/senderUserText"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Sender Text"
            android:textSize="20sp"/>
        <TextView
            android:id="@+id/msgText"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Message Text!"/>

</LinearLayout>

以下是Dashboard Activity.java中的内容

DashboardActivity.java

public class DashboardActivity extends AppCompatActivity {


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_dashboard);
    ParseMessages();
}

这里是&#34; ParseMessages()&#34;功能有效。

Database_IO.java

public static List<Message> msgList;
 public static List<Message> ParseMessageList()
{
    GenerateRandomMessages();
    return msgList;
}
public static void GenerateRandomMessages()
{
    Message message1 = new Message();
    message1.Message = "MSG1";
    message1.senderID = "Sender1";
    message1.recipientID = "Me";
    message1.isRead = false;

    Message message2 = new Message();
    message1.Message = "MSG2";
    message1.senderID = "Sender2";
    message1.recipientID = "Me";
    message1.isRead = true;

    msgList.add(message1);
    msgList.add(message2);
    Log.e("Message 1", message1.Message);
    Log.e("Message 2", message1.Message);
}

NullPointerException显示以下行:

msgList.add(message1);
msgList.add(message2);

1 个答案:

答案 0 :(得分:1)

您需要初始化您的msgList!

可选:您可能想要更正message2(您当前只将数据分配给message1)

执行此操作:

 public static void GenerateRandomMessages()
{
//initialize msgList
msgList = new ArrayList();

//如果你想纠正message2

   Message message2 = new Message();
    message2.Message = "MSG2";
    message2.senderID = "Sender2";
    message2.recipientID = "Me";
    message2.isRead = true;

    msgList.add(message1);
    msgList.add(message2);
    Log.e("Message 1", message1.Message);
//here also, you were using message 1
    Log.e("Message 2", message2.Message);