我有一个主类和一个单独的类(OnClickBtn.java),我想在其中保留按钮方法。我想不是从主类而是从 OnClickBtn.java 开始新的活动。
我已经运行了该程序,但它在启动时崩溃了。我想我的主要问题与 Intent 对象“Intent int_btnOpenA =new Intent(objContex, ActivityA.class)”的代码有关
这是我的代码 MainActivity.java:
package com.example.and2dtest;
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends Activity {
//START***VIEW LIST E BOTTONI
TextView txtView01;
Button btnOpenA;
Button btnOpenB;
EditText editText1;
OnClickBtn btnMethods=new OnClickBtn(this);
//END***VIEW LIST E BOTTONI
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//Start***set buttons views
txtView01=(TextView)findViewById(R.id.txtView01);
btnOpenA=(Button)findViewById(R.id.btnOpenA);
btnOpenB=(Button)findViewById(R.id.btnOpenB);
editText1=(EditText)findViewById(R.id.editText1);
btnOpenA.setOnClickListener(btnMethods);
btnOpenB.setOnClickListener(btnMethods);
//end***set buttons views
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
这是 OnClickBtn.java 类:
package com.example.and2dtest;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
public class OnClickBtn extends Activity implements OnClickListener {
MainActivity objContex;
public OnClickBtn(MainActivity cont){
objContex=cont;
}
Intent int_btnOpenA =new Intent(objContex, ActivityA.class); //<<THIS CAUSES CRASH!
@Override
public void onClick(View v) {
switch(v.getId()){
case R.id.btnOpenA:
Log.d("ZR", "in case btnOpenA");
//this.startActivity(int_btnOpenA); //<-clearly This Wont Start
break;
case R.id.btnOpenB:
Log.d("ZR","in case btnOpenB");
//do something
break;
}
}
}
我怎样才能让它工作,保持 startActivity 方法不在主要活动中?
谢谢,路德
好的,我一直在弄乱代码,以下似乎有效,ActivityA 不是从 MainActivity.java 而是从 OnClickBtn.java 开始的,正如我试图做的那样。这是代码:
MainActivity.java:
package com.example.and2dtest;
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends Activity {
//START***VIEW LIST E BOTTONI
TextView txtView01;
Button btnOpenA;
Button btnOpenB;
EditText editText1;
//END***VIEW LIST E BOTTONI
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
OnClickBtn btnMethods=new OnClickBtn(this);
//Start***set buttons views
txtView01=(TextView)findViewById(R.id.txtView01);
btnOpenA=(Button)findViewById(R.id.btnOpenA);
btnOpenB=(Button)findViewById(R.id.btnOpenB);
editText1=(EditText)findViewById(R.id.editText1);
btnOpenA.setOnClickListener(btnMethods);
btnOpenB.setOnClickListener(btnMethods);
//end***set buttons views
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
OnClickBtn.java:
package com.example.and2dtest;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
public class OnClickBtn implements OnClickListener {
Context objContext;
public OnClickBtn(Context con){
//v=view;
objContext=con;
}
//Intent int_btnOpenA =new Intent(objContext, ActivityA.class);
@Override
public void onClick(View v) {
switch(v.getId()){
case R.id.btnOpenA:
Log.d("ZR", "in case btnOpenA");
Intent int_btnOpenA =new Intent(objContext, ActivityA.class);
objContext.startActivity(int_btnOpenA);
break;
case R.id.btnOpenB:
Log.d("ZR","in case btnOpenB");
//do something
break;
}
}
}
活动A.java:
package com.example.and2dtest;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
public class ActivityA extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_activity);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.activity, menu);
return true;
}
}