这是我的代码:
public class fm_weapons extends Activity implements OnClickListener {
ImageView display;
TextView dis;
TextView[] textViewArray;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.weapons);
display = (ImageView) findViewById(R.id.empty);
dis = (TextView) findViewById(R.id.emptyText);
textViewArray= new TextView[] {(TextView)findViewById(R.id.k_k),(TextView)findViewById(R.id.s_s),
(TextView)findViewById(R.id.h_d) };
......
kk_btn.setOnClickListener(this);
ss_btn.setOnClickListener(this);
hd_btn.setOnClickListener(this);
}
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
switch (v.getId()){
case R.id.kk_btn:
display.setImageResource(R.drawable.king_k);
dis.setText(textViewArray[0].getText());
break;
case R.id.ss_btn:
display.setImageResource(R.drawable.start_s);
dis.setText(textViewArray[1].getText());
break;
case R.id.hd_btn:
display.setImageResource(R.drawable.hidden_d);
dis.setText(textViewArray[2].getText());
break;
}
我想要做的是,你可以看到我有3个按钮,我想当有人点击其中一个来表示分配给他们的textview时(检查图像以便更好地理解)。我的TextViews是用我正在处理的主xml中的另一个xml编写的,因为我没有任何它们出现在主xml中,除非它们被调用。
使用上面的代码在logcat中给出了这个错误:
08-15 19:27:57.686: E/AndroidRuntime(2067): FATAL EXCEPTION: main
08-15 19:27:57.686: E/AndroidRuntime(2067): java.lang.NullPointerException
08-15 19:27:57.686: E/AndroidRuntime(2067): at com.example.guide.fm_weapons.onClick(fm_weapons.java:153)
08-15 19:27:57.686: E/AndroidRuntime(2067): at android.view.View.performClick(View.java:4202)
08-15 19:27:57.686: E/AndroidRuntime(2067): at android.view.View$PerformClick.run(View.java:17340)
08-15 19:27:57.686: E/AndroidRuntime(2067): at android.os.Handler.handleCallback(Handler.java:725)
08-15 19:27:57.686: E/AndroidRuntime(2067): at android.os.Handler.dispatchMessage(Handler.java:92)
08-15 19:27:57.686: E/AndroidRuntime(2067): at android.os.Looper.loop(Looper.java:137)
08-15 19:27:57.686: E/AndroidRuntime(2067): at android.app.ActivityThread.main(ActivityThread.java:5039)
08-15 19:27:57.686: E/AndroidRuntime(2067): at java.lang.reflect.Method.invokeNative(Native Method)
08-15 19:27:57.686: E/AndroidRuntime(2067): at java.lang.reflect.Method.invoke(Method.java:511)
08-15 19:27:57.686: E/AndroidRuntime(2067): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
08-15 19:27:57.686: E/AndroidRuntime(2067): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
08-15 19:27:57.686: E/AndroidRuntime(2067): at dalvik.system.NativeStart.main(Native Method)
08-15 19:27:59.365: E/Trace(2106): error opening trace file: No such file or directory (2)
这是第153行:
dis.setText(textViewArray[0].getText());
textview的xml:
<TextView
android:id="@+id/k_k"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textSize="15sp"
android:textColor="@android:color/holo_blue_bright"
android:text="Increases defense"/>
<TextView
android:id="@+id/s_s"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textSize="15sp"
android:textColor="@android:color/holo_blue_bright"
android:text="Increases strenght." />
<TextView
android:id="@+id/h_d"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textSize="15sp"
android:textColor="@android:color/holo_blue_bright"
android:text="Restores MP." />
任何想法如何解决这个问题,如果你能帮助我,那将是很好的,谢谢
答案 0 :(得分:1)
如果你的文本视图位于不同的布局文件中,那么你需要使用它们来获取它们:
LayoutInflater inflater
= (LayoutInflater)getBaseContext()
.getSystemService(LAYOUT_INFLATER_SERVICE);
View v = inflater.inflate(R.layout.<yourtextviewlayout>, null);
TextView t = (TextView)v.findViewById(R.id.k_k);
....获取其他文本视图并将它们添加到数组
或将它们保持在与按钮相同的布局中,并在按钮点击时显示它们:
textView.setVisibility(View.VISIBLE)
默认情况下,通过使用:
在布局xml中设置它们使它们不可见android:visibility="false"