رفتن به مطلب
انجمن اندروید ایران | آموزش برنامه نویسی اندروید و موبایل
  • android.png.1fab383bc8500cd93127cebc65b1dcab.png

مشکل این برنامه چیه؟


پست های پیشنهاد شده

سلام.

پروژه من یه کتاب اس ام اس جوک هست.

10 تا تکست ویو داره برای نمایش جوک ها و 10 تا هم دکمه برای ارسال اون ها.

دو تا دکمه هم برای رفتن به صفحه بعد(button12) و قبل(button11) که البته کار نمی کنن. :(

در اولین اجرا فایل های text1 تا text10 داخل تکست ویوها نمایش داده میشن.

می خوام وقتی رو دکمه "صفحه بعد" کلیک کردم فایل های text10 تا text20 داخل تکست ویوها نمایش داده بشن که نمیشه...

هر تغییری که میدم با اینکه تو اکلیپس خطا میده یا موقع اجرا تو گوشی(امولاتور) وقتی رو دکمه های "صفحه بعد" یا "صفحه قبل" کلیک می کنم برنامه میبنده.

لطفا یه نگاه بندازید ببینید چه تغییری بدم!؟

[shcode=java]

package com.example.jok;

import java.io.BufferedReader;

import java.io.IOException;

import java.io.InputStream;

import java.io.InputStreamReader;

import java.io.UnsupportedEncodingException;

import android.os.Bundle;

import android.app.Activity;

import android.content.Context;

import android.content.Intent;

import android.view.ContextThemeWrapper;

import android.view.Menu;

import android.view.View;

import android.view.View.OnClickListener;

import android.widget.Button;

import android.widget.TextView;

public class MainActivity extends Activity {

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

//تعریف تکست ویو ها

final TextView tv1 = (TextView) findViewById(R.id.textView1);

final TextView tv2 = (TextView) findViewById(R.id.textView2);

final TextView tv3 = (TextView) findViewById(R.id.textView3);

final TextView tv4 = (TextView) findViewById(R.id.textView4);

final TextView tv5 = (TextView) findViewById(R.id.textView5);

final TextView tv6 = (TextView) findViewById(R.id.textView6);

final TextView tv7 = (TextView) findViewById(R.id.textView7);

final TextView tv8 = (TextView) findViewById(R.id.textView8);

final TextView tv9 = (TextView) findViewById(R.id.textView9);

final TextView tv10 = (TextView) findViewById(R.id.textView10);

//تعریف دکمه ها

Button btn1 = (Button) findViewById(R.id.button1);

Button btn2 = (Button) findViewById(R.id.button2);

Button btn3 = (Button) findViewById(R.id.button3);

Button btn4 = (Button) findViewById(R.id.button4);

Button btn5 = (Button) findViewById(R.id.button5);

Button btn6 = (Button) findViewById(R.id.button6);

Button btn7 = (Button) findViewById(R.id.button7);

Button btn8 = (Button) findViewById(R.id.button8);

Button btn9 = (Button) findViewById(R.id.button9);

Button btn10 = (Button) findViewById(R.id.button10);

Button btn11 = (Button) findViewById(R.id.button11);

Button btn12 = (Button) findViewById(R.id.button12);

//نمایش متن 1 تا 10 در اولین اجرا در تکست ویو ها

String data = null;

try {

data = readTextFile(this, R.raw.text1);

} catch (UnsupportedEncodingException e) {

// TODO Auto-generated catch block

e.printStackTrace();}

tv1.setText(data);

try {

data = readTextFile(this, R.raw.text2);

} catch (UnsupportedEncodingException e) {

// TODO Auto-generated catch block

e.printStackTrace();}

tv2.setText(data);

try {

data = readTextFile(this, R.raw.text3);

} catch (UnsupportedEncodingException e) {

// TODO Auto-generated catch block

e.printStackTrace();}

tv3.setText(data);

try {

data = readTextFile(this, R.raw.text4);

} catch (UnsupportedEncodingException e) {

// TODO Auto-generated catch block

e.printStackTrace();}

tv4.setText(data);

try {

data = readTextFile(this, R.raw.text5);

} catch (UnsupportedEncodingException e) {

// TODO Auto-generated catch block

e.printStackTrace();}

tv5.setText(data);

try {

data = readTextFile(this, R.raw.text6);

} catch (UnsupportedEncodingException e) {

// TODO Auto-generated catch block

e.printStackTrace();}

tv6.setText(data);

try {

data = readTextFile(this, R.raw.text7);

} catch (UnsupportedEncodingException e) {

// TODO Auto-generated catch block

e.printStackTrace();}

tv7.setText(data);

try {

data = readTextFile(this, R.raw.text8);

} catch (UnsupportedEncodingException e) {

// TODO Auto-generated catch block

e.printStackTrace();}

tv8.setText(data);

try {

data = readTextFile(this, R.raw.text9);

} catch (UnsupportedEncodingException e) {

// TODO Auto-generated catch block

e.printStackTrace();}

tv9.setText(data);

try {

data = readTextFile(this, R.raw.text10);

} catch (UnsupportedEncodingException e) {

// TODO Auto-generated catch block

e.printStackTrace();}

tv10.setText(data);

//تعریف شماره صفحه

final int i=1;

btn12.setOnClickListener(new OnClickListener() {

//دکمه بعدی - دکمه نمایش متن های 11 تا 20 در تکست ویو ها

@Override

public void onClick(View v) {

//اگر صفحه اول بود

if(i==1){

String data = null;

try {

data = readTextFile(this, R.raw.text11);

} catch (UnsupportedEncodingException e) {

// TODO Auto-generated catch block

e.printStackTrace();}

tv1.setText(data);

try {

data = readTextFile(this, R.raw.text12);

} catch (UnsupportedEncodingException e) {

// TODO Auto-generated catch block

e.printStackTrace();}

tv2.setText(data);

try {

data = readTextFile(this, R.raw.text13);

} catch (UnsupportedEncodingException e) {

// TODO Auto-generated catch block

e.printStackTrace();}

tv3.setText(data);

try {

data = readTextFile(this, R.raw.text14);

} catch (UnsupportedEncodingException e) {

// TODO Auto-generated catch block

e.printStackTrace();}

tv4.setText(data);

try {

data = readTextFile(this, R.raw.text15);

} catch (UnsupportedEncodingException e) {

// TODO Auto-generated catch block

e.printStackTrace();}

tv5.setText(data);

try {

data = readTextFile(this, R.raw.text16);

} catch (UnsupportedEncodingException e) {

// TODO Auto-generated catch block

e.printStackTrace();}

tv6.setText(data);

try {

data = readTextFile(this, R.raw.text17);

} catch (UnsupportedEncodingException e) {

// TODO Auto-generated catch block

e.printStackTrace();}

tv7.setText(data);

try {

data = readTextFile(this, R.raw.text18);

} catch (UnsupportedEncodingException e) {

// TODO Auto-generated catch block

e.printStackTrace();}

tv8.setText(data);

try {

data = readTextFile(this, R.raw.text19);

} catch (UnsupportedEncodingException e) {

// TODO Auto-generated catch block

e.printStackTrace();}

tv9.setText(data);

try {

data = readTextFile(this, R.raw.text20);

} catch (UnsupportedEncodingException e) {

// TODO Auto-generated catch block

e.printStackTrace();}

tv10.setText(data);

}

i++;// حالا صفحه یکی بیشتر شده

}});

btn11.setOnClickListener(new OnClickListener() {

//دکمه نمایش متن های 1 تا 10

@Override

public void onClick(View v) {

// اگر صفحه دوم بود

if(i==2){

String data = null;

try {

data = readTextFile(this, R.raw.text1);

} catch (UnsupportedEncodingException e) {

// TODO Auto-generated catch block

e.printStackTrace();}

tv1.setText(data);

try {

data = readTextFile(this, R.raw.text2);

} catch (UnsupportedEncodingException e) {

// TODO Auto-generated catch block

e.printStackTrace();}

tv2.setText(data);

try {

data = readTextFile(this, R.raw.text3);

} catch (UnsupportedEncodingException e) {

// TODO Auto-generated catch block

e.printStackTrace();}

tv3.setText(data);

try {

data = readTextFile(this, R.raw.text4);

} catch (UnsupportedEncodingException e) {

// TODO Auto-generated catch block

e.printStackTrace();}

tv4.setText(data);

try {

data = readTextFile(this, R.raw.text5);

} catch (UnsupportedEncodingException e) {

// TODO Auto-generated catch block

e.printStackTrace();}

tv5.setText(data);

try {

data = readTextFile(this, R.raw.text6);

} catch (UnsupportedEncodingException e) {

// TODO Auto-generated catch block

e.printStackTrace();}

tv6.setText(data);

try {

data = readTextFile(this, R.raw.text7);

} catch (UnsupportedEncodingException e) {

// TODO Auto-generated catch block

e.printStackTrace();}

tv7.setText(data);

try {

data = readTextFile(this, R.raw.text8);

} catch (UnsupportedEncodingException e) {

// TODO Auto-generated catch block

e.printStackTrace();}

tv8.setText(data);

try {

data = readTextFile(this, R.raw.text9);

} catch (UnsupportedEncodingException e) {

// TODO Auto-generated catch block

e.printStackTrace();}

tv9.setText(data);

try {

data = readTextFile(this, R.raw.text10);

} catch (UnsupportedEncodingException e) {

// TODO Auto-generated catch block

e.printStackTrace();}

tv10.setText(data);

}

i--;// حالا صفحه یکی کمتر شده

}});

//علکرد دکمه های ارسال

btn1.setOnClickListener(new OnClickListener() {

@Override

public void onClick(View v) {

Intent smsIntent = new Intent(Intent.ACTION_VIEW);

smsIntent.putExtra("sms_body", tv1.getText());

smsIntent.putExtra("address", "");

smsIntent.setType("vnd.android-dir/mms-sms");

startActivity(smsIntent);

}});

btn2.setOnClickListener(new OnClickListener() {

@Override

public void onClick(View v) {

Intent smsIntent = new Intent(Intent.ACTION_VIEW);

smsIntent.putExtra("sms_body", tv2.getText());

smsIntent.putExtra("address", "");

smsIntent.setType("vnd.android-dir/mms-sms");

startActivity(smsIntent);

}});

btn3.setOnClickListener(new OnClickListener() {

@Override

public void onClick(View v) {

Intent smsIntent = new Intent(Intent.ACTION_VIEW);

smsIntent.putExtra("sms_body", tv3.getText());

smsIntent.putExtra("address", "");

smsIntent.setType("vnd.android-dir/mms-sms");

startActivity(smsIntent);

}});

btn4.setOnClickListener(new OnClickListener() {

@Override

public void onClick(View v) {

Intent smsIntent = new Intent(Intent.ACTION_VIEW);

smsIntent.putExtra("sms_body", tv4.getText());

smsIntent.putExtra("address", "");

smsIntent.setType("vnd.android-dir/mms-sms");

startActivity(smsIntent);

}});

btn5.setOnClickListener(new OnClickListener() {

@Override

public void onClick(View v) {

Intent smsIntent = new Intent(Intent.ACTION_VIEW);

smsIntent.putExtra("sms_body", tv5.getText());

smsIntent.putExtra("address", "");

smsIntent.setType("vnd.android-dir/mms-sms");

startActivity(smsIntent);

}});

btn6.setOnClickListener(new OnClickListener() {

@Override

public void onClick(View v) {

Intent smsIntent = new Intent(Intent.ACTION_VIEW);

smsIntent.putExtra("sms_body", tv6.getText());

smsIntent.putExtra("address", "");

smsIntent.setType("vnd.android-dir/mms-sms");

startActivity(smsIntent);

}});

btn7.setOnClickListener(new OnClickListener() {

@Override

public void onClick(View v) {

Intent smsIntent = new Intent(Intent.ACTION_VIEW);

smsIntent.putExtra("sms_body", tv7.getText());

smsIntent.putExtra("address", "");

smsIntent.setType("vnd.android-dir/mms-sms");

startActivity(smsIntent);

}});

btn8.setOnClickListener(new OnClickListener() {

@Override

public void onClick(View v) {

Intent smsIntent = new Intent(Intent.ACTION_VIEW);

smsIntent.putExtra("sms_body", tv8.getText());

smsIntent.putExtra("address", "");

smsIntent.setType("vnd.android-dir/mms-sms");

startActivity(smsIntent);

}});

btn9.setOnClickListener(new OnClickListener() {

@Override

public void onClick(View v) {

Intent smsIntent = new Intent(Intent.ACTION_VIEW);

smsIntent.putExtra("sms_body", tv9.getText());

smsIntent.putExtra("address", "");

smsIntent.setType("vnd.android-dir/mms-sms");

startActivity(smsIntent);

}});

btn10.setOnClickListener(new OnClickListener() {

@Override

public void onClick(View v) {

Intent smsIntent = new Intent(Intent.ACTION_VIEW);

smsIntent.putExtra("sms_body", tv10.getText());

smsIntent.putExtra("address", "");

smsIntent.setType("vnd.android-dir/mms-sms");

startActivity(smsIntent);

}});

}

public static String readTextFile(Context ctx, int resId) throws UnsupportedEncodingException

{

InputStream inputStream = ctx.getResources().openRawResource(resId);

InputStreamReader inputreader = new InputStreamReader(inputStream, "UTF-8");

BufferedReader bufferedreader = new BufferedReader(inputreader);

String line;

StringBuilder stringBuilder = new StringBuilder();

try

{

while (( line = bufferedreader.readLine()) != null)

{

stringBuilder.append(line);

stringBuilder.append("\n");

}

}

catch (IOException e)

{

return null;

}

return stringBuilder.toString();

}

@Override

public boolean onCreateOptionsMenu(Menu menu) {

// Inflate the menu; this adds items to the action bar if it is present.

getMenuInflater().inflate(R.menu.main, menu);

return true;

}

}

