布局类似于whatsapp

时间:2014-02-05 16:24:28

标签: android layout

我正在设计与Whatsapp应用程序(显示聊天)界面相同的布局...有一些错误我不知道发生了什么......

 package com.example.coder;

    import java.util.ArrayList;
    import java.util.List;

    import com.example.coder.Custom.Row;
    import com.example.coder.CustomArrayAdapter.ViewHolder;
    import com.example.coder.Whats.MessageData;

    import android.app.Activity;
    import android.app.ListActivity;
    import android.content.Context;
    import android.os.Bundle;
    import android.view.LayoutInflater;
    import android.view.View;
    import android.view.View.OnClickListener;
    import android.view.ViewGroup;
    import android.widget.ArrayAdapter;
    import android.widget.Button;
    import android.widget.EditText;
    import android.widget.ImageView;
    import android.widget.ListView;
    import android.widget.TextView;

    public class Whats extends Activity {
        MessageAdapter<MessageData> adapter;
        EditText message;
        List<MessageData> msgs;
        Button b;

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

             message = (EditText) findViewById(R.id.enter_message);

            ListView lv = (ListView) findViewById(R.id.list);
            msgs =  new ArrayList<MessageData>();

            b=(Button)findViewById(R.id.send_button);

             adapter=new MessageAdapter<MessageData>(this,R.layout.whats,msgs);
             lv.setAdapter(adapter);
        }


        public void sendMessage() {    

            String mText = message.getText().toString();
            msgs.add(new MessageData(mText));
            adapter.notifyDataSetChanged();
            message.setText("");
        }
        public class MessageData {
            private String message;

            public MessageData(String message) {
                this.message = message;
            }
            public void setMessage(String message) {
                this.message = message;
            }
            public String getMessage() {
                return message;
            }
        }
        public class MessageAdapter<T> extends ArrayAdapter<MessageData> {


            Context context;
        public MessageAdapter(Context context,int resource, List<MessageData> objs) {
            super(context,resource,objs);
           this.context=context;

        }
        @Override
        public View getView(int position, View convertView, ViewGroup parent) {



            MessageView msgView = null;

            LayoutInflater mInflater = (LayoutInflater) context.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
            if(convertView == null)
            {
                // Get a new instance of the row layout view

                convertView = mInflater.inflate(R.layout.whats, null);

                // Hold the view objects in an object,
                // so they don't need to be re-fetched
                msgView = new MessageView();
                msgView.msg = (TextView) convertView.findViewById(R.id.message_text);

                // Cache the view objects in the tag,
                // so they can be re-accessed later
                convertView.setTag(msgView);
            } else {
                msgView = (MessageView) convertView.getTag();
            }

            // Transfer the stock data from the data object
            // to the view objects
            MessageData currentMsg =  getItem(position);
            msgView.msg.setText(currentMsg.getMessage());

            return convertView;
        }

          class MessageView {
             TextView msg;
        }
    }

    }

Whats活动的布局这是Whats活动使用的布局......

  <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        tools:context=".MainActivity"
        android:orientation="vertical">


        <!--<FrameLayout-->
            <!--android:background="@color/header_color"-->
            <!--android:layout_width="match_parent"-->
            <!--android:layout_height="0dp"-->
            <!--android:layout_weight="1">-->

        <!--</FrameLayout>-->

        <LinearLayout
            android:layout_width="match_parent"
            android:orientation="vertical"
            android:layout_height="0dp"
            android:layout_weight="10">

            <FrameLayout
                android:layout_height="0dp"
                android:layout_width="match_parent"
                android:layout_weight="11">

                <ListView
                    android:id="@+android:id/list"

                    android:drawSelectorOnTop="false"
                    android:layout_height="match_parent"
                    android:layout_width="match_parent"
                    android:footerDividersEnabled="true">


                </ListView>

            </FrameLayout>


            <FrameLayout
                android:layout_height="0dp"
                android:layout_width="match_parent"
                android:layout_weight="1">

                <LinearLayout
                    android:layout_height="match_parent"
                    android:layout_width="match_parent"

                    android:id="@+id/linearLayout">

                    <EditText
                        android:id="@+id/enter_message"
                        android:layout_width="0dp"
                        android:layout_height="match_parent"
                        android:layout_weight="1"
                        android:hint="edit_text" />

                    <Button
                        android:id="@+id/send_button"
                        android:layout_width="45dp"
                        android:layout_height="30dp"

                        android:onClick="sendMessage"/>

                </LinearLayout>

            </FrameLayout>

        </LinearLayout>

    </LinearLayout>

单个文字的布局

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:gravity="fill_horizontal"
    android:layout_height="wrap_content">

    <RelativeLayout
        android:layout_height="wrap_content"
        android:layout_width="match_parent">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:gravity="right"
            android:layout_marginTop="@dimen/activity_horizontal_margin"

            android:id="@+id/message_text" />

    </RelativeLayout>

</LinearLayout>

* 错误日志*

02-05 21:41:58.560: E/AndroidRuntime(2346): in writeCrashedAppName, pkgName :com.example.coder
02-05 21:41:58.560: D/AndroidRuntime(2346): file written successfully with content: com.example.coder StringBuffer : ;com.example.coder
02-05 21:41:58.570: I/Process(2346): Sending signal. PID: 2346 SIG: 9
02-05 21:41:58.570: E/AndroidRuntime(2346): FATAL EXCEPTION: main
02-05 21:41:58.570: E/AndroidRuntime(2346): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.coder/com.example.coder.Whats}: java.lang.NullPointerException
02-05 21:41:58.570: E/AndroidRuntime(2346):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1647)
02-05 21:41:58.570: E/AndroidRuntime(2346):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
02-05 21:41:58.570: E/AndroidRuntime(2346):     at android.app.ActivityThread.access$1500(ActivityThread.java:117)
02-05 21:41:58.570: E/AndroidRuntime(2346):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
02-05 21:41:58.570: E/AndroidRuntime(2346):     at android.os.Handler.dispatchMessage(Handler.java:99)
02-05 21:41:58.570: E/AndroidRuntime(2346):     at android.os.Looper.loop(Looper.java:130)
02-05 21:41:58.570: E/AndroidRuntime(2346):     at android.app.ActivityThread.main(ActivityThread.java:3683)
02-05 21:41:58.570: E/AndroidRuntime(2346):     at java.lang.reflect.Method.invokeNative(Native Method)
02-05 21:41:58.570: E/AndroidRuntime(2346):     at java.lang.reflect.Method.invoke(Method.java:507)
02-05 21:41:58.570: E/AndroidRuntime(2346):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:880)
02-05 21:41:58.570: E/AndroidRuntime(2346):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:638)
02-05 21:41:58.570: E/AndroidRuntime(2346):     at dalvik.system.NativeStart.main(Native Method)
02-05 21:41:58.570: E/AndroidRuntime(2346): Caused by: java.lang.NullPointerException
02-05 21:41:58.570: E/AndroidRuntime(2346):     at com.example.coder.Whats.onCreate(Whats.java:44)
02-05 21:41:58.570: E/AndroidRuntime(2346):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049)
02-05 21:41:58.570: E/AndroidRuntime(2346):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1611)

1 个答案:

答案 0 :(得分:1)

我会说你的ListView因此而为null:

<ListView
    android:id="@+android:id/list"

将其替换为

android:id="@+id/list"