-1
我是android開發新手,遇到崩潰問題。 這裏是我的代碼:Android應用程序立即崩潰
package com.maxwellcohn.www.jokegenerator;
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import java.util.Random;
public class Main extends Activity {
TextView jokeText = (TextView) findViewById(R.id.textView);
Random random = new Random();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final Button button = (Button) findViewById(R.id.btnAdd);
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
getJoke(0, 34);
}
});
}
@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;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
private void getJoke(int minimum, int maximum) {
int jokeNumber;
jokeNumber = random.nextInt(maximum - minimum + 1) + minimum;
jokeText.setText(jokes[jokeNumber]);
}
String[] jokes = {"Yo' mama so fat, when she was a baby, she took a bath with a rubber albatross.",
"A husband and wife are trying to set up a new password for their computer. The husband puts, 'Mypenis', and the wife falls on the ground laughing because on the screen it says, 'Error. Not long enough.'",
"DO NOT READ THE NEXT SENTENCE. You little rebel, I like you.",
"Teacher: What do you call a person who keeps on talking when people are no longer interested? Student: A teacher!",
"That moment when you're talking a test and you want everyone to know you're ahead so you flip the page as loud as possible.",
"Got arrested at the airport last week. Apparently, security doesn't appreciate it when you call 'shotgun' before boarding a plane.",
"Election and Erection are spelled almost exactly the same. They both mean the same thing too. A dick rising to power.",
"That frustrating moment when your almost done with your cereal and the last five pieces are like, 'Haha! catch me if you can'",
"When I asked my wife what she wanted for her birthday she said 'Just gimme something with diamonds.' That's why I got her a pack of cards.",
"What's the difference between a woman's argument and a knife? The knife has a point.", // Joke #10
"Women can walk around all day long in a bikini, but God forbid if you see them in their bras and panties.",
"Imagine if your fridge did what you do to it everyday. Every half hour it goes to your room opens the door, and stares at you for 5 minutes then leaves.",
"Wifi went down for five minutes, so i had to talk to my family. They seem like nice people.",
"I was just sitting around, doing nothing, when I was arrested for impersonating the President of the United States.",
"The only thing I use BING for is to search Google.",
"I killed a vampire on Halloween this year... or a kid. Either way, the wooden stake worked.",
"Don’t you hate it when you’re typing something and you’re thinking about something else so then you subconsciously type what you were titties.",
"If women ruled the world there would be no wars. Instead, there would just a bunch of countries not talking to each other.",
"When the person you hate the most falls down, you ask the ground if its okay.",
"Just tried to kill a spider with some Axe Body Spray but it survived and is now trying to make inappropriate sexual advances towards me.", //Joke #20
"What do constipated people say? 'I wanna scream and shout, and let it all out'",
"Today I decided to burn a lot of calories... So I lit a fat kid on fire.",
"I've noticed people don't like when you smack kids in public places for acting out. Especially when they're not your own.",
"Every morning, I jog around my block 15 times. Then I pick up the block and put it back in my toy chest.",
"So who was the first guy to see an egg come out of a chickens ass and say, 'I'm gonna eat that'?",
"Roses are red Violets are blue sugar is sweet and so are you But the Roses are wilting and the Violets are dead the sugar bowl is empty and so is your head.",
"I think the guy who invented ties was trying to commit suicide then he saw himself in the mirror & thought... 'Wait, this looks nice.'",
"Note to self: Its time to grow up, be responsible, and act like an adult. Self to note: Shut the f*ck up...",
"The only 2 states to have legal Marijuana are Colorado and Washington. The 2 best NFL teams are Seattle and Denver. Coincidence? I think not!",
"That moment when you're taking a test and you want everyone to know you're ahead so you flip the page as loud as possible.", // Joke #30
"World War 2 could have been prevented, if only someone had given Hitler a Snickers.",
"My friend thinks he is smart. He told me an onion is the only food that makes you cry, so I threw a coconut at his face.",
"That moment when you flex your stomach to show off your abs in front of a cute girl and then you sh*t yourself",
"Do you want to know Victoria's Secret? Their lingerie doesn't look the same on your wife as it does on their models.",
"Exercise? I thought you said extra fries."};
}
Android Studio中說,這是罰款,但應用程序立即崩潰在我的銀河S5。 android studio沒有錯誤。 這裏是我的XML:
<TextView
android:text="@string/hello_world"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="32dp"
android:textIsSelectable="true"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:id="@+id/textView" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="New Button"
android:id="@+id/btnAdd"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true" />
當你的應用程序崩潰,你得到任何錯誤? – 2014-09-30 01:58:13
你的logcat說什麼? – 2014-09-30 01:58:45
把'TextView jokeText =(TextView)findViewById(R.id.textView);'放在onCreate中。如果你是新手,你應該閱讀活動生命週期。 – AimanB 2014-09-30 01:59:55