0

I am making a game, and I am making a LevelSelect class with 3 different views, (jail, yard and sand), I used to have multiple intent's, but if I do it that way, there is no good way to shut down my application, and it doesn't return quite well.

I also tried using the setContentView() method, but as it turn's out it isn't able to load the class competent with the view, then I tried combining the class, but the class can't initialize objects outside of the current view in process. So my question is: How can I use multiple views without launching a new activity?

package com.muttgames.shadowpuzzle;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.LinearLayout;

public class LevelSelect extends Activity implements OnClickListener{




protected void onCreate(Bundle savedInstanceState){
    super.onCreate(savedInstanceState);
    setContentView(R.layout.jail_level_view);

    //Defining Inflater
    LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);

    //Defining sandView layout
    RelativeLayout jailView =  (RelativeLayout) findViewById(R.id.jail_level);
    View innerViewsJailView = inflater.inflate(R.id.yard_level, jailView, true);

    View jailMenu = innerViewsJailView.findViewById(R.id.jail2menu);
    jailMenu.setOnClickListener(this);

    //Defining sandView layout
    RelativeLayout yardView =  (RelativeLayout) findViewById(R.id.yard_level);
    View innerViewsYardView = inflater.inflate(R.id.yard_level, yardView, true);

    View yardMenu = innerViewsYardView.findViewById(R.id.yard2menu);
    yardMenu.setOnClickListener(this);

    //Defining sandView layout
    RelativeLayout sandView =  (RelativeLayout) findViewById(R.id.sand_level);
    View innerViewsSandView = inflater.inflate(R.id.sand_level, sandView, true);

    View sandMenu = innerViewsSandView.findViewById(R.id.sand2menu);
    sandMenu.setOnClickListener(this);
}

@Override
public void onClick(View v) {
    switch(v.getId()){
    case R.id.jail2menu:
        finish();
        break;
    case R.id.yard2menu:
        finish();
        break;
    case R.id.sand2menu:
        finish();
        break;
    }
}
}

I do realize that this is a very small class, but I do small tryout's to prevent myself from having to write immense parts of code again and again.

The error in this part is that I initialize yardMenu and sandMenu buttons, while they are not in the R.layout.jail_level_view xml file.

This code was to stop the levelselect activity whenever someone wanted to return to the main_activity.

How could I change that, that instead of shutting down the activity, I can just change it to the activity_main xml file.

xml Files(they pretty much all look the same, minor adjustments in color etc...:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/jail_level"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/app_bg"
android:orientation="vertical" >

<TextView
    android:id="@+id/view_title"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentTop="true"
    android:layout_centerHorizontal="true"
    android:layout_marginTop="30dp"
    android:text="Jail Levels"
    android:textSize="30sp" />

<Button
    android:id="@+id/level1"
    style="@drawable/jail_button"
    android:layout_width="50dp"
    android:layout_height="50dp"
    android:layout_below="@id/view_title"
    android:layout_marginLeft="45dp"
    android:layout_marginTop="30dp"
    android:background="@drawable/jail_button"
    android:text="1" />

<Button
    android:id="@+id/level2"
    style="@drawable/jail_button"
    android:layout_width="50dp"
    android:layout_height="50dp"
    android:layout_below="@id/view_title"
    android:layout_marginLeft="40dp"
    android:layout_marginTop="30dp"
    android:layout_toRightOf="@id/level1"
    android:background="@drawable/jail_button"
    android:text="2" />

<Button
    android:id="@+id/level3"
    style="@drawable/jail_button"
    android:layout_width="50dp"
    android:layout_height="50dp"
    android:layout_below="@id/view_title"
    android:layout_marginLeft="40dp"
    android:layout_marginTop="30dp"
    android:layout_toRightOf="@id/level2"
    android:background="@drawable/jail_button"
    android:text="3" />

<Button
    style="@drawable/yard_button"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:layout_marginBottom="30dp"
    android:layout_marginLeft="225dp"
    android:background="@drawable/yard_button"
    android:text="Next" />

<Button
    android:id="@+id/jail2menu"
    style="@drawable/menu_button"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:layout_marginBottom="30dp"
    android:layout_marginLeft="122dp"
    android:background="@drawable/menu_button"
    android:text="Menu" />

</RelativeLayout>

Logcat:

02-14 12:27:58.309: E/AndroidRuntime(8647): FATAL EXCEPTION: main

02-14 12:27:58.309: E/AndroidRuntime(8647): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.muttgames.shadowpuzzle/com.muttgames.shadowpuzzle.LevelSelect}: android.content.res.Resources$NotFoundException: Resource ID #0x7f08000d type #0x12 is not valid

02-14 12:27:58.309: E/AndroidRuntime(8647):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180)

02-14 12:27:58.309: E/AndroidRuntime(8647):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)

02-14 12:27:58.309: E/AndroidRuntime(8647):     at android.app.ActivityThread.access$600(ActivityThread.java:141)

02-14 12:27:58.309: E/AndroidRuntime(8647):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)

02-14 12:27:58.309: E/AndroidRuntime(8647):     at android.os.Handler.dispatchMessage(Handler.java:99)

02-14 12:27:58.309: E/AndroidRuntime(8647):     at android.os.Looper.loop(Looper.java:137)