[/shcode]

لینک ارسال
به اشتراک گذاری در سایت های دیگر

1. در فراخوانی متد readTextFile در رویداد onClick استفاده از کلمه this اشتباهه و باید بجای اون getApplicationContext استفاده بشه.

2.استفاده از متغیر محلی final اشتباهه. متغییر جهت نگهداری صفحه جاری رو در خارج از متد oncreate معرفی کنید.

3.textview ها رو از حالت final خارج و بیرون متد oncreate معرفی بشن.

4.در متد readTextFile  بعد از خواندن فایل اون رو ببندید! 

سورس تغییر یافته به صورت زیر : 

/*your package here*/import java.io.BufferedReader;import java.io.IOException;import java.io.InputStream;import java.io.InputStreamReader;import java.io.UnsupportedEncodingException;import android.app.Activity;import android.content.Context;import android.content.Intent;import android.os.Bundle;import android.view.View;import android.view.View.OnClickListener;import android.widget.Button;import android.widget.TextView;public class JokActivity extends Activity {   TextView tv1;   TextView tv2;   TextView tv3;   TextView tv4;   TextView tv5;   TextView tv6;   TextView tv7;   TextView tv8;   TextView tv9;   TextView tv10;   int      i;   @Override   public void onCreate(Bundle savedInstanceState) {       super.onCreate(savedInstanceState);       setContentView(R.layout.main);       tv1 = (TextView) findViewById(R.id.textView1);       tv2 = (TextView) findViewById(R.id.textView2);       tv3 = (TextView) findViewById(R.id.textView3);       tv4 = (TextView) findViewById(R.id.textView4);       tv5 = (TextView) findViewById(R.id.textView5);       tv6 = (TextView) findViewById(R.id.textView6);       tv7 = (TextView) findViewById(R.id.textView7);       tv8 = (TextView) findViewById(R.id.textView8);       tv9 = (TextView) findViewById(R.id.textView9);       tv10 = (TextView) findViewById(R.id.textView10);       Button btn1 = (Button) findViewById(R.id.button1);       Button btn2 = (Button) findViewById(R.id.button2);       Button btn3 = (Button) findViewById(R.id.button3);       Button btn4 = (Button) findViewById(R.id.button4);       Button btn5 = (Button) findViewById(R.id.button5);       Button btn6 = (Button) findViewById(R.id.button6);       Button btn7 = (Button) findViewById(R.id.button7);       Button btn8 = (Button) findViewById(R.id.button8);       Button btn9 = (Button) findViewById(R.id.button9);       Button btn10 = (Button) findViewById(R.id.button10);       Button btn11 = (Button) findViewById(R.id.button11);       Button btn12 = (Button) findViewById(R.id.button12);       String data = null;       try {           data = readTextFile(this, R.raw.text1);       }       catch (UnsupportedEncodingException e) {           // TODO Auto-generated catch block           e.printStackTrace();       }       tv1.setText(data);       try {           data = readTextFile(this, R.raw.text2);       }       catch (UnsupportedEncodingException e) {           // TODO Auto-generated catch block           e.printStackTrace();       }       tv2.setText(data);       try {           data = readTextFile(this, R.raw.text3);       }       catch (UnsupportedEncodingException e) {           // TODO Auto-generated catch block           e.printStackTrace();       }       tv3.setText(data);       try {           data = readTextFile(this, R.raw.text4);       }       catch (UnsupportedEncodingException e) {           // TODO Auto-generated catch block           e.printStackTrace();       }       tv4.setText(data);       try {           data = readTextFile(this, R.raw.text5);       }       catch (UnsupportedEncodingException e) {           // TODO Auto-generated catch block           e.printStackTrace();       }       tv5.setText(data);       try {           data = readTextFile(this, R.raw.text6);       }       catch (UnsupportedEncodingException e) {           // TODO Auto-generated catch block           e.printStackTrace();       }       tv6.setText(data);       try {           data = readTextFile(this, R.raw.text7);       }       catch (UnsupportedEncodingException e) {           // TODO Auto-generated catch block           e.printStackTrace();       }       tv7.setText(data);       try {           data = readTextFile(this, R.raw.text8);       }       catch (UnsupportedEncodingException e) {           // TODO Auto-generated catch block           e.printStackTrace();       }       tv8.setText(data);       try {           data = readTextFile(this, R.raw.text9);       }       catch (UnsupportedEncodingException e) {           // TODO Auto-generated catch block           e.printStackTrace();       }       tv9.setText(data);       try {           data = readTextFile(this, R.raw.text10);       }       catch (UnsupportedEncodingException e) {           // TODO Auto-generated catch block           e.printStackTrace();       }       tv10.setText(data);       i = 1;       btn12.setOnClickListener(new OnClickListener() {           @Override           public void onClick(View v) {               //               if (i == 1) {                   String data = null;                   try {                       data = readTextFile(getApplicationContext(), R.raw.text11);                   }                   catch (UnsupportedEncodingException e) {                       // TODO Auto-generated catch block                       e.printStackTrace();                   }                   tv1.setText(data);                   try {                       data = readTextFile(getApplicationContext(), R.raw.text12);                   }                   catch (UnsupportedEncodingException e) {                       // TODO Auto-generated catch block                       e.printStackTrace();                   }                   tv2.setText(data);                   try {                       data = readTextFile(getApplicationContext(), R.raw.text13);                   }                   catch (UnsupportedEncodingException e) {                       // TODO Auto-generated catch block                       e.printStackTrace();                   }                   tv3.setText(data);                   try {                       data = readTextFile(getApplicationContext(), R.raw.text14);                   }                   catch (UnsupportedEncodingException e) {                       // TODO Auto-generated catch block                       e.printStackTrace();                   }                   tv4.setText(data);                   try {                       data = readTextFile(getApplicationContext(), R.raw.text15);                   }                   catch (UnsupportedEncodingException e) {                       // TODO Auto-generated catch block                       e.printStackTrace();                   }                   tv5.setText(data);                   try {                       data = readTextFile(getApplicationContext(), R.raw.text16);                   }                   catch (UnsupportedEncodingException e) {                       // TODO Auto-generated catch block                       e.printStackTrace();                   }                   tv6.setText(data);                   try {                       data = readTextFile(getApplicationContext(), R.raw.text17);                   }                   catch (UnsupportedEncodingException e) {                       // TODO Auto-generated catch block                       e.printStackTrace();                   }                   tv7.setText(data);                   try {                       data = readTextFile(getApplicationContext(), R.raw.text18);                   }                   catch (UnsupportedEncodingException e) {                       // TODO Auto-generated catch block                       e.printStackTrace();                   }                   tv8.setText(data);                   try {                       data = readTextFile(getApplicationContext(), R.raw.text19);                   }                   catch (UnsupportedEncodingException e) {                       // TODO Auto-generated catch block                       e.printStackTrace();                   }                   tv9.setText(data);                   try {                       data = readTextFile(getApplicationContext(), R.raw.text20);                   }                   catch (UnsupportedEncodingException e) {                       // TODO Auto-generated catch block                       e.printStackTrace();                   }                   tv10.setText(data);               }               i++;           }       });       btn11.setOnClickListener(new OnClickListener() {           @Override           public void onClick(View v) {               if (i == 2) {                   String data = null;                   try {                       data = readTextFile(getApplicationContext(), R.raw.text1);                   }                   catch (UnsupportedEncodingException e) {                       // TODO Auto-generated catch block                       e.printStackTrace();                   }                   tv1.setText(data);                   try {                       data = readTextFile(getApplicationContext(), R.raw.text2);                   }                   catch (UnsupportedEncodingException e) {                       // TODO Auto-generated catch block                       e.printStackTrace();                   }                   tv2.setText(data);                   try {                       data = readTextFile(getApplicationContext(), R.raw.text3);                   }                   catch (UnsupportedEncodingException e) {                       // TODO Auto-generated catch block                       e.printStackTrace();                   }                   tv3.setText(data);                   try {                       data = readTextFile(getApplicationContext(), R.raw.text4);                   }                   catch (UnsupportedEncodingException e) {                       // TODO Auto-generated catch block                       e.printStackTrace();                   }                   tv4.setText(data);                   try {                       data = readTextFile(getApplicationContext(), R.raw.text5);                   }                   catch (UnsupportedEncodingException e) {                       // TODO Auto-generated catch block                       e.printStackTrace();                   }                   tv5.setText(data);                   try {                       data = readTextFile(getApplicationContext(), R.raw.text6);                   }                   catch (UnsupportedEncodingException e) {                       // TODO Auto-generated catch block                       e.printStackTrace();                   }                   tv6.setText(data);                   try {                       data = readTextFile(getApplicationContext(), R.raw.text7);                   }                   catch (UnsupportedEncodingException e) {                       // TODO Auto-generated catch block                       e.printStackTrace();                   }                   tv7.setText(data);                   try {                       data = readTextFile(getApplicationContext(), R.raw.text8);                   }                   catch (UnsupportedEncodingException e) {                       // TODO Auto-generated catch block                       e.printStackTrace();                   }                   tv8.setText(data);                   try {                       data = readTextFile(getApplicationContext(), R.raw.text9);                   }                   catch (UnsupportedEncodingException e) {                       // TODO Auto-generated catch block                       e.printStackTrace();                   }                   tv9.setText(data);                   try {                       data = readTextFile(getApplicationContext(), R.raw.text10);                   }                   catch (UnsupportedEncodingException e) {                       // TODO Auto-generated catch block                       e.printStackTrace();                   }                   tv10.setText(data);               }               i--;           }       });       btn1.setOnClickListener(new OnClickListener() {           @Override           public void onClick(View v) {               Intent smsIntent = new Intent(Intent.ACTION_VIEW);               smsIntent.putExtra("sms_body", tv1.getText());               smsIntent.putExtra("address", "");               smsIntent.setType("vnd.android-dir/mms-sms");               startActivity(smsIntent);           }       });       btn2.setOnClickListener(new OnClickListener() {           @Override           public void onClick(View v) {               Intent smsIntent = new Intent(Intent.ACTION_VIEW);               smsIntent.putExtra("sms_body", tv2.getText());               smsIntent.putExtra("address", "");               smsIntent.setType("vnd.android-dir/mms-sms");               startActivity(smsIntent);           }       });       btn3.setOnClickListener(new OnClickListener() {           @Override           public void onClick(View v) {               Intent smsIntent = new Intent(Intent.ACTION_VIEW);               smsIntent.putExtra("sms_body", tv3.getText());               smsIntent.putExtra("address", "");               smsIntent.setType("vnd.android-dir/mms-sms");               startActivity(smsIntent);           }       });       btn4.setOnClickListener(new OnClickListener() {           @Override           public void onClick(View v) {               Intent smsIntent = new Intent(Intent.ACTION_VIEW);               smsIntent.putExtra("sms_body", tv4.getText());               smsIntent.putExtra("address", "");               smsIntent.setType("vnd.android-dir/mms-sms");               startActivity(smsIntent);           }       });       btn5.setOnClickListener(new OnClickListener() {           @Override           public void onClick(View v) {               Intent smsIntent = new Intent(Intent.ACTION_VIEW);               smsIntent.putExtra("sms_body", tv5.getText());               smsIntent.putExtra("address", "");               smsIntent.setType("vnd.android-dir/mms-sms");               startActivity(smsIntent);           }       });       btn6.setOnClickListener(new OnClickListener() {           @Override           public void onClick(View v) {               Intent smsIntent = new Intent(Intent.ACTION_VIEW);               smsIntent.putExtra("sms_body", tv6.getText());               smsIntent.putExtra("address", "");               smsIntent.setType("vnd.android-dir/mms-sms");               startActivity(smsIntent);           }       });       btn7.setOnClickListener(new OnClickListener() {           @Override           public void onClick(View v) {               Intent smsIntent = new Intent(Intent.ACTION_VIEW);               smsIntent.putExtra("sms_body", tv7.getText());               smsIntent.putExtra("address", "");               smsIntent.setType("vnd.android-dir/mms-sms");               startActivity(smsIntent);           }       });       btn8.setOnClickListener(new OnClickListener() {           @Override           public void onClick(View v) {               Intent smsIntent = new Intent(Intent.ACTION_VIEW);               smsIntent.putExtra("sms_body", tv8.getText());               smsIntent.putExtra("address", "");               smsIntent.setType("vnd.android-dir/mms-sms");               startActivity(smsIntent);           }       });       btn9.setOnClickListener(new OnClickListener() {           @Override           public void onClick(View v) {               Intent smsIntent = new Intent(Intent.ACTION_VIEW);               smsIntent.putExtra("sms_body", tv9.getText());               smsIntent.putExtra("address", "");               smsIntent.setType("vnd.android-dir/mms-sms");               startActivity(smsIntent);           }       });       btn10.setOnClickListener(new OnClickListener() {           @Override           public void onClick(View v) {               Intent smsIntent = new Intent(Intent.ACTION_VIEW);               smsIntent.putExtra("sms_body", tv10.getText());               smsIntent.putExtra("address", "");               smsIntent.setType("vnd.android-dir/mms-sms");               startActivity(smsIntent);           }       });   }   public static String readTextFile(Context ctx, int resId) throws UnsupportedEncodingException   {       InputStream inputStream = ctx.getResources().openRawResource(resId);       InputStreamReader inputreader = new InputStreamReader(inputStream, "UTF-8");       BufferedReader bufferedreader = new BufferedReader(inputreader);       String line;       StringBuilder stringBuilder = new StringBuilder();       try       {           while ((line = bufferedreader.readLine()) != null)           {               stringBuilder.append(line);               stringBuilder.append("\n");           }       }       catch (IOException e)       {           return null;       } finally {           try {               inputStream.close();           }           catch (IOException e) {               // TODO Auto-generated catch block               e.printStackTrace();           }       }       return stringBuilder.toString();   }/* your source code for menu Options here*/}

اگه ناراحت نشی باید بگم که سورس کد موجود خیلی جالب نیست 

1. نام گذاری ها رعایت نشده

2. متد onCreate تقریبا 400 خط شده 

3. کد تکراری زیاد دیده میشه

لینک ارسال
به اشتراک گذاری در سایت های دیگر

ناراحت كه نميشم.من تازه كارم و خوشحال ميشم عيب كارمو بگيد و البته راه درست كردنشو.

براي كم كردن كدهاي تكراري اين برنامه چه كار بايد بكنم؟

البته جواب سوال پست اول هم اگه مي تونيد بگيد!


ناراحت كه نميشم.من تازه كارم و خوشحال ميشم عيب كارمو بگيد و البته راه درست كردنشو.

براي كم كردن كدهاي تكراري اين برنامه چه كار بايد بكنم؟

البته جواب سوال پست اول هم اگه مي تونيد بگيد!

لینک ارسال
به اشتراک گذاری در سایت های دیگر

در پست قبل چند تا از اشکالات کد رو گفتم و خودم هم در کدی که گذاشتم اون هارو اصلاح کردم. (نمونه کد اسکرول میشه)صفحه قبل و بعد هم textview ها پر میشن! 

ساده ترین راه حذف کد تکراری استفاده از متد  با پارامتر ورودی  مناسب

لینک ارسال
به اشتراک گذاری در سایت های دیگر

به گفتگو بپیوندید

هم اکنون می توانید مطلب خود را ارسال نمایید و بعداً ثبت نام کنید. اگر حساب کاربری دارید، برای ارسال با حساب کاربری خود اکنون وارد شوید .

مهمان
ارسال پاسخ به این موضوع...

×   شما در حال چسباندن محتوایی با قالب بندی هستید.   حذف قالب بندی

  تنها استفاده از 75 اموجی مجاز می باشد.

×   لینک شما به صورت اتوماتیک جای گذاری شد.   نمایش به صورت لینک

×   محتوای قبلی شما بازگردانی شد.   پاک کردن محتوای ویرایشگر

×   شما مستقیما نمی توانید تصویر خود را قرار دهید. یا آن را اینجا بارگذاری کنید یا از یک URL قرار دهید.

×
×
  • اضافه کردن...