开始一个新的活动android

时间:2015-10-21 19:10:13

标签: android-activity

我正在尝试学习在android中启动新活动的原则。我正在使用“Android工作室”。我创建了一个MainActivity类,其中包含一个文本,告诉用户按下按钮和按钮。当按下按钮时,应该启动新活动(aktivitet2Klasse)。此活动包含两个文本视图和一个按钮。按下第一个按钮时,tetview应显示“工作正常!”。当按下第二个按钮时,它应该显示“已完成!”。应用程序的关键是从另一个开始一个活动。但是,在按下第一个屏幕上的按钮(第一个活动)后,应用程序崩溃了。我在这些方面研究了很多相同的问题,但我无法弄清楚是什么问题。

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);
}
}

1 个答案:

答案 0 :(得分:2)

您忘记在第二个活动中设置内容视图

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
}