还有另一个退出应用程序的代码吗? 我不知道为什么它不在这里工作它一直为我工作。 也许是关于我的xml文件,我认为它的重量或我的代码需要修复。
这是我的代码
package ir.whitegate.guardians;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.widget.Button;
import android.view.View;
import android.view.View.OnClickListener;
public class MainActivity extends Activity {
Button button;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
addListenerOnButton();
getActionBar().hide();
}
public void addListenerOnButton() {
final Context context = this;
button = (Button) findViewById(R.id.buttonstory);
button.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
Intent intent = new Intent(context, Story.class);
startActivity(intent);
}
public void EXIT(View view)
{
finish();
}
});
}
}
这是我的xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_height="match_parent"
android:layout_width="match_parent"
android:orientation="vertical"
android:background="@drawable/mainsithis">
<RelativeLayout
android:layout_height="82dp"
android:layout_width="match_parent">
<HorizontalScrollView
android:layout_height="wrap_content"
android:layout_width="wrap_content">
<LinearLayout
android:layout_height="wrap_content"
android:layout_width="82dp"
android:orientation="horizontal">
<Button
android:layout_height="82dp"
android:layout_width="35dp"
android:background="@drawable/toleft"/>
<Button
android:layout_height="match_parent"
android:layout_width="82dp"
android:background="@drawable/bios"/>
<Button
android:layout_height="match_parent"
android:layout_width="82dp"
android:background="@drawable/howtoread"/>
<Button
android:layout_height="match_parent"
android:layout_width="82dp"
android:background="@drawable/about"/>
<Button
android:layout_height="match_parent"
android:layout_width="82dp"
android:background="@drawable/writer"/>
<Button
android:layout_height="match_parent"
android:layout_width="82dp"
android:background="@drawable/rateit"/>
<Button
android:layout_height="match_parent"
android:layout_width="82dp"
android:background="@drawable/moreapps"/>
<Button
android:layout_height="82dp"
android:layout_width="35dp"
android:background="@drawable/toright"/>
</LinearLayout>
</HorizontalScrollView>
</RelativeLayout>
<LinearLayout
android:layout_height="match_parent"
android:layout_width="match_parent"
android:orientation="horizontal"
android:layout_weight="0.25"
android:background="@drawable/rainbow"/>
<LinearLayout
android:layout_height="match_parent"
android:layout_width="match_parent"
android:orientation="horizontal"
android:layout_weight="0.80">
<Button
android:id="@+id/buttonstory"
android:layout_height="65dp"
android:layout_width="wrap_content"
android:background="@drawable/starttoread"
android:layout_weight="1.0"/>
<Button
android:layout_height="65dp"
android:layout_width="wrap_content"
android:background="@drawable/exit"
android:layout_weight="1.0"
android:onClick="EXIT"/>
</LinearLayout>
答案 0 :(得分:0)
您应该从按钮clicklistener将EXIT方法移动到类。
更改addListenerOnButton方法,如下面的方法
public void addListenerOnButton() {
final Context context = this;
button = (Button) findViewById(R.id.buttonstory);
button.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
Intent intent = new Intent(context, Story.class);
startActivity(intent);
}
});
}
public void EXIT(View view)
{
finish();
}
答案 1 :(得分:0)
更改
Intent intent = new Intent(context, Story.class);
以强>
Intent intent = new Intent(MainActivity.this,Story.class);
错误的参数上下文
将其移至buttonStory
的 android:onClick="EXIT"
强>
和将代码添加到MainActivity 。
public void EXIT(View view)
{
finish();
}