当我点击main_activity的按钮时,它显示"不幸的是,App_name已停止"

时间:2016-08-11 22:20:07

标签: android android-activity onclick onclicklistener

您好我是android开发的新手,我正在制作一个简单的短信应用程序,包含两个活动。在第一个活动我已经采取了34个按钮,第二个活动显示点击按钮的内容,但是当点击按钮时,它给出了遗憾。应用已停止。解压我的问题,请提前感谢。

activity_friend_list.xml

<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:background="@drawable/e">
    <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"
        android:layout_margin="15dp"
        android:orientation="vertical"
        tools:context="com.example.abc.msgdemo.FriendList">


        <Button
            style="@style/btn_txt_style"
            android:id="@+id/button1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="&quot;A single candle can illuminate an entire room. A true friend lights up an entire lifetime. Thanks for the bright lights of ur friendship.&quot;" />

        <Button
            style="@style/btn_txt_style"
            android:id="@+id/button2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="&quot;A friend is sweet when it is new. And it is sweeter when it is true. But you know what? It is sweetest when it is u.Happy Friendship Day&quot;" />

        <Button
            style="@style/btn_txt_style"
            android:id="@+id/button3"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="&quot;A coin is easy to earn, a friend is hard to find. The coin depreciates but a friend appreciates. I lost a coin when I smsed u, but it&apos;s okay because I got u.Happy Friendship Day  &quot;" />

        <Button
            style="@style/btn_txt_style"
            android:id="@+id/button4"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="&quot;If you open my heart, guess what u r gonna see? It&apos;s U. True friends are hard to find so I kept u.Happy Friendship Day  &quot;" />

        <Button
            style="@style/btn_txt_style"
            android:id="@+id/button5"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="&quot;How long shall v b friends? Do u want a clue? As long as stars twinkle in the sky, till the water runs dry &amp; till the day I die. We will b friends.Happy Friendship Day  &quot;" />

        <Button
            style="@style/btn_txt_style"
            android:id="@+id/button6"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="&quot;I might run out of cute graphics to send u, but I&apos;ll never run out of this: care 4 u.  &quot;" />

        <Button
            style="@style/btn_txt_style"
            android:id="@+id/button7"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="&quot;A friend is a push when you have stopped, a chat when u r lonely, a guide when u r searching, a smile when u r sad, a song when u r glad.Happy Friendship Day &quot;" />

        <Button
            style="@style/btn_txt_style"
            android:id="@+id/button8"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="&quot;Good Relations are just like &quot;Water&quot; No Colour , No Shape, No Smell, NO Taste. But Still Very Very &quot;Important&quot; For Life.&quot;" />

        <Button
            style="@style/btn_txt_style"
            android:id="@+id/button9"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="&quot;Relationship between two person is like the relationship between the hand and the eye. If the hand gets hurt, the eye cries, and if the eye cries the hand wipes its tears&quot;" />

        <Button
            style="@style/btn_txt_style"
            android:id="@+id/button10"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="&quot; Friendship is the heaviest coin in the international bank of love! and with your friendship, I am the richest person on earth!  So keep me rich forever. &quot;" />


        /////////////////////


        <Button
            style="@style/btn_txt_style"
            android:id="@+id/button11"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="&quot; A very short but very nice line for a true relation-  &quot;Hurt me with the truth, But Never comfort me with a lie.&quot; &quot;" />

        <Button
            style="@style/btn_txt_style"
            android:id="@+id/button12"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="&quot;I have a pen which is blue, I have a friend which is you. Flowers will die, waters will dry, but our friendship will never say goodbye.&quot;" />

        <Button
            style="@style/btn_txt_style"
            android:id="@+id/button13"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="&quot;Colors may fade, the sun may not shine, the moon may not be bright, heartbeats may stop, lives may pass but our friendship, I&apos;ll treasure &apos;till the day my heart stops.&quot;" />

        <Button
            style="@style/btn_txt_style"
            android:id="@+id/button14"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="&quot;When you&apos;re down, I&apos;ll be there to cheer you up.  When you&apos;re tired, I&apos;ll be there to lift you up.  I&apos;ll be your friend no matter what! &quot;" />

        <Button
            style="@style/btn_txt_style"
            android:id="@+id/button15"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="&quot;Years may fly, tears may dry, but my friendship with u will never die.&quot;" />

        <Button
            style="@style/btn_txt_style"
            android:id="@+id/button16"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="&quot;&quot;FEELING&quot;is a painting never spoil it &quot;FACE&quot;is a book try to read it &quot;LOVE&quot; is precious be ready to sacrifice for it &quot;FRiENDSHiP&quot; is a mirror never&quot;break&quot; it.&quot;" />

        <Button
            style="@style/btn_txt_style"
            android:id="@+id/button17"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="&quot; Dillagi dosto ke naam hoti hai Dilldari dosto ki shaan hoti hai  Kahi bhi raho par rahoge dil me mere Yahi sachchi dosti ki pehchaan hoti hai &quot;" />

        <Button
            style="@style/btn_txt_style"
            android:id="@+id/button18"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="&quot;Friendship often ends in love; but love ends in friendship - NEVER.&quot;" />

        <Button
            style="@style/btn_txt_style"
            android:id="@+id/button19"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="&quot;Make your life a house your heart can live in. With a door that is open to receive friends. And a garden full of memories of many good things. &quot;" />

        <Button
            style="@style/btn_txt_style"
            android:id="@+id/button20"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="&quot;  When u rise up, Your friend know who you are.  When you fall down, You know who your friend are. &quot;" />


        ///////////////////////

        <Button
            style="@style/btn_txt_style"
            android:id="@+id/buttonx1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="&quot;The ball can escape from bat, The rat can escape from cat, But u cannot escape from &quot;MY HEART U R Always my good friend&quot;&quot;" />

        <Button
            style="@style/btn_txt_style"
            android:id="@+id/buttonx2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="&quot;Distance never separates any relation &amp; Time never build any relation. If Feelings are True From Heart, Then &apos;Friends are always &quot;Friends&quot; forever.&quot;" />

        <Button
            style="@style/btn_txt_style"
            android:id="@+id/buttonx3"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="&quot;You are a part of a puzzle 0f someone&apos;s life. You may never kn0w where you fit, but always remember that someone&apos;s life may not be complete without you!:-)&quot;" />

        <Button
            style="@style/btn_txt_style"
            android:id="@+id/buttonx4"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="&quot;There are only two reasons why people can hurt you. 1-You really care about them. 2-They don&apos;t really care about you.Happy Friendship Day&quot;" />

        <Button
            style="@style/btn_txt_style"
            android:id="@+id/buttonx5"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="&quot;A single rose can be my garden A single friend, my world.Happy Friendship Day&quot;" />

        <Button
            style="@style/btn_txt_style"
            android:id="@+id/buttonx6"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="&quot;A coin is easy to earn, a friend is hard to find. The coin depreciates but a friend appreciates. I lost a coin when I texted you, but it&apos;s okay because I got you.&quot;" />

        <Button
            style="@style/btn_txt_style"
            android:id="@+id/buttonx7"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="&quot;  All friends are not True. But true friends are very few,  Which includes U. &quot;" />

        <Button
            style="@style/btn_txt_style"
            android:id="@+id/buttonx8"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="&quot;1 Stone is enough 2 break a glass, 1 sentence is enough 2 break a heart, 1 word is enough to fall in love &amp; 1 friend is enough to live in this world&quot;" />

        <Button
            style="@style/btn_txt_style"
            android:id="@+id/buttonx9"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="&quot;My past says u met me. My future says u will always remember me. My dream says u care for me. But my heart says this friend is born for me.&quot;" />

        <Button
            style="@style/btn_txt_style"
            android:id="@+id/buttonx10"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="&quot;Friends are like stars. You can&apos;t always see them, But you know they are always there for you.&quot;" />

        <Button
            style="@style/btn_txt_style"
            android:id="@+id/buttonx11"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="&quot; There are no strings attached to a friendship, no bonds to hold or ties to bind.  Friendship stand on its own. &quot;" />

        <Button
            style="@style/btn_txt_style"
            android:id="@+id/buttonx12"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="&quot;Some one asked me 4 how long will u both be friends? i remained silent bcoz i didnt know which is longer.. &quot;ALWAYS&quot; or &quot;FOREVER&quot;. &quot;" />

        <Button
            style="@style/btn_txt_style"
            android:id="@+id/buttonx13"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="&quot;Don&apos;t Love a Friend who Hurts U Don&apos;t Hurt a Friend who Loves U.&quot;" />
        <Button
            style="@style/btn_txt_style"
            android:id="@+id/buttonx14"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="&quot;My friendship is nothing but a round, Wandering why? Because it has no end.Happy Friendship Day&quot;" />



    </LinearLayout></ScrollView>

