【发布时间】:2011-07-29 22:23:40
【问题描述】:
一般来说,我对 Android 编程比较陌生,并且在使用 xml/java UI shuffle 时遇到了特别困难...我有一个布局,我想将其用作在实例化自定义视图类时显示的视图活动课。只需调用就可以很好地工作
setContentView(R.layout.mylayout) ;
在活动中或通过一个句柄从自定义视图类到活动。当我希望与布局上的小部件进行交互时,麻烦就来了——我尝试使用
来处理按钮myButton = (Button) findViewById(R.id.mybuttonid);
并分别与
Button myButton = new Button(contextHandle);
myButton = (Button) findViewById(R.layout.mybuttonid);
但是在这两种情况下,每当我尝试从假定的 myButton 对象调用任何方法时,我都会在 logcat 报告中得到 NullPointerException;显然 myButton 在上面给出的任何一种情况下都没有正确实例化。在这种结合 xml 和 java 以便它们可以动态调用方法的情况下,实例化视图组件的正确方法是什么?
谢谢, CCJ
编辑:感谢大家的回复,但我认为截至 2011 年 8 月 1 日,建议主要针对在活动类中实例化小部件的实现;我希望从自定义视图类中的 xml 布局实例化小部件——一个完全独立于扩展 View 并实现其自己的 OnClickListener 接口的活动类的类。以下是我的代码:
MyActivity 类:
package com.ccg.myactivity;
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.RadioButton;
public class MyActivity extends Activity implements OnClickListener {
private boolean touched = false;
private RadioButton myRB;
private Button runB;
private CustomView myView;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.mainlayout);
myRB = (RadioButton) findViewById(R.id.testrb);
runB = (Button) findViewById(R.id.goButton);
//set onClick listeners for activity class
runB.setOnClickListener(this);
}
public void onResume(){
super.onResume();
}
public void onClick(View v) {
// do something when the button is clicked
if (myRB.isChecked()){
setContentView(R.layout.mylayout);
myView = new CustomView(this,this); //passing in activity and context
//handles to custom View class
//myView.getAnotherB().setOnClickListener(this); //commented out as we
//don't want to register the custom view's button with the Activty class's
//OnClickListener; instead it should be registered with the custom View class's own
//OnClickListener implementation.
}
else{
Log.d("me","alt click");
}
}
}
自定义视图类:
package com.ccg.myactivity;
import android.app.Activity;
import android.content.Context;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.*;
import android.view.View.OnClickListener;
public class CustomView extends View implements OnClickListener{
private Button anotherB;
private Context contextHandle;
private Activity actHandle;
public CustomView(Context context, Activity act) {
super(context);
contextHandle = context;
actHandle = act;
//anotherB = new Button(contextHandle); //this shouldn't be necessary for
//instantiation from XML widget
initCustomView();
}
public void initCustomView(){
anotherB = (Button) findViewById(R.id.nextbutton);
anotherB.setOnClickListener(this);
}
public Button getAnotherB(){
return anotherB;
}
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
Log.d("me", "Got the custom click!");
}
}
生成默认视图的mainlayout.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
android:id="@+id/widget474"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical">
<RadioGroup android:id="@+id/widget30" android:orientation="horizontal"
android:layout_x="2dip" android:layout_y="57dip" android:layout_width="match_parent"
android:layout_height="wrap_content">
<RadioButton android:layout_height="wrap_content" android:id="@+id/testrb"
android:textSize="15sp" android:text="Run" android:layout_width="wrap_content"
android:textColor="#ffff99ff"></RadioButton>
</RadioGroup>
<Button android:layout_width="wrap_content" android:text="@string/RUN"
android:id="@+id/goButton" android:layout_height="wrap_content"
android:layout_x="222dip" android:layout_y="110dip"></Button>
</LinearLayout>
创建自定义视图布局的mylayout.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
android:id="@+id/widget0"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical">
<Button android:id="@+id/nextbutton" android:layout_height="wrap_content"
android:layout_width="wrap_content" android:text="work!!!"
>
</Button>
</LinearLayout>
好的,如果有人可以解释为什么从按钮对象 anotherB(上面的另一个 B.setOnClickListener(this),还有更简单的另一个 B.bringToFront())调用任何方法会导致在 logcat 中使用上述实现强制关闭和 nullpointerexception我将不胜感激。谢谢! CCJ
【问题讨论】:
-
您能否从您创建 UI 的 Activity 中发布布局和相关 Java 代码。
标签: java android android-layout android-widget