Fragment and Toggle Button - android

I am very new to Android. I am trying to add a simple toggle button to a class which extends Fragment class.
I tried everything but I could not get it working. I managed to add a normal button but that is not useful for me.
All I want is add a toggle button.
Please help me.
Thanks all
<?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="#cccccc" >
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="18dp"
android:text="Light Tab"
android:textColor="#333333"
android:textSize="20sp" />
<Button
android:id="#+id/fragment_button"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/textView1"
android:layout_alignRight="#+id/textView1"
android:layout_below="#+id/textView1"
android:layout_marginTop="61dp"
android:text="#string/btn_fragment" />
</RelativeLayout>
package com.baris.smartgame;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.app.Activity;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;
public class Light extends Fragment {
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.light, container, false);
((Button) view.findViewById(R.id.fragment_button))
.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
Activity activity = getActivity();
if (activity != null) {
Toast.makeText(activity,
R.string.toast_you_just_clicked_a_fragment,
Toast.LENGTH_SHORT).show();
}
}
});
return view;
}
}

Use This as an Example :
<LinearLayout
android:id="#+id/txtDayTypes"
android:layout_below="#+id/main_header"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<ToggleButton
android:id="#+id/tglDay1"
android:layout_width="160dp"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:text="Saturday"
android:textOn="Saturday"
android:textOff="Saturday"
android:checked="true"/>
</LinearLayout>
and you can Access it by :
if(currentButton != R.id.tglDay1) ((ToggleButton)findViewById(R.id.tglDay1)).setChecked(false);

Related

Null pointer exception while setting onClickListener on an imageView in a fragment class [duplicate]

This question already has answers here:
What is a NullPointerException, and how do I fix it?
(12 answers)
Closed 7 years ago.
I am new to android and I am in a learning phase.I searched a lot but couldn't find an appropriate solution for this. I am getting a null pointer exception at notes.setOnClickListener though I have added findViewById statement
package com.example.hp.newcalendar;
import android.app.Activity;
import android.support.v4.app.Fragment;
import android.content.Context;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
public class PreviouslyCalledFragment extends Fragment {
ListView students;
TextView test;
ImageView notes;
SimpleDateFormat formatter = new SimpleDateFormat("dd M yyyy hh:mm a");
String dateInString="23 Dec 2015 18:00 pm";
View view;
Context mContext;
ArrayList<FieldDetails> searchResults;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
view = inflater.inflate(R.layout.activity_previously_called_fragment, container, false);
students = (ListView) view.findViewById(R.id.alreadyCalled);
notes=(ImageView)view.findViewById(R.id.notesView);
searchResults = GetSearchResults();
students.setAdapter(new CustomAdapter_Notes(mContext, searchResults));
notes.setOnClickListener(new View.OnClickListener() { //null pointer exception
#Override
public void onClick(View v) {
Toast.makeText(mContext,"hello", Toast.LENGTH_LONG).show();
}
});
return view;
}
#Override
public void onAttach(final Activity activity) {
super.onAttach(activity);
mContext = activity;
}
private ArrayList<FieldDetails> GetSearchResults() {
ArrayList<FieldDetails> results = new ArrayList<FieldDetails>();
FieldDetails fieldDetails = new FieldDetails();
fieldDetails.setName("Aman Jain");
fieldDetails.setTime("Dec 23,18:00 pm");
results.add(fieldDetails);
fieldDetails = new FieldDetails();
fieldDetails.setName("Keshav Sharma");
fieldDetails.setTime("Dec 23,19:00 pm");
results.add(fieldDetails);
return results;
}
}
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=".MainActivity"
android:padding="5dp"
android:background="#ffffff">
<ListView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/alreadyCalled"
android:layout_centerHorizontal="true"
android:clickable="false"
android:layout_alignParentTop="true" />
Custom_layout xml :
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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:textSize="15dp"
android:id="#+id/nameOfInterviewee"
android:textColor="#008080"
android:layout_marginTop="5dp"
android:paddingBottom="5dp"
android:layout_marginLeft="2dp"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceSmall"
android:text="Small Text"
android:textSize="12dp"
android:id="#+id/dateAndTime"
android:textColor="#008080"
android:paddingBottom="10dp"
android:layout_below="#+id/nameOfInterviewee"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginTop="-5dp"
/>
<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="#848482"
android:id="#+id/view"
android:layout_below="#+id/dateAndTime"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
<ImageView
android:layout_width="30dp"
android:layout_height="20dp"
android:id="#+id/notesView"
android:clickable="true"
android:layout_marginTop="16dp"
android:layout_alignParentTop="true"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:src="#drawable/notes"/>
</RelativeLayout>
PLEASE HELP !!
This is most likely because R.id.notesView id is not present in your xml file R.layout.activity_previously_called_fragment. Please check the xml file or share it here.

