如何声明ArrayList? (预期标识符)

时间:2016-07-21 08:13:35

标签: java android arrays arraylist android-arrayadapter

我正忙着使用我在网上找到的教程在Android中构建一个基本的聊天应用程序。我复制了他们在示例中使用的代码,到目前为止没有任何问题。现在我收到以下错误:

C:\Users\Brian\AndroidStudioProjects\ChatApp\app\src\main\java\com\example\brian\chatapp\ChatAdapter.java
Error:(20, 14) error: <identifier> expected
Error:(20, 17) error: <identifier> expected
Error:(20, 29) error: <identifier> expected
Error:(20, 32) error: <identifier> expected
Error:(20, 49) error: <identifier> expected
Error:(22, 52) error: <identifier> expected
Error:(22, 53) error: ';' expected
Error:(22, 67) error: <identifier> expected
Error:(22, 70) error: <identifier> expected
Error:(22, 76) error: <identifier> expected
C:\Users\Brian\AndroidStudioProjects\ChatApp\app\src\main\java\com\example\brian\fragments\Chats.java
Error:(26, 28) error: <identifier> expected
Error:(26, 31) error: <identifier> expected
Error:(26, 43) error: <identifier> expected
Error:(26, 46) error: <identifier> expected
Error:(26, 56) error: <identifier> expected
Error:(47, 33) error: '(' or '[' expected
Error:(47, 48) error: not a statement
Error:(47, 54) error: lambda expressions are not supported in -source 1.7
(use -source 8 or higher to enable lambda expressions)
:app:compileDebugJavaWithJavac FAILED
Error:Execution failed for task ':app:compileDebugJavaWithJavac'.
> Compilation failed; see the compiler error output for details.
Information:BUILD FAILED
Information:Total time: 1.069 secs
Information:19 errors
Information:0 warnings
Information:See complete output in console

我对Arrays相当新,并希望在本教程的帮助下实际理解它们,所有其他类都已完美编译。这是代码:

package com.example.brian.chatapp;

import java.util.ArrayList;

import android.app.Activity;
import android.content.Context;
import android.graphics.Color;
import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.LinearLayout;
import android.widget.TextView;

public class ChatAdapter extends BaseAdapter {

private static LayoutInflater inflater = null;

ArrayList&lt;
ChatMessage&gt;
chatMessageList;

public ChatAdapter(Activity activity, ArrayList&lt;ChatMessage&gt; list) {
    chatMessageList = list;
    inflater = (LayoutInflater) activity
            .getSystemService(Context.LAYOUT_INFLATER_SERVICE);

}

@Override
public int getCount() {
    return chatMessageList.size();
}

@Override
public Object getItem(int position) {
    return position;
}

@Override
public long getItemId(int position) {
    return position;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    ChatMessage message = (ChatMessage) chatMessageList.get(position);
    View vi = convertView;
    if (convertView == null)
        vi = inflater.inflate(R.layout.chatbubble, null);

    TextView msg = (TextView) vi.findViewById(R.id.message_text);
    msg.setText(message.body);
    LinearLayout layout = (LinearLayout) vi
            .findViewById(R.id.bubble_layout);
    LinearLayout parent_layout = (LinearLayout) vi
            .findViewById(R.id.bubble_layout_parent);

    // if message is mine then align to right
    if (message.isMine) {
        layout.setBackgroundResource(R.drawable.bubble2);
        parent_layout.setGravity(Gravity.RIGHT);
    }
    // If not mine then align to left
    else {
        layout.setBackgroundResource(R.drawable.bubble1);
        parent_layout.setGravity(Gravity.LEFT);
    }
    msg.setTextColor(Color.BLACK);
    return vi;
}

public void add(ChatMessage object) {
    chatMessageList.add(object);
}
}

任何帮助将不胜感激!以下是我正在使用的教程的链接:http://www.tutorialsface.com/2015/08/building-your-own-android-chat-messenger-app-similar-to-whatsapp-using-xmpp-smack-4-1-api-from-scratch-part-1/

:编辑:

它似乎已经通过使用@ krzyk的方法解决了。但是现在我遇到了Chats.java类中的另一个问题,这里是代码:

package com.example.brian.fragments;

import java.util.ArrayList;
import java.util.Random;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v7.app.ActionBarActivity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.ListView;

import com.example.brian.chatapp.ChatAdapter;
import com.example.brian.chatapp.ChatMessage;
import com.example.brian.chatapp.CommonMethods;
import com.example.brian.chatapp.R;

public class Chats extends Fragment implements OnClickListener {

private EditText msg_edittext;
private String user1 = "khushi", user2 = "khushi1";
private Random random;
public static ArrayList<ChatMessage> chatlist;
public static ChatAdapter chatAdapter;
ListView msgListView;

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.chat_layout, container, false);
    random = new Random();
    ((ActionBarActivity) getActivity()).getSupportActionBar().setTitle(
            "Chats");
    msg_edittext = (EditText) view.findViewById(R.id.messageEditText);
    msgListView = (ListView) view.findViewById(R.id.msgListView);
    ImageButton sendButton = (ImageButton) view
            .findViewById(R.id.sendMessageButton);
    sendButton.setOnClickListener(this);

    // ----Set autoscroll of listview when a new message arrives----//
    msgListView.setTranscriptMode(ListView.TRANSCRIPT_MODE_ALWAYS_SCROLL);
    msgListView.setStackFromBottom(true);

    chatlist = new ArrayList<ChatMessage>();
    chatAdapter = new ChatAdapter(getActivity(), chatlist);
    msgListView.setAdapter(chatAdapter);
    return view;
}

@Override
public void onSaveInstanceState(Bundle outState) {
}

