getting error while using multispinner - android

I want to select multiple items in spinner but getting runtime exception due to java.lang.CastException in XML file
XML:
<com.example.lenovo.abc.MultiSpinner
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/hobby"
android:layout_marginLeft="30dp"
android:layout_marginStart="30dp"
android:layout_marginTop="30dp"
android:layout_weight="0.91"
/>
MultiSpinner class:
Android Spinner with multiple choice
About class
package com.example.lenovo.abc;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.widget.TextView;
import java.util.Arrays;
import java.util.List;
public class About extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.about);
TextView intro= (TextView) findViewById(R.id.introducing);
MultiSpinner hobby= (MultiSpinner) findViewById(R.id.hobby);
List<String> h = Arrays.asList(getResources().getStringArray(R.array.hobby));
hobby.setItems(h,"Hobbies", (MultiSpinner.MultiSpinnerListener) this);
}
}

hobby.setItems(h,"Hobbies", (MultiSpinner.MultiSpinnerListener) this);
You are not implementing MultiSpinner.MultiSpinnerListener in this activity and passing context casting to MultiSpinner.MultiSpinnerListener. Try implementing the listener in this activity.

Related

React-native Android Splashscreen - Custom fonts

I created a splashscreen for my react-native app and have to implement TextView with custom fonts.
I created an Android layout specifically for the splashscreen with res/layout/launch_screen.xml and the TextViews appears but without custom fonts then i launch that on splashActivity file.
But it's strange because when i inject the layout directly on the mainActivity file the fonts seems be loaded.
So i think it's due to the fonts path in SplashActivity file but the code seems be ok.
Here are the files :
SplashActivity.js :
package com.app;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.widget.TextView;
import android.graphics.Typeface;
import android.content.res.AssetManager;
public class SplashActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.launch_screen);
TextView txt_1 = (TextView) findViewById(R.id.din_bold);
Typeface font_1 = Typeface.createFromAsset(getAssets(), "fonts/DinCondBold.otf");
txt_1.setTypeface(font_1);
TextView txt_2 = (TextView) findViewById(R.id.din_light);
Typeface font_2 = Typeface.createFromAsset(getAssets(), "fonts/DinCondLight.otf");
txt_2.setTypeface(font_2);
Intent intent = new Intent(this, MainActivity.class);
startActivity(intent);
finish();
}
}
MainActivity.js
package com.app;
import android.app.Activity;
import com.facebook.react.ReactActivity;
import android.os.Bundle;
import org.devio.rn.splashscreen.SplashScreen;
public class MainActivity extends ReactActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
SplashScreen.show(this, R.style.SplashScreenTheme);
}
/**
* Returns the name of the main component registered from JavaScript.
* This is used to schedule rendering of the component.
*/
#Override
protected String getMainComponentName() {
return "app";
}
}
And launch_screen.xml
<LinearLayout 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"
android:background="#drawable/background"
android:gravity="center"
android:orientation="vertical"
tools:context="com.mitsu.MainActivity">
<TextView
android:id="#+id/din_bold"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="SCAN PRODUITS"
android:textColor="#eb212e"
android:textSize="36sp"
android:textStyle="bold" />
<TextView
android:id="#+id/din_light"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="BIENVENUE"
android:textColor="#717171"
android:textSize="36sp" />
<ImageView
android:layout_width="180dp"
android:layout_height="180dp"
android:layout_marginTop="14dp"
android:src="#drawable/splash_logo" />
</LinearLayout>
Does anyone know what's going on?

List Crashes App when updated

