Android : There is another way for Switch case? - android

on Wednesday, October 8, 2014


After two days of search I found a way to do what I need. (I am begginer with Android developement).


MainActivity:



package com.example.getquotes;

import java.util.Random;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.res.Resources;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.google.android.gms.ads.AdRequest;
import com.google.android.gms.ads.AdSize;
import com.google.android.gms.ads.AdView;

public class MainActivity extends ActionBarActivity {
Context context;
private String[] myString;
private static final Random rgenerator = new Random();
int index = 0;
private AdView adView;
private static final String AD_UNIT_ID = "";

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
adView = new AdView(this);
adView.setAdSize(AdSize.SMART_BANNER);
adView.setAdUnitId(AD_UNIT_ID);
context = this;
LinearLayout layout = (LinearLayout) findViewById(R.id.ads_lin);
layout.addView(adView);

AdRequest adRequest = new AdRequest.Builder()
.addTestDevice(AdRequest.DEVICE_ID_EMULATOR)
.addTestDevice("5B895A3CC0CA50D56506E300A4C8342B")
.addTestDevice("D039292A1F434C999B21503D63D6FD88")

.addTestDevice("TEST_EMULATOR").build();

//Start loading ad in background
adView.loadAd(adRequest);
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;

}

@SuppressLint("CutPasteId")
@Override
public boolean onOptionsItemSelected(MenuItem item) {

super.onOptionsItemSelected(item);

switch (item.getItemId()) {
case R.id.phone:
Toast.makeText(getBaseContext(), "Munca",
Toast.LENGTH_SHORT).show();
Button btn = (Button) findViewById(R.id.btn);
Resources res = getResources();
myString = res.getStringArray(R.array.work);
final TextView tv = (TextView) findViewById(R.id.quote);
final String q = myString[rgenerator.nextInt(myString.length)];
tv.setText(q);
btn.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
myString = getResources().getStringArray(R.array.work);
index = rgenerator.nextInt(myString.length);
String q = myString[index];
tv.setText(q);
}
});

break;

case R.id.computer:

Toast.makeText(getBaseContext(), "Inspirationale",
Toast.LENGTH_SHORT).show();
Button btn2 = (Button) findViewById(R.id.btn);
final TextView tv2 = (TextView) findViewById(R.id.quote);
btn2.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
myString = getResources().getStringArray(
R.array.inspiration);
index = rgenerator.nextInt(myString.length);
String q = myString[index];
tv2.setText(q);
}
});
break;

case R.id.gamepad:
Toast.makeText(getBaseContext(), "Motivare",
Toast.LENGTH_SHORT).show();
Button btn3 = (Button) findViewById(R.id.btn);
final TextView tv3 = (TextView) findViewById(R.id.quote);
btn3.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
myString = getResources()
.getStringArray(R.array.motivation);
index = rgenerator.nextInt(myString.length);
String q = myString[index];
tv3.setText(q);
}
});
break;

case R.id.camera:
Toast.makeText(getBaseContext(), "Succes",
Toast.LENGTH_SHORT).show();
Button btn4 = (Button) findViewById(R.id.btn);
final TextView tv4 = (TextView) findViewById(R.id.quote);
btn4.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
myString = getResources().getStringArray(R.array.succes);
index = rgenerator.nextInt(myString.length);
String q = myString[index];
tv4.setText(q);
}
});
break;

case R.id.video:
Toast.makeText(getBaseContext(), "Iubire",
Toast.LENGTH_SHORT).show();
Button btn5 = (Button) findViewById(R.id.btn);
final TextView tv5 = (TextView) findViewById(R.id.quote);
btn5.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
myString = getResources().getStringArray(R.array.love);
index = rgenerator.nextInt(myString.length);
String q = myString[index];
tv5.setText(q);
}
});
break;

case R.id.email:
Toast.makeText(getBaseContext(), "Munca",
Toast.LENGTH_SHORT).show();

Button btn6 = (Button) findViewById(R.id.btn);
final TextView tv6 = (TextView) findViewById(R.id.quote);
btn6.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
myString = getResources().getStringArray(R.array.work);
index = rgenerator.nextInt(myString.length);
String q = myString[index];
tv6.setText(q);
}
});
break;
}
return true;
}
@Override
public void onResume() {
super.onResume();
if (adView != null) {
adView.resume();
}
}

@Override
public void onPause() {
if (adView != null) {
adView.pause();
}
super.onPause();
}

/** Called before the activity is destroyed. */
@Override
public void onDestroy() {
// Destroy the AdView.
if (adView != null) {
adView.destroy();
}
super.onDestroy();
}

}


I don't know but I am thinking there is a easier way for Switch case, this is the single method I have found.


And this is activity_main.xml



<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.getquotes.MainActivity" >

<TextView
android:id="@+id/quote"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:textSize="18sp"
android:layout_alignParentTop="true"
android:layout_marginTop="10dp" />

<Button
android:id="@+id/btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:src="@drawable/btn"
android:layout_alignParentBottom="true"
android:layout_marginBottom="10dp" />

<LinearLayout1
android:id="@+id/ads_lin"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@null"
android:gravity="center"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:orientation="vertical" >