FriendList.java
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

public class FriendList extends AppCompatActivity implements OnClickListener {
    Button[] FrendBtn;Button[]  FrendBtnx;
    int i;

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


        FrendBtn = new Button[20];
        FrendBtnx = new Button[14];

        for (int i = 0; i < FrendBtn.length; i++) {
            {
                String buttonID = "button" + (i + 1);

                int resID = getResources().getIdentifier(buttonID, "id", getPackageName());
                FrendBtn[i] = ((Button) findViewById(resID));
                FrendBtn[i].setOnClickListener(this);
            }
        }


        for (int j = 0; j <FrendBtnx.length; j++) {
            {
                String buttonID = "buttonx" + (j + 1);

                int resID = getResources().getIdentifier(buttonID, "id", getPackageName());
                FrendBtnx[j] = ((Button) findViewById(resID));
                FrendBtnx[j].setOnClickListener(this);
            }
        }


    }



    @Override
    public void onClick(View v) {

        int index = 0;

        for (int i = 0; i < FrendBtn.length; i++) {
            if (v.getId() == FrendBtn[i].getId()) {
                Intent nextIntent1 = new Intent(getApplicationContext(),MornSms1.class);
                nextIntent1.putExtra("txtFromPrev", FrendBtn[i].getText().toString());
                startActivity(nextIntent1);
                index = i;
                break;
            }
        }



        for (int j = 0; j < FrendBtnx.length; j++) {
            if (v.getId() == FrendBtnx[j].getId()) {
                Intent nextIntent1 = new Intent(getApplicationContext(), MornSms1.class);
                nextIntent1.putExtra("txtFromPrev", FrendBtnx[j].getText().toString());
                startActivity(nextIntent1);
                index = j;
                break;
            }
        }

    }
}