I'm trying to get a ListView to show the contents of a simple array but my program crashes at the setAdapeter line. As I understand this is supposed to be because the final value comes out as null but I don't see why that would be the case or how to fix it. I have already tried using ArrayAdapeter instead as well as catching the null exception with a try block. The former changes nothing and the latter simply shows a blank activity with no list to speak of. Please help.
Here's Main Activity
package ballton.fowdeckbuilder;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.ListAdapter;
import android.widget.ArrayAdapter;
import android.widget.Button;
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button btnDecks = (Button) (findViewById(R.id.btnDecks));
Button btnCards = (Button) (findViewById(R.id.btnCards));
btnDecks.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
startActivity(new Intent(MainActivity.this, DeckCatalogue.class));
}
});
btnCards.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
startActivity(new Intent(MainActivity.this, CardCatalogue.class));
}
});
}
}
Here's the activity with the Problem
package ballton.fowdeckbuilder;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.widget.ArrayAdapter;
import android.widget.ListAdapter;
import android.widget.ListView;
public class DeckCatalogue extends AppCompatActivity {
ListView Ldecks;
String TAG = "TAG";
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_deck_catalogue);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
Ldecks = (ListView) (findViewById(R.id.DeckList));
String Decks[] = new String[] {"Faria","Melgis"};
ListAdapter feed = (new ArrayAdapter<String>(this, R.layout.show_deck, Decks));
Ldecks.setAdapter(feed);
}
}
Here's the Layout for that activity
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="#string/appbar_scrolling_view_behavior"
tools:context="ballton.fowdeckbuilder.DeckCatalogue"
tools:showIn="#layout/activity_deck_catalogue">
<ListView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/DeckList"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
</RelativeLayout>
And Here's the layout XML I use for the list times
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceSmall"
android:text="Small Text"
android:id="#+id/Deck" />
</LinearLayout>
Use this code instead of yours:
ListAdapter feed = (new ArrayAdapter<String>(this, R.layout.show_deck, R.id.Deck, Decks));
This happened because you must supply the resource ID (as your xml: R.id.Deck) for the TextView you used for ListView.

findViewById is not working

I have a TextView in my app and I want to change its textSize programmatically using the setTextSize method, but everytime I ran my code, I got an error message : "java.lang.NullPointerException". I tried to print the value of the TextView and it got NULL value.
Here's my code:
LoadActivity.java
import android.app.Activity;
import android.os.Bundle;
import android.widget.ImageView;
import android.widget.TextView;
public class LoadActivity extends Activity{
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
TextView text = (TextView) findViewById(R.id.loadTextView);
try{
System.out.println("TextView: "+text);
}catch(Exception e){
System.err.println(e);
}
text.setTextSize(30);
setContentView(R.layout.activity_load);
}
}
activity_load.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#ffffffff">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Please Wait"
android:id="#+id/loadTextView"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true"
android:typeface="monospace"
android:textSize="35sp"
android:textStyle="bold"
android:textColor="#000000"/>
</RelativeLayout>
What do you think is the problem?
setContentView(R.layout.activity_load); should be declared before initializing your views(TextView) and System.out.println("TextView: "+text); is not correct form to print value. The correct form is System.out.println("TextView: "+text.getText().toString());
So your corrected code will be like this
import android.app.Activity;
import android.os.Bundle;
import android.widget.ImageView;
import android.widget.TextView;
public class LoadActivity extends Activity{
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_load);
TextView text = (TextView) findViewById(R.id.loadTextView);
try{
System.out.println("TextView: "+text.getText().toString());
}catch(Exception e){
System.err.println(e);
}
text.setTextSize(30);
}
}
Use this in your onCreate Method
setContentView(R.layout.activity_load);
TextView text = (TextView) findViewById(R.id.loadTextView);
text.setTextSize(30);
In order for you to find views by their id, you need to write
setContentView(R.layout.activity_load);
This is because it essentially gets the layout resource file that defines your UI, and then allows you to retrieve other views within that layout (with findViewById();)
Look here for more information on how to properly set up Activites
import android.app.Activity;
import android.os.Bundle;
import android.widget.ImageView;
import android.widget.TextView;
public class LoadActivity extends Activity{
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_load); // Retrieves layout that defines your UI
// After setContentView is called, you can find views within the layout
TextView text = (TextView) findViewById(R.id.loadTextView);
try{
System.out.println("TextView: "+text.getText().toString());
}catch(Exception e){
System.err.println(e);
}
text.setTextSize(30);
}
}

setOnClickListener makes Force Close