</LinearLayout1>

</RelativeLayout>


Everything was working good until I added ads. When I run the app, it stop.


LOGCAT:



10-08 15:42:38.069: E/AndroidRuntime(16445): FATAL EXCEPTION: main
10-08 15:42:38.069: E/AndroidRuntime(16445): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.getquotes/com.example.getquotes.MainActivity}: android.view.InflateException: Binary XML file line #25: Error inflating class LinearLayout1
10-08 15:42:38.069: E/AndroidRuntime(16445): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2245)
10-08 15:42:38.069: E/AndroidRuntime(16445): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2299)
10-08 15:42:38.069: E/AndroidRuntime(16445): at android.app.ActivityThread.access$700(ActivityThread.java:150)
10-08 15:42:38.069: E/AndroidRuntime(16445): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1280)
10-08 15:42:38.069: E/AndroidRuntime(16445): at android.os.Handler.dispatchMessage(Handler.java:99)
10-08 15:42:38.069: E/AndroidRuntime(16445): at android.os.Looper.loop(Looper.java:137)
10-08 15:42:38.069: E/AndroidRuntime(16445): at android.app.ActivityThread.main(ActivityThread.java:5283)
10-08 15:42:38.069: E/AndroidRuntime(16445): at java.lang.reflect.Method.invokeNative(Native Method)
10-08 15:42:38.069: E/AndroidRuntime(16445): at java.lang.reflect.Method.invoke(Method.java:511)
10-08 15:42:38.069: E/AndroidRuntime(16445): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1102)
10-08 15:42:38.069: E/AndroidRuntime(16445): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:869)
10-08 15:42:38.069: E/AndroidRuntime(16445): at dalvik.system.NativeStart.main(Native Method)
10-08 15:42:38.069: E/AndroidRuntime(16445): Caused by: android.view.InflateException: Binary XML file line #25: Error inflating class LinearLayout1
10-08 15:42:38.069: E/AndroidRuntime(16445): at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:704)
10-08 15:42:38.069: E/AndroidRuntime(16445): at android.view.LayoutInflater.rInflate(LayoutInflater.java:752)
10-08 15:42:38.069: E/AndroidRuntime(16445): at android.view.LayoutInflater.inflate(LayoutInflater.java:495)
10-08 15:42:38.069: E/AndroidRuntime(16445): at android.view.LayoutInflater.inflate(LayoutInflater.java:397)
10-08 15:42:38.069: E/AndroidRuntime(16445): at android.view.LayoutInflater.inflate(LayoutInflater.java:353)
10-08 15:42:38.069: E/AndroidRuntime(16445): at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:364)
10-08 15:42:38.069: E/AndroidRuntime(16445): at android.app.Activity.setContentView(Activity.java:1930)
10-08 15:42:38.069: E/AndroidRuntime(16445): at android.support.v7.app.ActionBarActivity.superSetContentView(ActionBarActivity.java:217)
10-08 15:42:38.069: E/AndroidRuntime(16445): at android.support.v7.app.ActionBarActivityDelegateICS.setContentView(ActionBarActivityDelegateICS.java:110)
10-08 15:42:38.069: E/AndroidRuntime(16445): at android.support.v7.app.ActionBarActivity.setContentView(ActionBarActivity.java:77)
10-08 15:42:38.069: E/AndroidRuntime(16445): at com.example.getquotes.MainActivity.onCreate(MainActivity.java:33)
10-08 15:42:38.069: E/AndroidRuntime(16445): at android.app.Activity.performCreate(Activity.java:5283)
10-08 15:42:38.069: E/AndroidRuntime(16445): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1097)
10-08 15:42:38.069: E/AndroidRuntime(16445): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2209)
10-08 15:42:38.069: E/AndroidRuntime(16445): ... 11 more
10-08 15:42:38.069: E/AndroidRuntime(16445): Caused by: java.lang.ClassNotFoundException: Didn't find class "android.view.LinearLayout1" on path: /data/app/com.example.getquotes-2.apk
10-08 15:42:38.069: E/AndroidRuntime(16445): at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:65)
10-08 15:42:38.069: E/AndroidRuntime(16445): at java.lang.ClassLoader.loadClass(ClassLoader.java:501)
10-08 15:42:38.069: E/AndroidRuntime(16445): at java.lang.ClassLoader.loadClass(ClassLoader.java:461)
10-08 15:42:38.069: E/AndroidRuntime(16445): at android.view.LayoutInflater.createView(LayoutInflater.java:558)
10-08 15:42:38.069: E/AndroidRuntime(16445): at android.view.LayoutInflater.onCreateView(LayoutInflater.java:649)
10-08 15:42:38.069: E/AndroidRuntime(16445): at com.android.internal.policy.impl.PhoneLayoutInflater.onCreateView(PhoneLayoutInflater.java:66)
10-08 15:42:38.069: E/AndroidRuntime(16445): at android.view.LayoutInflater.onCreateView(LayoutInflater.java:666)
10-08 15:42:38.069: E/AndroidRuntime(16445): at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:691)
10-08 15:42:38.069: E/AndroidRuntime(16445): ... 24 more


I don't know from where comes the error.


0 comments:

Post a Comment