开始一个新的活动android [英] Starting a new activity android
问题描述
我正在尝试学习在android中启动新活动的原理.我正在使用"Android Studio".我创建了一个MainActivity类,其中包含一个文本,该文本告诉用户按下按钮以及按钮.按下按钮后,新的活动应该开始了(aktivitet2Klasse).该活动包含两个文本视图和一个按钮.当按下第一个按钮时,tetview应显示工作正常!".当按下第二个按钮时,应该显示"Finished!".该应用程序的目的仅仅是从另一个活动开始.但是,在第一个屏幕上按下按钮(第一个活动)后,应用程序崩溃了.在这些方面,我已经研究了很多相同的问题,但是我无法弄清楚哪里出了问题.
I am trying to learn the principles of starting a new activity in android. I am using "Android studio". I have created a MainActivity class which contains a text which tells the user to push the button, and the button. When the button is pushed the new activity is supposed to start(aktivitet2Klasse). This activity contains two textviews and a button. When the first button is pressed, the tetview should display "works fine!". When the second button is pushed it is supposed to display "Finished!". The point with the app is just to start one activity from another. However after pushing the button on the first screen (first activity) the app crashes. I have studies a lot of the same issues on these sides, but I cannot figure what is wrong.
Here is the code:
The main activity:
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
private TextView instruction;
private Button startButton;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
instruction = (TextView) findViewById(R.id.InstruksjonsView);
startButton = (Button) findViewById(R.id.btn1);
startButton.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View v){
Intent intent = new Intent(MainActivity.this,aktivitet2Klasse.class);
startActivity(intent);
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.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();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
下一个活动:
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
public class aktivitet2Klasse extends AppCompatActivity {
private Button button2;
private TextView text;
private Button button3;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Intent intent = getIntent();
button2 = (Button) findViewById(R.id.btn2);
button3 = (Button) findViewById(R.id.btn3);
text = (TextView) findViewById(R.id.textView1);
button2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
text.setText("Works fine!");
}
});//End button2Listener
button3.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
text.setText("Finished!");
}
});//End button3Listener
}
@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();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
推荐答案
您忘记在第二个活动中设置内容视图
you forgot to set the content view in the second activity
public class aktivitet2Klasse extends AppCompatActivity {
private Button button2;
private TextView text;
private Button button3;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//YOU NEED TO SET THE CONTENTVIEW
// like setContentView(R.layout.activity_secondary);
Intent intent = getIntent();
button2 = (Button) findViewById(R.id.btn2);
button3 = (Button) findViewById(R.id.btn3);
text = (TextView) findViewById(R.id.textView1);
button2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
text.setText("Works fine!");
}
});//End button2Listener
button3.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
text.setText("Finished!");
}
});//End button3Listener
}
这篇关于开始一个新的活动android的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!