求助啊~新手在学android ,代码没报错,但运行不了
MainActivity_MixView.java:
package com.example.mixview;
import android.os.Bundle;
import android.app.Activity;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import android.widget.LinearLayout;
public class MainActivity_MixView extends Activity {
//定义一个访问图片的数组
int []images = new int[]{
R.drawable.a,
R.drawable.b,
R.drawable.c,
R.drawable.d,
R.drawable.e,
};
int currentImg = 0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main_mixview);
//获取LinearLayout布局容器
LinearLayout main=(LinearLayout)findViewById(R.id.root);
//程序创建ImageView组件
final ImageView image = new ImageView(this);
//将ImageView组件添加到LinearLayout布局容器
main.addView(image);
//初始化显示第一张图片
image.setImageResource(images[2]);
image.setOnClickListener( new OnClickListener()
{
public void onClick(View v)
{
if(currentImg >= 4)
{currentImg = -1;}
image.setImageResource(images[++currentImg]);
}
});
}
}
xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/root"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
</LinearLayout>
[解决办法]
你这是写了个啥?
下面都已经设置R.layout.activity_main_mixview为布局管理器了,你写那些还管个啥用啊
给你个例子吧,我刚写的
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Controller controller = new Controller(this,new ISystemMethods() {
@Override
public Object Output(Object sender, Object receiver, Object msg) {
// TODO Auto-generated method stub
return null;
}
@Override
public Executor GetHospotThreadPools() {
// TODO Auto-generated method stub
return null;
}
@Override
public String GetAppWorkingPath() {
// TODO Auto-generated method stub
return null;
}
});
setContentView(controller);
}
@Override
public void onOptionsMenuClosed(Menu menu) {
// TODO Auto-generated method stub
super.onOptionsMenuClosed(menu);
}
}
public class Controller extends RelativeLayout implements ICommunication{
private String TargetName;
private int QuantumID;
private String sender;
private ISystemMethods parentISystemMethods;
private Context parentContext;
private ShowMsg showmsg;
private ControlCollection collection;
public Controller(Context context,ISystemMethods parentISystemMethods) {
super(context);
this.parentContext=context;
this.parentISystemMethods=parentISystemMethods;
setBackgroundResource(R.drawable.background);
collection=ControlCollection.getControlCollection();
init();
}
public void init(){
showmsg=new ShowMsg(parentContext);
showmsg.setId(100);
showmsg.setText("这里显示文字");
showmsg.getLp().setMargins(40, 20, 20, 0);
addView(showmsg, showmsg.getLp());
collection.setShowMsg(showmsg);
ControllerHelper helper=new ControllerHelper(parentContext,this,collection);
List<MyButton> btnlist = helper.getBtnlist();
for(MyButton btn:btnlist){
addView(btn, btn.getLp());
}
collection.setBtnlist(btnlist);
}