How to Fix java.lang.ClassCastException: com.gc.materialdesign.views.ButtonFlat cannot be cast to android.widget.EditText

I am creating an android application that consists of registration form in dialog. Here, I placed a drop down spinner in registration dialog. But,here it was showing type casting error even i did the correct type casting. Please check it and make it solve. I am trying to solve since 2 hours. This is my code:
package com.example.testingsample;
import com.gc.materialdesign.views.ButtonFlat;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.app.Dialog;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.Toast;
public class MainActivity extends Activity
{
Button checking,spinner_check;
EditText username,password,confirm,seq_answer;
ButtonFlat clear,register;
Spinner spinner;
String[] questions = {
"What is your first name.?",
"What is your first pet name.?",
"What is your school name.?",
"What is your date of birth.?",
};
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
checking = (Button)findViewById(R.id.button1);
checking.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v)
{
android.app.Dialog registration_dialog = new android.app.Dialog(MainActivity.this);
registration_dialog.setContentView(R.layout.signup);
registration_dialog.setTitle("MedeQuip Registration");
username = (EditText)registration_dialog.findViewById(R.id.Dialog_edittext_username);
password = (EditText)registration_dialog.findViewById(R.id.Dialog_edittext_password);
confirm = (EditText)registration_dialog.findViewById(R.id.Dialog_edittext_confirmpassword);
spinner = (Spinner)registration_dialog.findViewById(R.id.spinner1);
ArrayAdapter adpter = new ArrayAdapter(MainActivity.this ,android.R.layout.simple_dropdown_item_1line,questions);
spinner.setAdapter(adpter);
spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parent,
View view, int position, long id)
{
Toast.makeText(MainActivity.this, questions[position], Toast.LENGTH_LONG).show();
}
#Override
public void onNothingSelected(AdapterView<?> parent) {
// TODO Auto-generated method stub
}
});
seq_answer = (EditText)registration_dialog.findViewById(R.id.editText1);
clear = (ButtonFlat)registration_dialog.findViewById(R.id.dialog_button_clear);
register = (ButtonFlat)registration_dialog.findViewById(R.id.Dialog_button_register);
//seq_answer = (EditText)registration_dialog.findViewById(R.id.editText1);*/
/*spinner_check.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v)
{
Toast.makeText(MainActivity.this, "Checking", Toast.LENGTH_LONG).show();
}
});*/
registration_dialog.show();
}
});
}}
This is my dialog xml:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/RelativeLayout1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center_vertical"
android:orientation="vertical" >
<EditText
android:id="#+id/Dialog_edittext_password"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="#+id/Dialog_edittext_username"
android:ems="10"
android:hint="Password"
android:inputType="textPassword" >
<requestFocus />
</EditText>
<EditText
android:id="#+id/Dialog_edittext_confirmpassword"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="#+id/Dialog_edittext_password"
android:ems="10"
android:hint="Confirm Password"
android:inputType="textPassword" />
<EditText
android:id="#+id/Dialog_edittext_username"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:ems="10"
android:hint="User Name" />
<Spinner
android:id="#+id/spinner1"
android:layout_width="345dp"
android:layout_height="60dp"
android:layout_below="#+id/Dialog_edittext_confirmpassword"
android:layout_centerHorizontal="true" />
<com.gc.materialdesign.views.ButtonFlat
android:id="#+id/Dialog_button_register"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignTop="#+id/dialog_button_clear"
android:text="Register"
android:textColor="#ffffff" >
</com.gc.materialdesign.views.ButtonFlat>
<com.gc.materialdesign.views.ButtonFlat
android:id="#+id/dialog_button_clear"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/editText1"
android:layout_marginRight="52dp"
android:layout_marginTop="59dp"
android:layout_toLeftOf="#+id/Dialog_button_register"
android:text="clear"
android:textColor="#ffffff" >
</com.gc.materialdesign.views.ButtonFlat>
<EditText
android:id="#+id/editText1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_below="#+id/spinner1"
android:layout_marginTop="67dp"
android:ems="10"
android:hint="Enter your answer for security question" />
</RelativeLayout>
Same problem i faced but i solved that problem with following solutions
-> If you are implementing in Eclipse once clean the project then test it.
-> If above also not works once restart your Eclipse.
Hope it will helps you.