When I launch the application and click the button to go to the Activity, I suddenly get a Force Close. I've tried to erase it all and write it again, but it is still there. What could be the problem?
package com.alexgascon.formuladora;
import android.app.Activity;
import android.view.View.OnClickListener;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class Matematicas extends Activity {
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
final Button BtnEcSegundoGrado = (Button)findViewById(R.id.ecsegundogrado);
final Button BtnFracciones = (Button)findViewById(R.id.fracciones);
final Button BtnMCD = (Button)findViewById(R.id.maximocomunBoton);
BtnMCD.setOnClickListener(new OnClickListener(){
#Override
public void onClick(View arg0) {
Intent MCDintent = new Intent(Matematicas.this,Maximocomun.class);
startActivity(MCDintent);
}
});
}
}
And here is the layout code
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#drawable/pizarramatesverde"
android:gravity="center" >
<Button
android:id="#+id/ecsegundogrado"
android:layout_height="wrap_content"
android:layout_width="265sp"
android:text="#string/ecsegundo"
android:textColor="#color/Negro"
/>
<Button
android:id="#+id/fracciones"
android:layout_height="wrap_content"
android:layout_width="265sp"
android:text="#string/fracciones"
android:layout_below="#id/ecsegundogrado"
android:textColor="#color/Negro"
/>
<Button
android:id="#+id/maximocomunBoton"
android:layout_height="wrap_content"
android:layout_width="265sp"
android:text="#string/maximocomun"
android:layout_below="#id/fracciones"
android:layout_marginBottom="80sp"
android:textColor="#color/Negro"
/>
</RelativeLayout>
You need to call setContentView before findViewById:
setContentView(R.layout.yourlayoutxmlname);
final Button BtnEcSegundoGrado = (Button)findViewById(R.id.ecsegundogrado);
final Button BtnFracciones = (Button)findViewById(R.id.fracciones);
final Button BtnMCD = (Button)findViewById(R.id.maximocomunBoton);
You need to call setContentView(R.layout.yourxmlLayout); to have the layout being referenced on your activity class.
Try to pop a toast from the click listener and see if it works. If it does - then the problem is in the activity that you want to launch.
You need to add the setContentView(R.layout.yourlayoutxmlname);.
Try the following code:
package com.alexgascon.formuladora;
import android.app.Activity;
import android.view.View.OnClickListener;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class Matematicas extends Activity {
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.yourlayoutxmlname);
final Button BtnEcSegundoGrado = (Button)findViewById(R.id.ecsegundogrado);
final Button BtnFracciones = (Button)findViewById(R.id.fracciones);
final Button BtnMCD = (Button)findViewById(R.id.maximocomunBoton);
BtnMCD.setOnClickListener( new OnClickListener(){
#Override
public void onClick(View arg0) {
Intent MCDintent = new Intent(Matematicas.this,Maximocomun.class);
startActivity(MCDintent);
}
});
}
}

Android OnItemClickListener not working

I'm using android 2.3.3. I set up a layout like,
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="fill_parent"
android:layout_height="fill_parent">
<ListView android:id="#+id/mainList" android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
And I'm manipulating it with
package org.dewsworld.ui;
import org.dewsworld.core.NBConfig;
import android.app.Activity;
import android.app.ListActivity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ListAdapter;
import android.widget.ListView;
public class NewsBotActivity extends ListActivity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
setListAdapter( new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1,
NBConfig.topics));
ListView listView = (ListView) findViewById(R.id.mainList) ;
listView.setOnItemClickListener( new OnItemClickListener() {
});
}
}
Using eclise IDE, when I setthe OnItemClickListener it gives me the error
The method setOnItemClickListener(AdapterView.OnItemClickListener) in the type AdapterView<ListAdapter> is not applicable for the arguments (new
OnItemClickListener(){})
I can't fix this. [I've added an image with the error]
It seems you have imported the wrong OnItemClickListener, try this one instead, and remove import of android.view.View.OnClickListener
import android.widget.AdapterView.OnItemClickListener;
how about filling in the body of your new object by defining the onItemClick() function:
public void onItemClick(AdapterView parent, View v, int position, long id)
{
// Display a messagebox.
Toast.makeText(this,"Your Listener Works!",Toast.LENGTH_SHORT).show();
}
try using ctrl+shift+o in eclipse to organize all your imports automatically...
Just implement OnItemClickListener to your class.
Like This:
public class ClassName extends Activity implements OnItemClickListener{}

Categories

Resources