我正在構建一個Android Studio應用程序,但每次點擊一個按鈕時,應用程序崩潰。 這是Java MainActivity文件:Android錯誤點擊
package com.example.alex.exemplu1;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.view.Menu;
import android.view.MenuItem;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
.setAction("Action", null).show();
}
});
}
@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);
}
public void buton1()
{
setContentView(R.layout.layout);
}
我說的唯一的事情就是布頓1功能,我希望它改變頁面。 這是我的XML文件:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:weightSum="1">
<TextView
android:layout_width="match_parent"
android:layout_height="110dp"
android:text="hello World!"
android:textSize="50dp"
android:textColor="@android:color/holo_blue_bright"
android:background="@android:color/holo_green_dark"
android:textAlignment="center"/>
<Button
android:layout_width="match_parent"
android:layout_height="100dp"
android:text="welcome"
android:onClick="buton1()"
/>
</LinearLayout>
我希望應用程序從「網主頁」的頁面切換到名爲「佈局」頁面 你能幫幫我嗎?
發佈您的logcat – AbhayBohra