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
Related
What i'm doing wrong, i have checked few times IDs all is ok in XML but this error comes when i want to build release app, when i'm build debug app then i don't have this error.
error: cannot find symbol
notifications = findViewById(R.id.notification_text);
^
symbol: variable notification_text
MainActivity
public class MainActivity extends AppCompatActivity {
TextView notifications;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
bottomNavigation = findViewById(R.id.bottom_navigation);
bottomNavigation.setOnNavigationItemSelectedListener(navigationItemSelectedListener);
notifications = findViewById(R.id.notification_text);
}
}
activity_main.xml
<androidx.constraintlayout.widget.ConstraintLayout 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/relativeLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="false"
android:windowTranslucentNavigation="false"
tools:context=".MainActivity">
<androidx.constraintlayout.widget.ConstraintLayout
android:id="#+id/BottomLayout"
android:layout_width="0dp"
android:layout_height="wrap_content"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/swipeRefresh">
<com.google.android.material.bottomnavigation.BottomNavigationView
android:id="#+id/bottom_navigation"
android:layout_width="wrap_content"
android:layout_height="50dp"
android:background="#drawable/border_top_bottom_menu"
android:clipToPadding="false"
android:foreground="#android:color/transparent"
android:foregroundTint="#android:color/transparent"
android:isScrollContainer="true"
android:theme="#style/bottom_navigation_menu"
android:visibility="visible"
app:barrierAllowsGoneWidgets="false"
app:elevation="0dp"
app:itemHorizontalTranslationEnabled="false"
app:itemIconSize="22dp"
app:itemIconTint="#color/drawer_item"
app:itemTextColor="#color/drawer_item"
app:labelVisibilityMode="labeled"
app:layout_constraintBottom_toTopOf="#id/notification_text"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:menu="#menu/bottom_navigation_menu">
</com.google.android.material.bottomnavigation.BottomNavigationView>
<TextView
android:id="#+id/notification_text"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_gravity="center"
android:background="#color/notification_offline"
android:fontFamily="#font/roboto"
android:gravity="center"
android:textAlignment="center"
android:textColor="#FFFFFF"
android:textIsSelectable="false"
android:textSize="12sp"
android:textStyle="normal"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/bottom_navigation" />
</androidx.constraintlayout.widget.ConstraintLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
Also app is working good on emulator i don't have any errors and also notification_text is working as i expect. Problem is only when i try to build release app.
Edit: Imports
package com.android.app_name;
import android.animation.ValueAnimator;
import android.annotation.SuppressLint;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.res.Configuration;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import androidx.annotation.IdRes;
import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.app.AppCompatDelegate;
import androidx.core.content.ContextCompat;
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.graphics.drawable.TransitionDrawable;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Looper;
import android.os.StrictMode;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.animation.DecelerateInterpolator;
import android.webkit.CookieManager;
import android.webkit.JavascriptInterface;
import android.webkit.WebChromeClient;
import android.webkit.WebResourceError;
import android.webkit.WebResourceRequest;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.Button;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ProgressBar;
import android.widget.TextView;
import android.widget.Toast;
import android.os.Handler;
import com.androidstudy.networkmanager.Monitor;
import com.androidstudy.networkmanager.Tovuti;
import com.github.javiersantos.appupdater.enums.UpdateFrom;
import com.github.javiersantos.appupdater.enums.Display;
import com.github.javiersantos.appupdater.AppUpdater;
import com.google.android.material.bottomnavigation.BottomNavigationItemView;
import com.google.android.material.bottomnavigation.BottomNavigationView;
import android.view.MenuItem;
import androidx.annotation.NonNull;
import android.content.Intent;
import androidx.preference.PreferenceManager;
import android.content.SharedPreferences;
import android.net.Uri;
import android.webkit.ValueCallback;
import android.os.Build;
import android.content.ActivityNotFoundException;
import android.app.DownloadManager;
import android.os.Environment;
import android.content.pm.PackageManager;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
I can't find your import to R in your imports. try cleaning your project and then rebuilding it.You might also be importing R from the wrong package somehow. It has happened to me before that another import has an R package and thus I don't get the error of no package imported. Re-check your imports, maybe slect all your imports do cmd+x (MACOS)/ctrl+x (WIN), then import first the R package and then ctrl+v(WIN)/cmd+v(MACOS) your imports. That could also solve it. Let me know if it works!
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?
I am using google image search to look for images and get the URL of the first image in my android application using JSOUP library
My problem is that no matter how much I try It shows me that the URL is null
looks like the element that contains the first image does not have a url
here is the XML file:
<?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:id="#+id/activity_main"
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="arb.myapplication.MainActivity">
<ImageView
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:srcCompat="#mipmap/ic_launcher"
android:layout_alignParentBottom="true"
android:layout_marginBottom="131dp"
android:id="#+id/imageView" />
<Button
android:text="Button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:layout_marginRight="39dp"
android:layout_marginEnd="39dp"
android:layout_marginTop="11dp"
android:id="#+id/button3"
android:elevation="0dp" />
Ignore the image view
I use the button to get the url of the image
here is the JAVA file
package arb.myapplication;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.drawable.BitmapDrawable;
import android.os.AsyncTask;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.Toast;
import org.jsoup.Connection;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Attribute;
import org.jsoup.nodes.Attributes;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
public class MainActivity extends AppCompatActivity {
class DownloadIcon extends AsyncTask<Object, Object, Integer>
{
#Override
protected Integer doInBackground(Object... objects) {
Connection connecion= Jsoup.connect("http://www.google.com/search?tbm=isch&q=facebook+logo");
try {
Document document=connecion.get();
Elements element=document.select("img.rg_ic.rg_i");
String url=element.first().absUrl("src");
publishProgress(url);
} catch (IOException e1) {
publishProgress(-1);
}
return 0;
}
#Override
protected void onProgressUpdate(Object... values) {
/* ImageView imageView=(ImageView) findViewById(R.id.imageView);
try {
InputStream inputStream= new URL(values[0].toString()).openStream();
imageView.setImageBitmap(BitmapFactory.decodeStream(inputStream));
} catch (IOException e) {
throw new RuntimeException(e);
}*/
Toast.makeText(getBaseContext(),values[0].toString(),Toast.LENGTH_LONG).show();
}
#Override
protected void onPostExecute(Integer integer) {
Toast.makeText(getBaseContext(),"done",Toast.LENGTH_SHORT).show();
}
}
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button button3=(Button) findViewById(R.id.button3);
button3.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
new DownloadIcon().execute();
}
});
}
}
in the onProgressUpdate function does not show the Toast because its text is null
I already tried to get the attribute keys and values of the chosen element
It matches those displayed when I parse the element using the Chrome explorer but without src attribute
The text is null because you get no images. If you debug your request you'll see 403 status code which tells you that Jsoup client is not authorized to fetch images from Google. You need to use or emulate browser to achieve your goal.
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);
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.