当前位置: 代码迷 >> Android >> 帮忙看看程序为什么强制停止解决办法
  详细解决方案

帮忙看看程序为什么强制停止解决办法

热度:39   发布时间:2016-05-01 21:05:56.0
帮忙看看程序为什么强制停止
本人新手,开发的时候不报错 但是运行后强制停止,我贴下代码,各位帮忙看看


package lzz.activity;

import android.app.Activity;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.DialogInterface.OnClickListener;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;

public class Activity02 extends Activity {
  /** Called when the activity is first created. */
private Button myButton=null;
  @Override
  public void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.main);
  myButton=(Button)findViewById(R.id.myButton);
  myButton.setText("点击进入...");
  myButton.setOnClickListener((android.view.View.OnClickListener) new MyButtonListener());
  }
  class MyButtonListener implements OnClickListener{

@Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
Intent intent=new Intent();
intent.setClass(Activity02.this,OtherActivity.class);
Activity02.this.startActivity(intent);

}



 
  }
}

------------------------
package lzz.activity;

import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;

public class OtherActivity extends Activity {
private TextView myTextView=null;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.other);
myTextView=(TextView)findViewById(R.id.myTextView); 
myTextView.setText(R.string.other);
}
}

------解决方案--------------------
报错提示日志要发上来
------解决方案--------------------
光看代码没用的,建议你贴LOg啊
------解决方案--------------------
Log...Log...Log...
------解决方案--------------------
日志呢?
------解决方案--------------------
楼主的错误很明显,myButton.setOnClickListener((android.view.View.OnClickListener) new MyButtonListener());
import android.content.DialogInterface.OnClickListener;
public void onClick(DialogInterface dialog, int which) {
这3个自己对照看看就知道了。用Dialog的OnClickListener接口实例强制转换成android.view.View.OnClickListener接口,肯定是classCastExpression错误了。 

------解决方案--------------------
探讨

楼主的错误很明显,myButton.setOnClickListener((android.view.View.OnClickListener) new MyButtonListener());
import android.content.DialogInterface.OnClickListener;
public void onClick(DialogInterface dialog,……

------解决方案--------------------
探讨

楼主的错误很明显,myButton.setOnClickListener((android.view.View.OnClickListener) new MyButtonListener());
import android.content.DialogInterface.OnClickListener;
public void onClick(DialogInterface dialog,……

------解决方案--------------------
+1,这个错误是你太轻信Eclipse自动导入导致的,下次要看好。许多人在一开始都会犯这个错误。
探讨

楼主的错误很明显,myButton.setOnClickListener((android.view.View.OnClickListener) new MyButtonListener());
import android.content.DialogInterface.OnClickListener;
public void onClick(DialogInterface dialog,……
  相关解决方案