How to add a ShowcaseView that pops up only for the first time to show how to use our app?

I wanted to do some thing like this as shown in image below,Is there any widget to do this or we need to add a view & make it transparent.
Thanks .
you must be create a custom view for it and make it transparent..
Main Activity:
import android.os.Bundle;
import android.os.Handler;
import android.app.Activity;
import android.app.Dialog;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.ColorDrawable;
import android.view.Menu;
import android.view.View;
import android.view.Window;
import android.view.View.OnClickListener;
import android.widget.RelativeLayout;
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initPopup();
}
#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;
}
public void initPopup() {
final Dialog view = new Dialog(this);
view.requestWindowFeature(Window.FEATURE_NO_TITLE);
view.getWindow().setBackgroundDrawable(
new ColorDrawable(android.graphics.Color.TRANSPARENT));
view.setContentView(R.layout.transparent_layout);
view.setCanceledOnTouchOutside(true);
view.show();
RelativeLayout rl_chooseone_menu_main = (RelativeLayout) view
.findViewById(R.id.rl_chooseone_menu_main);
rl_chooseone_menu_main.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
view.dismiss();
}
});
/*Handler handler = null;
handler = new Handler();
handler.postDelayed(new Runnable() {
public void run() {
view.cancel();
view.dismiss();
}
}, 3000);*/
}
}
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"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context=".MainActivity" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/hello_world" />
</RelativeLayout>
This is the Transparent View name is transparent_layout.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="#android:color/transparent" >
<RelativeLayout
android:id="#+id/rl_chooseone_menu_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginBottom="15dp"
android:layout_marginLeft="15dp"
android:layout_marginRight="15dp"
android:layout_marginTop="10dp" >
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_below="#+id/imageView1"
android:layout_marginRight="30dp"
android:text="Tap to view all" />
<ImageView
android:id="#+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:layout_marginTop="20dp"
android:src="#drawable/ic_launcher" />
<ImageView
android:id="#+id/imageView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_marginBottom="26dp"
android:src="#drawable/ic_launcher" />
<TextView
android:id="#+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="#+id/imageView2"
android:layout_alignRight="#+id/imageView2"
android:text="Tap to place your order" />
</RelativeLayout>
</RelativeLayout>
Enjoy Brother,i have used dimmy images,replace it with your own images..
You could make a tutorial Activity with a semi-transparent background, containing ImageViews and/or TextViews as shown in your example.
Tutorial activity's layout
<LinearLayout ...
android:background="#color/semitransparent"
... >
...
colors.xml
...
<color name="semitransparent">#80000000</color>
...
Then just start it on top of your usual Activity if you notice this is the first launch of your app by the user.

Fragment UI not getting displayed

