メイン画面から次の画面に移動するときにボタンを押すと操作がうまくいく小さなアプリがありますが、次のページにボタンを追加したとき)それは壊れます。セカンダリ画面で戻るボタンを参照するとAndroidアプリがハングアップする
Fun.java
package com.forcetechnology.OptusApp;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class Fun extends Activity {
OnClickListener backListener;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.fun);
Button backButtonf = (Button)findViewById(com.forcetechnology.OptusApp.R.id.backtoMainf);
backListener = new OnClickListener()
{
public void onClick(View v)
{
Intent i = new Intent();
i.setClassName("com.forcetechnology.OptusApp", "com.forcetechnology.OptusApp.OptusAppMain");
startActivity(i);
}
};
backButtonf.setOnClickListener(backListener);
}
}
Fun.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" >
<ImageButton
android:id="@+id/backtoMainf"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/val5" />
</LinearLayout>
Main.xml
<ImageButton
android:id="@+id/funbutton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:background="@drawable/val5"
android:src="@drawable/val5" />
<ImageButton
android:id="@+id/executionbutton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="@+id/funbutton"
android:layout_alignParentRight="true"
android:background="@drawable/val2"
android:src="@drawable/val2" />
<ImageButton
android:id="@+id/performancebutton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/executionbutton"
android:layout_toLeftOf="@+id/funbutton"
android:background="@drawable/val3"
android:src="@drawable/val4" />
<ImageButton
android:id="@+id/innovationbutton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="@+id/executionbutton"
android:layout_alignParentLeft="true"
android:background="@drawable/val3"
android:src="@drawable/val3" />
<ImageButton
android:id="@+id/peoplebutton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="@+id/innovationbutton"
android:layout_toLeftOf="@+id/executionbutton"
android:background="@drawable/val1"
android:src="@drawable/val1" />
</RelativeLayout>
OPtusAppMain.java
package com.forcetechnology.OptusApp;
import android.R;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageButton;
import android.widget.TextView;
import android.widget.Toast;
public class OptusAppMain extends Activity
{
OnClickListener funListener,executionListener,innovationListener,peopleListener,performanceListener;;
TextView testView;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(com.forcetechnology.OptusApp.R.layout.main);
ImageButton funButton = (ImageButton)findViewById(com.forcetechnology.OptusApp.R.id.funbutton);
ImageButton executionButton = (ImageButton)findViewById(com.forcetechnology.OptusApp.R.id.executionbutton);
ImageButton innovationButton = (ImageButton)findViewById(com.forcetechnology.OptusApp.R.id.innovationbutton);
ImageButton peopleButton = (ImageButton)findViewById(com.forcetechnology.OptusApp.R.id.peoplebutton);
ImageButton performanceButton = (ImageButton)findViewById(com.forcetechnology.OptusApp.R.id.performancebutton);
funListener = new OnClickListener()
{
public void onClick(View v)
{
Intent i = new Intent();
i.setClassName("com.forcetechnology.OptusApp", "com.forcetechnology.OptusApp.Fun");
startActivity(i);
}
};
executionListener = new OnClickListener()
{
public void onClick(View v)
{
Intent i = new Intent();
i.setClassName("com.forcetechnology.OptusApp", "com.forcetechnology.OptusApp.Execution");
startActivity(i);
}
};
innovationListener = new OnClickListener()
{
public void onClick(View v)
{
Intent i = new Intent();
i.setClassName("com.forcetechnology.OptusApp", "com.forcetechnology.OptusApp.Innovation");
startActivity(i);
}
};
peopleListener = new OnClickListener()
{
public void onClick(View v)
{
Intent i = new Intent();
i.setClassName("com.forcetechnology.OptusApp", "com.forcetechnology.OptusApp.People");
startActivity(i);
}
};
performanceListener = new OnClickListener()
{
public void onClick(View v)
{
Intent i = new Intent();
i.setClassName("com.forcetechnology.OptusApp", "com.forcetechnology.OptusApp.Performance");
startActivity(i);
}
};
funButton.setOnClickListener(funListener);
executionButton.setOnClickListener(executionListener);
innovationButton.setOnClickListener(innovationListener);
peopleButton.setOnClickListener(peopleListener);
performanceButton.setOnClickListener(performanceListener);
}
}
編集:私はこの行にエラーをトレースしました。ボタンbackButtonf =(ボタン)findViewById(com.forcetechnology.OptusApp.R.id.backtoMainf); in fun.java。
}
}、ボタンbackButtonf =(ボタン)findViewById(com.forcetechnology.OptusApp.R.id.backtoMainf)。エラーラインです。 – CouncilScribe
'fun.xml'では' backtoMainf'は 'ImageButton'ですが、' Fun.java'では 'Button'にキャストしようとしています。 'ImageButton'は' Button'を拡張しないので、 'ClassCastException'を取得します。これを修正するには、 'Fun.java'で' Button'の代わりに 'ImageButton'を使います。 http://developer.android.com/reference/android/widget/ImageButton.html –
ありがとう、それを完全に逃した。 – CouncilScribe