02-14 12:27:58.309: E/AndroidRuntime(8647):     at android.app.ActivityThread.main(ActivityThread.java:5039)

02-14 12:27:58.309: E/AndroidRuntime(8647):     at java.lang.reflect.Method.invokeNative(Native Method)

02-14 12:27:58.309: E/AndroidRuntime(8647):     at java.lang.reflect.Method.invoke(Method.java:511)

02-14 12:27:58.309: E/AndroidRuntime(8647):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)

02-14 12:27:58.309: E/AndroidRuntime(8647):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
02-14 12:27:58.309: E/AndroidRuntime(8647):     at dalvik.system.NativeStart.main(Native Method)

02-14 12:27:58.309: E/AndroidRuntime(8647): Caused by: android.content.res.Resources$NotFoundException: Resource ID #0x7f08000d type #0x12 is not valid

02-14 12:27:58.309: E/AndroidRuntime(8647):     at android.content.res.Resources.loadXmlResourceParser(Resources.java:2144)

02-14 12:27:58.309: E/AndroidRuntime(8647):     at android.content.res.Resources.getLayout(Resources.java:853)

02-14 12:27:58.309: E/AndroidRuntime(8647):     at android.view.LayoutInflater.inflate(LayoutInflater.java:394)

02-14 12:27:58.309: E/AndroidRuntime(8647):     at com.muttgames.shadowpuzzle.LevelSelect.onCreate(LevelSelect.java:25)

02-14 12:27:58.309: E/AndroidRuntime(8647):     at android.app.Activity.performCreate(Activity.java:5104)
02-14 12:27:58.309: E/AndroidRuntime(8647):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)

02-14 12:27:58.309: E/AndroidRuntime(8647):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144)

02-14 12:27:58.309: E/AndroidRuntime(8647):     ... 11 more

1 Answers1

0

You need a layout inflater and a container. Make for example "sandMenu" a LinearLayout and then in code:

sandMenu.removeAllViews();
LayoutInflater inflater = (LayoutInflater) .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View innerViewsSandMenu = inflater.inflate(R.layout.yourLAYOUT, sandMenu, true);

And later you can access the inner views of sandMenu in this way:

innerViewsSandMenu.findViewById(R.id.....
PaNaVTEC
  • 2,505
  • 1
  • 23
  • 36
  • I take that with `sandMenu` you mean my `sand_level_view`, my `sandMenu` is actually a button that changes from my `sand_level_view` to my `activity_main` by using `finish();` Anyway, where do I put this part, and how do I initialize the `sand_level_view` so I can use it in the code that you just gave me? I'm sorry for asking so much, I just started about a week ago. – user1946839 Feb 14 '13 at 08:39
  • The code i attached to you is for inflate a layout in this case "jail_level_view" to a container, when you have inflated this layout you can use the views inside "jail_level_view" by doing findViewById – PaNaVTEC Feb 14 '13 at 09:08
  • I think I get what it does, it initializes, or in android; inflates the jail_level_view into an object you can later call variables from. Anyway, it doesn't work, it still doesn't launch my `LevelSelect.class` when I click try to open it from my `MainActivity.class`, could you maybe edit my code so that it works? :/ – user1946839 Feb 14 '13 at 09:14
  • put your inflate lines behind setContentView in onCreate and be careful with sand_level_view that it haves correct width, height and orientation, can you share your XML too? – PaNaVTEC Feb 14 '13 at 09:27
  • Okay, I got them working, there is just 1 point that isn't working yet. I declare my 3 views(`jail_level_view yard_level_view sand_level_view`) but when I try to declare my buttons, that doesn't work again... I'll update my main post with the code I currently have. – user1946839 Feb 14 '13 at 11:13
  • Please can you be more specific? What is "doesn't work again"? It crashes? It returns null on the findView? The onclick dont work? – PaNaVTEC Feb 14 '13 at 11:19
  • As soon as I click my select level button in the `MainActivity.class` it crashes. ".... has suddenly stopped." If I don't write the buttons in `onCreate()` it works fine, but as soon as I implement the buttons, it crashes. As a side note, if I only implement the `sandMenu` button, it does work, my guess is that this is because the `sand_level_view` is inflated last. – user1946839 Feb 14 '13 at 11:26
  • Post your layout.sand_level_view.xml jail_level_view.xml and the logcat with the code that you have posted please. – PaNaVTEC Feb 14 '13 at 11:34
  • your doing "findViewById(R.LAYOUT.asdasdasd)" instead of findViewById(R.id.YOUR_ID) so this findViewById is retuning null and is not inflating to the layout, also the buttons are not populated and returns null, this is why you are getting nullpointers. – PaNaVTEC Feb 14 '13 at 12:19
  • Okay, I gave my xml files an Id, and I called them by that, then I got another error, this time I got it, I was assigning LinearLayout's to RelativeLayouts, but after that, I got another :/ I'll update the code and the log cat. – user1946839 Feb 14 '13 at 12:30
  • You are still doing: LinearLayout sandView = (LinearLayout) findViewById(R.layout.sand_level_view); Please understand what are you doing, you are finding a view, the views are declared by view IDS not view LAYOUTS you never can find a view with R.layout. – PaNaVTEC Feb 14 '13 at 12:35
  • Yes, I was still busy with updating the post :/ – user1946839 Feb 14 '13 at 12:37