0

here is my code:

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;

}

what I am trying to do is, as you can see I have 3 buttons and I want when someone clicks one of them to represent the textview assigned to them (check the image for a better understanding). My TextViews are written in an other xml to in the main xml that I am working on because I don't any of them to appear in the main xml except if they are called.

enter image description here

Using the code above gives me this error in the 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)

this is line #153:

dis.setText(textViewArray[0].getText());

the xml of the textview:

<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." />

Any Idea how to fix this, that would be great if you are able to help me, Thanks

7
  • which part of the xml @Rod_Algonquin Commented Aug 15, 2014 at 19:54
  • the three textView above in the array Commented Aug 15, 2014 at 19:55
  • you can put your textviews in the same layout as your buttons and make them visible on buttonclick Commented Aug 15, 2014 at 19:57
  • @user3487063 can you tell me how to do that. the part of make them visible on buttonclick Commented Aug 15, 2014 at 20:00
  • @Rod_Algonquin i added the textviews Commented Aug 15, 2014 at 20:01

1 Answer 1

1

If your text views are on different layout file, then you need to get them using:

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); 

.... get other textviews too and add them to an array

or keep them in same layout as your buttons and make them visible on button click:

textView.setVisibility(View.VISIBLE)

by default have them invisible by setting it in layout xml using:

android:visibility="false"
Sign up to request clarification or add additional context in comments.

3 Comments

sorry but what should i replace context with
you should replace "yourtextviewlayout" with name of the xml file where textviews are, no need to change anything in the first line, see my edit
easiest thing would be to have your textviews in same layout as buttons and make them visibile and invisible as you want

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.