public void sendTextMessage(View v) {
    String message = msg_edittext.getEditableText().toString();
    if (!message.equalsIgnoreCase("")) {
        final ChatMessage chatMessage = new ChatMessage(user1, user2,
                message, "" + random.nextInt(1000), true);
        chatMessage.setMsgID();
        chatMessage.body = message;
        chatMessage.Date = CommonMethods.getCurrentDate();
        chatMessage.Time = CommonMethods.getCurrentTime();
        msg_edittext.setText("");
        chatAdapter.add(chatMessage);
        chatAdapter.notifyDataSetChanged();
    }
}

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

    }
}

}

以下是我现在得到的错误:

07-21 04:20:44.658 1987-1987/com.example.brian.chatapp E/AndroidRuntime: FATAL EXCEPTION: main
                                                                     Process: com.example.brian.chatapp, PID: 1987
                                                                     java.lang.ClassCastException: com.example.brian.chatapp.MainActivity cannot be cast to android.support.v7.app.ActionBarActivity
                                                                         at com.example.brian.fragments.Chats.onCreateView(Chats.java:35)
                                                                         at android.support.v4.app.Fragment.performCreateView(Fragment.java:2074)
                                                                         at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1104)
                                                                         at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1286)
                                                                         at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:758)
                                                                         at android.support.v4.app.FragmentManagerImpl.execSingleAction(FragmentManager.java:1632)
                                                                         at android.support.v4.app.BackStackRecord.commitNowAllowingStateLoss(BackStackRecord.java:637)
                                                                         at android.support.v4.app.FragmentPagerAdapter.finishUpdate(FragmentPagerAdapter.java:143)
                                                                         at android.support.v4.view.ViewPager.populate(ViewPager.java:1235)
                                                                         at android.support.v4.view.ViewPager.populate(ViewPager.java:1083)
                                                                         at android.support.v4.view.ViewPager.onMeasure(ViewPager.java:1609)
                                                                         at android.view.View.measure(View.java:17430)
                                                                         at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5463)
                                                                         at android.support.design.widget.CoordinatorLayout.onMeasureChild(CoordinatorLayout.java:669)
                                                                         at android.support.design.widget.HeaderScrollingViewBehavior.onMeasureChild(HeaderScrollingViewBehavior.java:89)
                                                                         at android.support.design.widget.AppBarLayout$ScrollingViewBehavior.onMeasureChild(AppBarLayout.java:1319)
                                                                         at android.support.design.widget.CoordinatorLayout.onMeasure(CoordinatorLayout.java:734)
                                                                         at android.view.View.measure(View.java:17430)
                                                                         at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5463)
                                                                         at android.widget.FrameLayout.onMeasure(FrameLayout.java:430)
                                                                         at android.support.v7.widget.ContentFrameLayout.onMeasure(ContentFrameLayout.java:135)
                                                                         at android.view.View.measure(View.java:17430)
                                                                         at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5463)
                                                                         at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1436)
                                                                         at android.widget.LinearLayout.measureVertical(LinearLayout.java:722)
                                                                         at android.widget.LinearLayout.onMeasure(LinearLayout.java:613)
                                                                         at android.view.View.measure(View.java:17430)
                                                                         at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5463)
                                                                         at android.widget.FrameLayout.onMeasure(FrameLayout.java:430)
                                                                         at android.view.View.measure(View.java:17430)
                                                                         at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5463)
                                                                         at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1436)
                                                                         at android.widget.LinearLayout.measureVertical(LinearLayout.java:722)
                                                                         at android.widget.LinearLayout.onMeasure(LinearLayout.java:613)
                                                                         at android.view.View.measure(View.java:17430)
                                                                         at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5463)
                                                                         at android.widget.FrameLayout.onMeasure(FrameLayout.java:430)
                                                                         at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2560)
                                                                         at android.view.View.measure(View.java:17430)
                                                                         at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:2001)
                                                                         at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1166)
                                                                         at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1372)
                                                                         at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1054)
                                                                         at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:5779)
                                                                         at android.view.Choreographer$CallbackRecord.run(Choreographer.java:767)
                                                                         at android.view.Choreographer.doCallbacks(Choreographer.java:580)
                                                                         at android.view.Choreographer.doFrame(Choreographer.java:550)
                                                                         at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:753)
                                                                         at android.os.Handler.handleCallback(Handler.java:739)
                                                                         at android.os.Handler.dispatchMessage(Handler.java:95)
                                                                         at android.os.Looper.loop(Looper.java:135)
                                                                         at android.app.ActivityThread.main(ActivityThread.java:5221)
                                                                         at java.lang.reflect.Method.invoke(Native Method)
                                                                         at java.lang.reflect.Method.invoke(Method.java:372)
                                                                        at   com.android.internal.os.ZygoteInit$MethodAndArgsCaller
07-21 04:21:29.569 1987-1987/com.example.brian.chatapp I/Process: Sending signal. PID: 1987 SIG: 9

2 个答案:

答案 0 :(得分:3)

变化:

ArrayList&lt;
ChatMessage&gt;
chatMessageList;

public ChatAdapter(Activity activity, ArrayList&lt;ChatMessage&gt; list) {

为:

ArrayList<ChatMessage> chatMessageList;

public ChatAdapter(Activity activity, ArrayList<ChatMessage> list) {

如果它显示了&lt;&gt;等html实体,那么您的教程写得很糟糕。如果您遇到它们,只需将&lt;更改为<,将&gt;更改为>

答案 1 :(得分:1)

您需要删除&amp; lt和&amp; gt:

ArrayList<ChatMessage> chatMessageList;

public ChatAdapter(Activity activity, ArrayList<ChatMessage> list) {
    chatMessageList = list;
    inflater = (LayoutInflater) activity
        .getSystemService(Context.LAYOUT_INFLATER_SERVICE);

}