activity_morn_sms1.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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"
android:padding="10dp"
    tools:context="com.example.abc.msgdemo.MornSms1"
    android:contextClickable="false"
    android:background="@drawable/ab"
>

    <Button

        android:textAllCaps="false"
        android:layout_width="300dp"
        android:layout_height="340dp"
        android:id="@+id/txtDisplay"
        android:editable="true"
        android:textColor="#960202"
        android:textSize="25dp"
        android:background="#01ffffff"
        android:layout_centerHorizontal="true"
        android:layout_below="@+id/button"
        android:textAlignment="center"
        android:layout_above="@+id/imageButton" />

    <ImageButton

         android:background="@drawable/share"
        android:layout_width="60dp"
        android:layout_height="60dp"
        android:paddingLeft="10dp"
        android:paddingRight="10dp"
        android:id="@+id/imageButton"


        android:text="Share"
        android:onClick="shareText"
        android:textColor="#fefcfc"
        android:layout_alignParentBottom="true"
        android:layout_alignRight="@+id/txtDisplay"
        android:layout_alignEnd="@+id/txtDisplay" />

    <ImageButton
        android:layout_width="60dp"
        android:layout_height="60dp"
        android:paddingLeft="10dp"
        android:paddingRight="10dp"
        android:id="@+id/copybtn"
        android:background="@drawable/copy"
        android:text="Copy"
        android:onClick="onCopy"
        android:textColor="#fefcfc"
        android:layout_alignParentBottom="true"
        android:layout_alignLeft="@+id/txtDisplay"
        android:layout_alignStart="@+id/txtDisplay" />

    <ImageButton
        android:layout_width="60dp"
        android:layout_height="60dp"
        android:text="H"
        android:id="@+id/button"
        android:layout_alignParentTop="true"
        android:background="@drawable/hom"
        android:onClick="onHome"
        android:textColor="#fffdfd"

        android:layout_alignRight="@+id/copybtn"
        android:layout_alignEnd="@+id/copybtn" />

</RelativeLayout>

MornListSMS1.java

import android.app.Activity;
import android.content.ClipData;
import android.content.ClipboardManager;
import android.content.Intent;
//import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

public class MornSms1 extends AppCompatActivity {
  Button txtDisp;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_morn_sms1);
        txtDisp=(Button)findViewById(R.id.txtDisplay);
        Bundle extras = getIntent().getExtras();
        if (extras != null) {

            txtDisp.setText(extras.getString("txtFromPrev"));
        }
    }

    public void shareText(View v) {
        Intent share = new Intent(android.content.Intent.ACTION_SEND);
        share.setType("text/plain");

       //` share.putExtra(Intent.EXTRA_SUBJECT, "Title Of The Post");
        share.putExtra(Intent.EXTRA_TEXT, "Share Via");

        share.putExtra(android.content.Intent.EXTRA_TEXT,txtDisp.getText().toString());
        startActivity(Intent.createChooser(share, "Share link!"));
    }

    public void onCopy(View v) {




        //txtview = (TextView) findViewById(R.id.txt); // fetch the textview from the layout
        ClipboardManager clipboard = (ClipboardManager) getSystemService(CLIPBOARD_SERVICE);
        ClipData clip = ClipData.newPlainText("label", txtDisp.getText().toString());
        clipboard.setPrimaryClip(clip);
        Toast.makeText(this, "Text Copied", Toast.LENGTH_SHORT).show();

    }

    public void onHome(View v)
    {    Intent nextMornActivity = new Intent(getApplicationContext(), home.class);
        startActivity(nextMornActivity);}

}

0 个答案:

没有答案