I am following a tutorial on making list views, but I cannot find the cause of the error
mainlistview1 cannot be resolved or is not a field.
Here is my current code:
import android.support.v7.app.ActionBarActivity;
import android.support.v7.app.ActionBar;
import android.support.v4.app.Fragment;
import android.app.Activity;
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.ArrayAdapter;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.os.Build;
import android.widget.AdapterView.*;
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.fragment_main);
String[] riverCities = new String[] {"Greenville", "Vicksburg",
"Memphis", "Natchez", "Cape Girardeau", "Helena"};
ListView listView = (ListView)findViewById(R.id.mainListView1);
ListAdapter adapter = new ArrayAdapter<String>(this, android.R.id.mainListView1, riverCities);
listView.setAdapter(adapter);
}
The layout
<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:orientation="horizontal" >
<ListView
android:id="#+id/mainListView1"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</ListView>
</LinearLayout>
problem:
android.R.id.mainListView1
The mainListView1 is not located in the android default layout id thus the compiler cannot find it.
solution:
Change it to the default list view item id
ListAdapter adapter = new ArrayAdapter<String>(this ,android.R.layout.simple_list_item_1, riverCities);
Related
In my Android App, I want to play set of some songs. Here with the help of my code I can play songs by clicking individually. But I can't make play the next song continuously once the song gets finished using Media Player. And also here I can't make a shuffle song also. Here Looping a same song only possible. But my requirement is to play the next song automatically once the firstly played song gets finished. Here is my code
XML Code:
<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout 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:id="#+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:openDrawer="start">
<ScrollView
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="2"
android:fillViewport="true">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:layout_marginTop="65dp"
android:padding="10dp"
android:layout_marginRight="5dp"
android:layout_marginLeft="5dp">
<ListView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true"
android:id="#+id/listView"/>
</LinearLayout>
</ScrollView>
<include
layout="#layout/app_bar_homepage"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<android.support.design.widget.NavigationView
android:id="#+id/nav_view"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="start"
android:fitsSystemWindows="true"
app:headerLayout="#layout/nav_header_homepage"
app:menu="#menu/activity_homepage_drawer" />
</android.support.v4.widget.DrawerLayout>
Java Code:
package com.musicapp;
import android.content.Intent;
import android.graphics.Color;
import android.media.MediaPlayer;
import android.net.Uri;
import android.os.Bundle;
import android.provider.MediaStore;
import android.support.constraint.Group;
import android.support.v4.view.GravityCompat;
import android.support.v7.app.ActionBarDrawerToggle;
import android.view.MenuItem;
import android.support.design.widget.NavigationView;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.Toast;
import java.io.IOException;
import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Random;
public class MusicActivity extends AppCompatActivity
implements NavigationView.OnNavigationItemSelectedListener {
ListView listView;
List<String> list;
ListAdapter adapter;
MediaPlayer mediaPlayer;
private int currentSongIndex = 0;
View previousSelectedItem;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_music);
listView = findViewById(R.id.listView);
list=new ArrayList<>();
Field[] fields = R.raw.class.getFields();
for(int i=0;i<fields.length;i++){
list.add(fields[i].getName());
}
adapter=new ArrayAdapter<>(this,android.R.layout.simple_list_item_1,list);
listView.setAdapter(adapter);
listView.setPadding(0,20,0,0);
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
if(mediaPlayer!=null){
mediaPlayer.release();
}
int resID = getResources().getIdentifier(list.get(i),"raw",getPackageName());
mediaPlayer = MediaPlayer.create(MusicActivity.this,resID);
mediaPlayer.start();
if (previousSelectedItem!=null) {
previousSelectedItem.setBackgroundColor(Color.WHITE);
}
previousSelectedItem=view;
view.setBackgroundResource(R.drawable.bg_with_color);
}
});
}
Could anybody help me?
Main Activity layout
<?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="wrap_content"
tools:context=".MainActivity">
<android.support.v7.widget.Toolbar
android:layout_width="match_parent"
android:layout_height="?android:attr/actionBarSize"
app:titleTextColor="#color/white"
android:background="#color/colorPrimary"
android:id="#+id/ToolbarMain"
tools:targetApi="honeycomb">
</android.support.v7.widget.Toolbar>
<ListView
android:id="#+id/lvMain"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginLeft="10dp"
android:layout_marginBottom="10dp"
android:layout_marginRight="10dp"
android:layout_marginTop="10dp"
android:dividerHeight="10dp"
android:divider="#null"
android:layout_below="#+id/ToolbarMain">
</ListView>
</RelativeLayout>
main activity.java
package com.example.admin.ttabledemo;
import android.content.Context;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.Toolbar;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
private Toolbar toolbar;
private ListView listview;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
setupIViews();
iniToolbar();
}
private void setupIViews(){
toolbar = (android.support.v7.widget.Toolbar) findViewById(R.id.ToolbarMain);
listview = (ListView)findViewById(R.id.lvMain);
}
private void iniToolbar(){
setSupportActionBar(toolbar);
getSupportActionBar().setTitle("TIMETABLE APP");
}
I am trying to build a time table app. List is not displaying. Just the titlebar.
Although in design listview is visible. please help.
Nothing is showing on the screen. I can't figure out what is wrong.
This is my first attempt to build an app. I am a complete beginner.
Use setAdapter() like this way.
String data[] = new String[]{"Most Popular", "UpComing", "Top Rated"};
ListView listview = (ListView) findViewById(R.id.lvMain);
YourAdapterName cma = new YourAdapterName(this, android.R.layout.simple_list_item_1, data);
listview .setAdapter(cma);
You have to add something to show. Try the following code:
String items[]={"one","two","three","four","five"};
ArrayAdapter<String> arrayAdapter=new ArrayAdapter<>(this, android.R.layout.simple_list_item_1, items);
listview.setAdapter(arrayAdapter);
Without the button code it's working fine.
I didn't see anything in the LogCat.
This is the MainActivity.java code:
package com.test.webservertest;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.speech.tts.TextToSpeech;
import android.speech.tts.TextToSpeech.OnInitListener;
import android.support.v7.app.ActionBarActivity;
import android.util.AndroidRuntimeException;
import android.util.Xml;
import android.view.Menu;
import android.view.MenuItem;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
import android.content.Context;
import android.view.View.OnClickListener;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.util.EncodingUtils;
import org.json.JSONObject;
import org.json.JSONTokener;
import java.io.DataOutputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.nio.charset.Charset;
import java.util.Arrays;
import java.util.Locale;
import java.net.URLConnection;
import java.net.HttpURLConnection;
import java.io.*;
import java.util.concurrent.Executor;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.logging.Logger;
import android.widget.Button;
public class MainActivity extends ActionBarActivity
{
private static final int MY_DATA_CHECK_CODE = 0;
public static MainActivity currentActivity;
TextToSpeech mTts;
private String targetURL;
private String urlParameters;
private Button btnClick;
private String clicking = "clicked";
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
setContentView(new SingleTouchEventView(this, null));
addListenerOnButton();
currentActivity = this;
initTTS();
}
public void addListenerOnButton() {
btnClick = (Button) findViewById(R.id.checkipbutton);
btnClick.setOnClickListener(new OnClickListener()
{
#Override
public void onClick(View arg0)
{
Intent browserIntent =
new Intent(Intent.ACTION_VIEW, Uri.parse("http://www.mkyong.com"));
startActivity(browserIntent);
}
});
}
And in the layout:
<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:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
android:paddingBottom="#dimen/activity_vertical_margin"
tools:context=".MainActivity">
<TextView
android:text="WEBSERVER"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true"
android:textSize="50dp"
android:textColor="#FFF"
android:id="#+id/textView"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Check Ip"
android:id="#+id/checkipbutton"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true"/>
</RelativeLayout>
If i'm calling the method: addListenerOnButton(); i'm getting message on the android: unfortunately has stopped and then the program shut down.
If i'm not calling addListenerOnButton(); then the program will keep working fine.
Also when running the program if not calling the addListenerOnButton(); i don't see the button i added in the layout designer on the screen at all.
you set the contentview twice cancelling the first with the layout of the button in it
if you comment out the second call to setContentView it may work
setContentView(R.layout.activity_main);
//setContentView(new SingleTouchEventView(this, null));
if you wish to use
SingleTouchEventView
consider adding it to your activity_main layout
I ve been searching for hours and I can't figure out what I'm doing wrong.
"list cannot be resolved or is not a field"
here is my search.java file:
package com.example.testxxx;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.json.JSONException;
import org.json.JSONObject;
import android.app.ListActivity;
import android.app.SearchManager;
import android.content.Intent;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
public class Search extends ListActivity {
private TextView mTextView;
private ListView mListView;
private String query;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.search);
mTextView = (TextView) findViewById(R.id.text);
mListView = (ListView) findViewById(R.id.list);
}
}
and here is my search.xml layout:
<?xml version="1.0" encoding="utf-8"?>
<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=".Search" >
<TextView
android:id="#+id/text"
android:textColor="?android:textColorPrimary"
android:background="#android:drawable/title_bar"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
<ListView
android:id="#android:id/list"
android:layout_width="fill_parent"
android:layout_height="0dp" />
</RelativeLayout>
I have noticed that if i change
android:id="#android:id/list"
to
android:id="#android:id/list"
then it will find it, but if i do so, i will have another error because I am extending listactivity that need the listview to have the ID set this way (android:id="#android:id/list")
Do you have a solution? Thank you!
Just replace
mListView = (ListView) findViewById(R.id.list);
with
mListView = (ListView) findViewById(android.R.id.list);
or simply you can call
mListView = getListView()
as you are extending ListActivity and your xml layout contains a ListView with id android.R.id.list
when you referencing to internal ID's (ID's may be drawable ID's, String ID's or any resource) then those ID's will not be generated in your R.java. you have to import those ID's from where you referenced.
I am making an android launcher, and i need to know how i can load all the apps into a gridview. I want system stuff like calculator and all of that to show up as well. How can i do that? I have search for a long time now without any solution. Please help me and thanks very much!
Java code:
package com.mysoftware.mysoftwareos.launcher;
import java.util.List;
import android.app.Activity;
import android.app.ActivityManager;
import android.content.Context;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.GridView;
import android.widget.LinearLayout;
public class AllAppsActivity extends Activity {
LinearLayout allappsLayout;
GridView allappsGridView;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.allapps_screen);
//Import views
allappsLayout = (LinearLayout)findViewById(R.id.allappsLayout);
allappsGridView = (GridView)findViewById(R.id.allappsGridView);
//Setup animation for the main layout
Animation a = AnimationUtils.loadAnimation(this, R.anim.fadeout);
a.reset();
allappsLayout.clearAnimation();
allappsLayout.startAnimation(a);
//Load all apps into the apptray
}
}
My xml layout:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/allappsLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#drawable/wallpaper"
android:orientation="vertical" >
<GridView
android:id="#+id/allappsGridView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:numColumns="3" >
</GridView>
</LinearLayout>
ADW.Launcher is an open source android launcher...
browse around: http://code.google.com/p/adw-launcher-android/