我正在尝试制作一个活动,要求用户输入密码以继续使用该应用程序。为了做到这一点,我有一组存储在String []上的密码,程序每次用户启动应用程序时都会随机询问一个,显示你必须在textView中给出的密码。 它应该检索用户在密码字段中写的内容'密码'当它点击“确定”按钮时,如果它是正确的(我使用.equal),它将继续启动' menumesas'活动和做一些其他的事情,比如存储今天的日期(这是因为应用程序应该存储您访问应用程序的日期,将其保存在Data.txt文件中,以便您可以访问应用程序一整天。)。要做所有这些日期保存/阅读的事情,我使用三种方法' guardar',' readfromfile1',' getdate'。要获取一个随机数来询问其中一个密码我使用的方法是“随机”#。
我的问题是该程序突然关闭无所事事,如果我从代码中取出文本视图,该程序可以工作,但从不启动活动' menumesas'我想其他什么也没做,
我真的很感激一些帮助......拜托!
package com.example.casino2014;
public class MainActivity extends Activity {
Button okbutton;
String tempnum="", day1,day2,month1,month2,year1,year2,pass,val1,val2,val3,val4,val5,val6,strDate,todo,strValu e;
String[] datapass;
TextView myAwesomeTextView = (TextView)findViewById(R.id.passnum);
int random;
int check;
public static String TAG="MainActivity";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.password);
readFromFile1();
getdate();
dorandom();
}
public void mybutton(View v) {
if ((val1.equals(day1))&(val2.equals(day2))&(val3.equals(month1))& (val4.equals(month2))&(val5.equals(year1))&(val6.equals(year2))){
lanzarmenumesas();
}else{
// Set an EditText view to get user input
final EditText simpleEditText = (EditText) findViewById(R.id.password);
strValue = simpleEditText.getText().toString();
if (strValue.equals("gabriel10")){
check=0;
Toast.makeText(getBaseContext(),
"Thank you, password is correct. Enjoy!",
Toast.LENGTH_SHORT).show();
day1=val1;
day2=val2;
month1=val3;
month2=val4;
year1=val5;
year2=val6;
char[] temporal = todo.toCharArray();
temporal[0]=day1.charAt(0);
temporal[1]=day2.charAt(0);
temporal[2]=month1.charAt(0);
temporal[3]=month2.charAt(0);
temporal[4]=year1.charAt(0);
temporal[5]=year2.charAt(0);
todo = new String(temporal);
guardar();
lanzarmenumesas();
}
}
}
public void dorandom(){
random = 0 + (int)(Math.random() * ((99 - 0) + 1));
datapass=new String[]{"LknbObJKkT", "JiBoloKLpR", "ZcwXZupBhy", "epswZZVYUX", "2HWHKtabgn", "VLmHxlz5ON", "N8OyJDF2QB", "pWtx9uqO5t", "8dM6BAdhXY", "ysZIwQeb6D", "pYRPiaPULT", "IjLeTfuVsq", "6R2lcFiUs4", "PZZm9F0vih", "JUlcfpZs6I", "SlWAeaAw1w", "2xFWpravx8", "g45Toz0Xps", "oqg9VnuqML", "DpgQevBloM", "96Kmk2jtTn", "hunCWMSzvT", "PigQ46ka2Y", "e8lqt1LX83", "uxK53SyPwf", "Xd0ju1mizA", "t8Dil8StHo", "Cqh2ZLkT1T", "nfc3Tx8gUZ", "P6FZux7aSJ", "Sbw7kt7F4U", "iEAIRgVLsw", "LyEdXc43qn", "9N9ZvCyJax", "5LA4skLnhM", "K7IYRVl7R4", "nUP2hkF9Lm", "nawv8T5rUi", "4CLiLFItRQ", "Rvq1IiIR1I", "Udr47iy9Kb", "LpFbphO0Dq", "HEPstUe23O", "S25tXKKJIj", "TRrbnayyCf", "Uo90XVMG0l", "QBkmoSxn6B", "mNSvwitUHF", "Y55vlexzyw", "W5Grn0kB5C", "8TOLrOE4B2", "d1NBH8Xqao", "hlecHWq4UP", "synSXwXSk7", "q0VbZSHtcv", "7V8XcjNrG5", "6v1811VB7K", "1SGgnCVQIn", "y9HDYb43E6", "soMPOfNvsv", "JErWsN6uLa", "kIcweQuWbt", "NGcrTlHYMr", "bm8Ei3PuCZ", "K2UmC3BaHa", "1eM2Netyyr", "pkYO9pDn5L", "DGwUg1lqsk", "4MFf8kjWC5", "25QboU4GjE", "UmXHNkIRKb", "ZTMS5Nxjfi", "hnix8MTC3x", "fdL6Nku0bC", "nF27kejm3N", "BTqE2nmIb4", "fUvEL8wP6e", "VHhnCXPu8s", "YPBQWJiqfN", "9UGvP2utKC", "jVZc7qv7os", "1WMhEnPnDN", "YQ7g2P3xWs", "LqCsrMZr0q", "ABGmqvMkd6", "WrDAnUAaZF", "eEOKjsEUjb", "dvrVbx9hmj", "P1HQZkOWus", "Xs4lIAmJ2w", "bpubtCrtF5", "i6fQW1Sr8Q", "aFvcYqYhrn", "21tfFUCf9g", "lgIS6mcDxF", "NBdWFRt0sb", "VW8zKM1XVB", "3n4Gjxjfau", "XMbQQYMMhW" };
myAwesomeTextView.setText(random*13+"."+random+"."+(random+57));
}
public void lanzarmenumesas() {
Intent i = new Intent(this, menumesas.class );
startActivity(i);
}
public void getdate(){
Calendar c = Calendar.getInstance();
SimpleDateFormat sdf = new SimpleDateFormat("dd:MM:yy");
strDate = sdf.format(c.getTime());
val1=strDate.charAt(0)+"";
val2=strDate.charAt(1)+"";
val3=strDate.charAt(3)+"";
val4=strDate.charAt(4)+"";
val5=strDate.charAt(6)+"";
val6=strDate.charAt(7)+"";
}
private void readFromFile1() {
String nomarchivo = "Data.txt";
File tarjeta = Environment.getExternalStorageDirectory();
File file = new File(tarjeta.getAbsolutePath(), nomarchivo);
try {
FileInputStream fIn = new FileInputStream(file);
InputStreamReader archivo = new InputStreamReader(fIn);
BufferedReader br = new BufferedReader(archivo);
String linea = br.readLine();
todo = "";
while (linea != null) {
todo = todo + linea + "";
linea = br.readLine();
}
br.close();
archivo.close();
day1=todo.charAt(0)+"";
day2=todo.charAt(1)+"";
month1=todo.charAt(2)+"";
month2=todo.charAt(3)+"";
year1=todo.charAt(4)+"";
year2=todo.charAt(5)+"";
} catch (IOException e) {
day1="0";
day2="0";
month1="0";
month2="0";
year1="0";
year2="0";
Toast.makeText(getBaseContext(),
"'Data.txt' not found!",
Toast.LENGTH_SHORT).show();
}
}
public void guardar(){
try {
File tarjeta = Environment.getExternalStorageDirectory();
File myFile = new File(tarjeta.getAbsolutePath(), "Data.txt");
FileOutputStream fOut = new FileOutputStream(myFile);
OutputStreamWriter myOutWriter =new OutputStreamWriter(fOut);
for(int i=0; i<todo.length(); i++){
myOutWriter.append(todo);
}
myOutWriter.flush();
myOutWriter.close();
fOut.close();
Toast.makeText(getBaseContext(),
"Done writing to SD 'Data.txt'",
Toast.LENGTH_SHORT).show();
}
catch (Exception e)
{
Toast.makeText(getBaseContext(), e.getMessage(),
Toast.LENGTH_SHORT).show();
}
finish();
}
}
我的xml文件是
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView
android:id="@+id/textView1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_vertical|center_horizontal"
android:text="Password"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textSize="@dimen/fifty" />
<TextView
android:id="@+id/passnum"
android:layout_width="305dp"
android:layout_height="wrap_content"
android:gravity="center_horizontal"
android:text="TextView" />
<EditText
android:id="@+id/password"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:inputType="textPassword" >
<requestFocus />
</EditText>
<Button
android:id="@+id/mybutton"
android:layout_width="116dp"
android:layout_height="wrap_content"
android:text="OK" />
</LinearLayout>
答案 0 :(得分:0)
如果不想开始过于消极,那么你的问题并不是很好。我建议您快速查看guidelines以及其中的一些链接。但总的来说:
那就是说,我记得从Android开始,它可能有点棘手,所以这里有几点可以帮助你:
首先,您过早地尝试查找TextView。你有这条线:
TextView myAwesomeTextView = (TextView) findViewById(R.id.passnum);
在顶部的变量声明中。但是,此时尚未创建任何内容,因此无法找到它。这是导致应用程序在尝试启动时崩溃的原因,这在堆栈跟踪中非常清楚。
您需要做的是更改它,以便在顶部定义变量:
TextView myAwesomeTextView;
但是,在您进入onCreate之前,请不要尝试找到TextView:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.password);
//Now things have been created, I can try to find them...
myAwesomeTextView = (TextView) findViewById(R.id.passnum);
...
那会让你超越第一道障碍。
然后,您需要在按钮上添加OnClickListener,以便在单击它时实际发生某些事情。例如。 (再次在onCreate上):
Button myButton = (Button) findViewById(R.id.mybutton);
myButton.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
// TODO - Some action here
}
});
这两点将使您的应用程序启动,并在您单击按钮时实际执行某些操作。在那之后,它取决于你。
如果您还有其他问题,请提出新问题并尝试尽可能具体。另外,如果您还没有这样做,我建议您先看看Android教程并开始使用。其中很多都会被覆盖。