今天上午在家写程序,想在之前写的程序基础上不断进行扩展。上午主要想实现的功能是:页面的跳转。功能代码本身没有错误,但是就是一直调试不通。后来才发现是在androidmanifest.xml文件中忘记吧跳转页面活动activity忘记注册导致程序异常:
正确的应该如下:例如:我想在第二个页面满足某个条件的时候,视图跳转到第三个页面:
androidManiFest.xml文件中先注册second和third两个活动,当然了必须保证second和third对应相应文件名和类。
<activity
android:name=".Second"
android:label="@string/second" >
</activity>
<activity
android:name=".Third"
android:label="@string/third" >
</activity>
//接下来是功能函数:
package com.example;
import android.app.Activity;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.content.Intent;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.Spinner;
import android.widget.TextView;
public class Second extends Activity {
public static final String[] M_DATA = {"PHP","JAVA","C++","ANDROID","PYTHON"};
public TextView mTextView;
public Button mButton;
public Spinner mSpinner;
public ArrayAdapter<String> adapter;
public OnItemSelectedListener a = new OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
mTextView.setText("你擅长的码农语言是"+M_DATA[arg2]);
arg0.setVisibility(View.VISIBLE);
String test = "你擅长的码农语言是JAVA";
if(mTextView.getText().toString().equals(test)){
Intent a = new Intent(Second.this,Third.class);
startActivity(a);
}
}
@Override
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.second);
mTextView = (TextView)findViewById(R.id.codeLanguage);
String test = "你擅长的码农语言是JAVA";
if(mTextView.getText().toString().equals(test)){
Intent a = new Intent(Second.this,Third.class);
startActivity(a);
}
mSpinner = (Spinner)findViewById(R.id.devType);
adapter = new ArrayAdapter<String>(this,android.R.layout.simple_spinner_dropdown_item, M_DATA);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
mSpinner.setAdapter(adapter);
mSpinner.setOnItemSelectedListener(a);
}
}
package com.example;
import android.app.Activity;
import android.os.Bundle;
public class Third extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.third);
}
}
运行截图: