我正在尝试通过在Android Studio中启动新活动来更改布局。但是,当我按下按钮时,它正在关闭。我可以进入MainActivity,但为“设置”更改它会使整个应用程序崩溃。
这是MainActivity类
public class MainActivity extends AppCompatActivity implements View.OnClickListener{
Button btnApp,btnSettings,btnExit;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btnApp = findViewById(R.id.appButton);
btnApp.setOnClickListener(this);
btnSettings = findViewById(R.id.settingsButton);
btnSettings.setOnClickListener(this);
btnExit = findViewById(R.id.exitButton);
btnExit.setOnClickListener(this);
}
@Override
public void onClick(View v) {
switch(v.getId()){
case R.id.appButton:
Intent app = new Intent(this,App.class);
startActivity(app);
break;
case R.id.settingsButton:
Intent settings = new Intent(this,Settings.class);
startActivity(settings);
break;
case R.id.exitButton:
finishAffinity();
System.exit(0);
break;
default:
break;
}
}
}
这是“设置”类
public class Settings extends AppCompatActivity implements View.OnClickListener{
EditText DlugoscGeo;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.settings_layout);
DlugoscGeo = findViewById(R.id.dlugoscGeoWpis);
DlugoscGeo.setOnClickListener(this);
}
@Override
public void onClick(View v) {
switch (v.getId()){
case R.id.dlugoscGeoWpis:
break;
default:
break;
}
}
}
这是设置xml文件
<?xml version="1.0" encoding="utf-8"?>
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<TableRow
android:layout_width="match_parent"
android:layout_height="match_parent" >
<TableRow
android:layout_width="match_parent"
android:layout_height="match_parent" >
<EditText
android:id="@+id/dlugoscGeoWpis"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_margin="3dp"
android:ems="19"
android:hint="@string/przykład"
android:inputType="numberDecimal" />
</TableRow>
</TableLayout>
我在上一个程序中完成了此操作,通常可以以此来更改活动,但是现在该应用程序无法正常工作。我在其他应用中尝试过,但仍然可以正常使用。