您好我是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=""A single candle can illuminate an entire room. A true friend lights up an entire lifetime. Thanks for the bright lights of ur friendship."" />
<Button
style="@style/btn_txt_style"
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=""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"" />
<Button
style="@style/btn_txt_style"
android:id="@+id/button3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=""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's okay because I got u.Happy Friendship Day "" />
<Button
style="@style/btn_txt_style"
android:id="@+id/button4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=""If you open my heart, guess what u r gonna see? It's U. True friends are hard to find so I kept u.Happy Friendship Day "" />
<Button
style="@style/btn_txt_style"
android:id="@+id/button5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=""How long shall v b friends? Do u want a clue? As long as stars twinkle in the sky, till the water runs dry & till the day I die. We will b friends.Happy Friendship Day "" />
<Button
style="@style/btn_txt_style"
android:id="@+id/button6"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=""I might run out of cute graphics to send u, but I'll never run out of this: care 4 u. "" />
<Button
style="@style/btn_txt_style"
android:id="@+id/button7"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=""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 "" />
<Button
style="@style/btn_txt_style"
android:id="@+id/button8"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=""Good Relations are just like "Water" No Colour , No Shape, No Smell, NO Taste. But Still Very Very "Important" For Life."" />
<Button
style="@style/btn_txt_style"
android:id="@+id/button9"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=""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"" />
<Button
style="@style/btn_txt_style"
android:id="@+id/button10"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="" 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. "" />
/////////////////////
<Button
style="@style/btn_txt_style"
android:id="@+id/button11"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="" A very short but very nice line for a true relation- "Hurt me with the truth, But Never comfort me with a lie." "" />
<Button
style="@style/btn_txt_style"
android:id="@+id/button12"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=""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."" />
<Button
style="@style/btn_txt_style"
android:id="@+id/button13"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=""Colors may fade, the sun may not shine, the moon may not be bright, heartbeats may stop, lives may pass but our friendship, I'll treasure 'till the day my heart stops."" />
<Button
style="@style/btn_txt_style"
android:id="@+id/button14"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=""When you're down, I'll be there to cheer you up. When you're tired, I'll be there to lift you up. I'll be your friend no matter what! "" />
<Button
style="@style/btn_txt_style"
android:id="@+id/button15"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=""Years may fly, tears may dry, but my friendship with u will never die."" />
<Button
style="@style/btn_txt_style"
android:id="@+id/button16"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="""FEELING"is a painting never spoil it "FACE"is a book try to read it "LOVE" is precious be ready to sacrifice for it "FRiENDSHiP" is a mirror never"break" it."" />
<Button
style="@style/btn_txt_style"
android:id="@+id/button17"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="" 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 "" />
<Button
style="@style/btn_txt_style"
android:id="@+id/button18"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=""Friendship often ends in love; but love ends in friendship - NEVER."" />
<Button
style="@style/btn_txt_style"
android:id="@+id/button19"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=""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. "" />
<Button
style="@style/btn_txt_style"
android:id="@+id/button20"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="" When u rise up, Your friend know who you are. When you fall down, You know who your friend are. "" />
///////////////////////
<Button
style="@style/btn_txt_style"
android:id="@+id/buttonx1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=""The ball can escape from bat, The rat can escape from cat, But u cannot escape from "MY HEART U R Always my good friend""" />
<Button
style="@style/btn_txt_style"
android:id="@+id/buttonx2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=""Distance never separates any relation & Time never build any relation. If Feelings are True From Heart, Then 'Friends are always "Friends" forever."" />
<Button
style="@style/btn_txt_style"
android:id="@+id/buttonx3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=""You are a part of a puzzle 0f someone's life. You may never kn0w where you fit, but always remember that someone's life may not be complete without you!:-)"" />
<Button
style="@style/btn_txt_style"
android:id="@+id/buttonx4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=""There are only two reasons why people can hurt you. 1-You really care about them. 2-They don't really care about you.Happy Friendship Day"" />
<Button
style="@style/btn_txt_style"
android:id="@+id/buttonx5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=""A single rose can be my garden A single friend, my world.Happy Friendship Day"" />
<Button
style="@style/btn_txt_style"
android:id="@+id/buttonx6"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=""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's okay because I got you."" />
<Button
style="@style/btn_txt_style"
android:id="@+id/buttonx7"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="" All friends are not True. But true friends are very few, Which includes U. "" />
<Button
style="@style/btn_txt_style"
android:id="@+id/buttonx8"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=""1 Stone is enough 2 break a glass, 1 sentence is enough 2 break a heart, 1 word is enough to fall in love & 1 friend is enough to live in this world"" />
<Button
style="@style/btn_txt_style"
android:id="@+id/buttonx9"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=""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."" />
<Button
style="@style/btn_txt_style"
android:id="@+id/buttonx10"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=""Friends are like stars. You can't always see them, But you know they are always there for you."" />
<Button
style="@style/btn_txt_style"
android:id="@+id/buttonx11"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="" There are no strings attached to a friendship, no bonds to hold or ties to bind. Friendship stand on its own. "" />
<Button
style="@style/btn_txt_style"
android:id="@+id/buttonx12"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=""Some one asked me 4 how long will u both be friends? i remained silent bcoz i didnt know which is longer.. "ALWAYS" or "FOREVER". "" />
<Button
style="@style/btn_txt_style"
android:id="@+id/buttonx13"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=""Don't Love a Friend who Hurts U Don't Hurt a Friend who Loves U."" />
<Button
style="@style/btn_txt_style"
android:id="@+id/buttonx14"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=""My friendship is nothing but a round, Wandering why? Because it has no end.Happy Friendship Day"" />
</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);}
}