Activity Layout : activity_text_entry.xml
<?xml version="1.0" encoding="utf-8" ?>
<FrameLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_height="match_parent"
android:layout_width="match_parent"
android:id="#+id/fragmentContainer_TextEntry"
/>
Fragment Layout : fragment_text_entry.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
>
<EditText
android:id="#+id/Text_Entry_Date"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="#string/choose_date_text"
android:layout_marginLeft="16dp"
android:layout_marginRight="16dp"
/>
<requestFocus />
<EditText
android:id="#+id/Text_Entry_Title"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="#string/choose_title_text"
android:layout_marginBottom="16dp"
android:layout_marginLeft="16dp"
android:layout_marginRight="16dp"
android:layout_marginTop="16dp" >
</EditText>
<EditText
android:id="#+id/Text_Entry_Content"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="16dp"
android:layout_marginLeft="16dp"
android:layout_marginRight="16dp"
android:layout_marginTop="16dp"
android:ems="100"
android:gravity="left|top"
android:hint="#string/enter_text_here"
android:inputType="textMultiLine" >
</EditText>
<LinearLayout
android:layout_height="match_parent"
android:layout_width="match_parent"
android:orientation="horizontal"
android:gravity="center|bottom"
>
<Button
android:id="#+id/Text_Entry_Button_Save"
android:text="#string/action_Save"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="10dp" />
<Button
android:id="#+id/Text_Entry_Button_Cancel"
android:text="#string/action_Cancel"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="10dp" />
</LinearLayout>
</LinearLayout>
Activity : TextEntryActivity
package com.app.journal_v001;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.widget.Toast;
public class TextEntryActivity extends FragmentActivity {
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_text_entry);
FragmentManager m_fm_text_entry_activity = getSupportFragmentManager();
Fragment m_f_text_entry_activity = m_fm_text_entry_activity.findFragmentById(R.id.fragmentContainer_TextEntry);
if(m_f_text_entry_activity == null)
{
m_f_text_entry_activity = new Fragment();
m_fm_text_entry_activity.beginTransaction()
.add(R.id.fragmentContainer_TextEntry, m_f_text_entry_activity)
.commit();
}
}
}
Fragment : TextEntryFragment
package com.app.journal_v001;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.Toast;
public class TextEntryFragment extends Fragment {
//private Button m_btn_Text_Entry_Save;
//private Button m_btn_Text_Entry_Cancel;
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
}
#Override
public View onCreateView(LayoutInflater inflate,ViewGroup parent,Bundle savedInstanceState)
{
View v = inflate.inflate(R.layout.fragment_text_entry, parent, false);
return v;
}
}
I am trying to render TextEntryFragment(fragment) from this TextEntryActivity(activity).
I have kept a series of toasts in various places to track and I can see my code is getting executed till FragmentManager's FragmentTransaction and after that it does not seem to call the Fragment's onCreate/onCreateView.
Can anyone point me whats wrong ?
When I execute I am not able to see the fragment UI being rendered.
remove
if(m_f_text_entry_activity == null)
{
m_f_text_entry_activity = new Fragment();
m_fm_text_entry_activity.beginTransaction()
.add(R.id.fragmentContainer_TextEntry, m_f_text_entry_activity)
.commit();
}
and replace with
m_fm_text_entry_activity.beginTransaction()
.add(R.id.fragmentContainer_TextEntry, m_f_text_entry_activity)
.commit();
==================EDIT==================================
Also
TextEntryFragment m_f_text_entry_activity=new TextEntryFragment();
instead of
Fragment m_f_text_entry_activity = m_fm_text_entry_activity.findFragmentById(R.id.fragmentContainer_TextEntry);

How to write method for listView with android:Onclick="listViewMetod" in its layout?

I am trying to create 5 buttons in my main activity and am trying to put new activities in them.
In the fifth button, I am having a problem with listView.
In the below code, am I missing anything required for the listView(View v) method to work properly?
Here is my code:
listActivity.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<ListView
android:id="#+id/listView1"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
</ListView>
</LinearLayout>
main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="#string/hello" />
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="newActivity"
android:text="Yeni Aktivite" />
<Button
android:id="#+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="toast"
android:text="Toast" />
<Button
android:id="#+id/button3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="web"
android:text="Web" />
<Button
android:id="#+id/button4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="preferences"
android:text="Preferences" />
<Button
android:id="#+id/button5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="listVieww"
android:text="ListView" />
</LinearLayout>
Main activity
package com.uygulamalar.odev;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
public class OdevKarmaActivity extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
public void newActivity(View v) {
Intent intent = new Intent(getApplicationContext(), NewActivity.class);
startActivity(intent);
}
public void toast(View v) {
Toast toast=Toast.makeText(getApplicationContext(), "vazzupp", 9999);
toast.show();
}
public void listVieww(View v) {
ListView listView1;
String jedigiller[] = {"pelin","figen","aylin","gizem"};
listView1=(ListView) findViewById(R.id.listView1);
listView1.setAdapter(new
ArrayAdapter<String>(this,android.R.layout.simple_list_item_1 , jedigiller));
}
}
When run, the code forces the Android emulator to close.
I am suspecting that the issue is related to the listView(View v) method, did I miss anything when using it?
You are attempting to reference a ListView found in listActivity.xml, while you have set your content view to main.xml.
Either copy the ListView over to main.xml, or start a new Activity in listVieww() that displays the list, and uses setContentView(R.layout.listActivity);

Categories

Resources