Problem regarding extending ListActivity - java

In my project whenever I extend activity it works fine but as soon as I extend ListActivity it throws exception and shows file not found. Why is that? We already know that ListActivity itself extends the Activity class. The application must run fine.
Here's the java file:
package com.android.feedGrabber;
import java.net.URL;
import java.net.URLConnection;
import java.util.Collection;
import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;
import com.sun.cnpi.rss.elements.Item;
import com.sun.cnpi.rss.elements.Rss;
import com.sun.cnpi.rss.parser.RssParser;
import com.sun.cnpi.rss.parser.RssParserFactory;
public class feedGrabber extends Activity {
public static Collection<Item> readRSSDocument(String url) throws Exception {
RssParser parser = RssParserFactory.createDefault();
URL feedUrl = new URL(url);
URLConnection urlc=feedUrl.openConnection();
urlc.setRequestProperty("User-Agent","");
urlc.connect();
Rss rss = parser.parse(urlc.getInputStream());
return rss.getChannel().getItems();
}
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
int links=0;
try
{
for(Item item : feedGrabber.readRSSDocument("http://feeds.feedburner.com/Tutorialzine")){
if(links++>3)
break;
TextView t = (TextView)findViewById(R.id.title);
t.append("Title: ");
t.append(item.getTitle().toString());
t.append("\n\n");
//System.out.println("Title: " + item.getTitle());
//System.out.println("Link: " + item.getLink());
}
}catch(Exception e)
{
//
}
}
}
If I change "extends Activity" to "extends ListActivity" it gives the exception. I need to bring this change because I want to implement it on ListView instead of TextView.
Error it throws:
debug mode gets active and it highlights under Suspended(exception RunTimeException): ActivityThread.performLaunchActivity(ActivityThread$ActivityRecord, Intent) line: 2663.

When you extend a ListActivity, it is expecting the Layout to have a ListView that has an id of "#android:id/list", if you do not have it, it will throw an exception
You layout should look something like this:
<?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="#android:id/list"
android:layout_width="fill_parent"
android:layout_height="wrap_content"/>
<TextView android:id="#android:id/empty"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/noresults"/>
</LinearLayout>

Without seeing your main.xml or the exception I can't be sure, but I'd suggest checking if your list has the #android:id/list identity as required.

Related

Making a textview global

I am wondering if there is any way that you can make a textview visible over every screen so that I don't have to individually create a textview on every activity.
I want to do this so I can display my application version on every screen for debugging.
Thank you.
Yes! it is possible.
See you have to add this code.
<include layout="#layout/layout_version"/>
and
public class MainActivity extends BaseActivity {
}
And you will find version text.
Just add this few code.
(1) Create a layout that will represent version text.
layout_version.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content" // or match_parent (according to your need)
android:layout_height="wrap_content">
<TextView
android:id="#+id/tvVersion"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
... other properties
/>
<!--... other component if required-->
</LinearLayout>
(2) Include this to your activity layout like activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
...>
<include layout="#layout/layout_version"/>
</LinearLayout>
(3) Create a BaseActivity.class or add my code if you have already.
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.widget.TextView;
import com.innovanathinklabs.sample.BuildConfig;
import com.innovanathinklabs.sample.R;
public class BaseActivity extends AppCompatActivity {
#Override
protected void onCreate(#Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
TextView tvVersion = findViewById(R.id.tvVersion);
if (tvVersion != null) tvVersion.setText(BuildConfig.VERSION_NAME);
}
}
(4) Final step is to extend all your activity by BaseActivity.class.
public class MainActivity extends BaseActivity {
}
That's all!

How to put GLSurfaceView inside a layout on android

I would like to create an app where the glsurfaceview is inside a relative layout together with a listview. While coding, my IDE shows no errors, but once I run the application it just crashes.
main.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="center"
android:orientation="horizontal">
<ListView
android:layout_height="wrap_content"
android:layout_width="100dp"
android:background="#6D6D6D"
android:id="#+id/listObjects"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:layout_alignParentEnd="true"
android:layout_centerHorizontal="false"/>
<com.shirofuji.thrrededit.GL_handler
android:layout_height="match_parent"
android:layout_width="wrap_content"
android:orientation="horizontal"
android:id="#+id/gl_layout"
android:layout_alignParentLeft="true"
android:layout_alignEnd="#id/listObjects"/>
</RelativeLayout>
MainActivity
package com.shirofuji.thrrededit;
import android.app.*;
import android.os.*;
import android.view.*;
import android.widget.*;
import android.opengl.*;
public class MainActivity extends Activity
{
GL_handler gl_maineditor;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
gl_maineditor = (GL_handler)findViewById(R.id.gl_layout);
//layout_3d.addView(gl_maineditor);
setContentView(R.layout.main);
}
#Override
public void onResume(){
super.onResume();
gl_maineditor.onResume();
}
#Override
public void onPause(){
super.onPause();
gl_maineditor.onPause();
}
}
GL_handler
package com.shirofuji.thrrededit;
import android.opengl.*;
import android.content.Context;
import android.view.View;
import android.util.*;
public class GL_handler extends View
{
GLSurfaceView mainsurface;
GL_renderer scene_renderer;
public GL_handler(Context ctx,AttributeSet attr){
super(ctx,attr);
mainsurface = new GLSurfaceView(ctx);
scene_renderer = new GL_renderer();
mainsurface.setRenderer(scene_renderer);
}
public void onResume(){
mainsurface.onResume();
}
public void onPause(){
mainsurface.onPause();
}
}
The order of calls in the onCreate() method of the activity is wrong:
gl_maineditor = (GL_handler)findViewById(R.id.gl_layout);
setContentView(R.layout.main);
setContentView() needs to be called before you can get any view with findByViewId(). The sequence should be:
setContentView(R.layout.main);
gl_maineditor = (GL_handler)findViewById(R.id.gl_layout);
With that out of the way, I think there's another aspect where you might be on the wrong track. Unless you have a master plan that is just not visible from the posted code. You're not placing a GLSurfaceView in your layout. The GL_handler you place in the layout derives from a plain View. It has a GLSurfaceView as a member variable, but that alone will not make the GLSurfaceView appear.
Unless you have a good reason, it should be much easier to derive your GL_handler class from GLSurfaceView. This will actually result in a GLSurfaceView that becomes part of your view hierarchy when the layout is inflated.

App starts normally But crashes when I click Button "oneplus"

First of all let me tell you that I am a beginner at this... I started to try developing apps about a week ago.
When I start my app on a real device it starts normally But when I click the button "oneplus" it says that app isn't responding...
this app adds one number to the number in TextView .Earlier I faced same problem and thought it was because of fragments.So i removed them and made a very simple app But still the problem persists
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: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:layout_width="match_parent"
android:layout_height="80dp"
android:text="0"
android:id="#+id/num"
android:textSize="70sp"
/>
<Button
android:layout_width="match_parent"
android:layout_height="80dp"
android:layout_marginTop="100dp"
android:text=" + 1 "
android:textSize="50sp"
android:id="#+id/plusone"
android:onClick="onClick"
/>
</RelativeLayout>`
mainactivity.java
package com.example.tjain.oneup;
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
public class MainActivity extends Activity implements View.OnClickListener {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
#Override
public void onClick(View v) {
final TextView number = (TextView) findViewById(R.id.num);
int original = Integer.parseInt(number.getText().toString());
int newnum = original + 1;
number.setText(newnum);
}
}
I am facing this error in other apps too. Maybe I am repeating my mistake . Please point it out. Thanks in advance
As I doubt for two scenario in your onClick for Exception
NumberFormat Exception
ResourceNotFound Exception
For First,
try
{
int original = Integer.parseInt(number.getText().toString());
}catch(Exception ex)
{
ex.printStacktrace();
}
For Second,
Because int is not a argument for TextView's setText (only if its not a part of String resource id), as it will find with resource id not accepting as String value. and obvious Resource Not Found exception
you have to convert int to String,
number.setText(String.valueOf(newnum));
What I would recommend doing is adding your UI elements at the class level of your activity so you have one TextView object and one Button object, that way you only have to search for the resource once and you can add a click listener to the button itself like this:
public class MainActivity extends Activity{
private TextView mNumberTextView;
private Button mOnePlusButton;
#Override
protected void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mNumberTextView = findViewbyId(R.id.num);
mOnePlusButton = findViewbyId(R.id.plusone);
mOnePlusButton.setOnClickListener(new OnClickListener(){
#Override
public void onClick(View v){
int original = Integer.parseInt(mNumberTextView.getText().toString());
mNumberTextView.setText(original + 1);
}
});
}
}
Edit: Since you're still new at this, it's always a good idea to check the documentation.
You should use
number.setText(+newnum);
instead
number.setText(newnum);

Android WebView in Fragment

I know the following is probably simple, but I can't seem to get it to work. It compile and runs, but doesn't load any URL I throw at it. I have Internet set in permisions, even checked to make sure it was running in some other code.
package com.richardmather.autoaccidentattorney;
import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.webkit.WebView;
public class FindHospitalFragment extends Fragment {
public FindHospitalFragment(){}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_find_hospital, container, false);
final WebView webView = (WebView) rootView.findViewById(R.id.webView);
String searchURL = "http://www.stackoverflow.com";
webView.getSettings().setJavaScriptEnabled(true);
webView.loadUrl("searchURL");
return rootView;
}
}
Here is the 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">
<WebView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/webView"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
</LinearLayout>
I know it's something simple I'm missing.
EDIT: Took out the quotation marks, now it's loading the URL in Google Chrome....
The problem probably is the line webView.loadURL("searchURL");.
By doing that it actually tries to load the URL searchURL and not the URL http://www.stackoverflow.com you saved in the variable searchURL.
So just remove the "s:
webView.loadURL(searchURL); // searchURL = "http://www.stackoverflow.com"
Edit:
To not launch chrome, simply add the following line:
webview.setWebViewClient(new WebViewClient());
Also see this question for details.

Eclipse / Android error

So I'm getting 3 errors "final int sortColumnIndex = c.getColumnIndex(YOUR_SORT_COLUMN_NAME);" & "public class MyAdapter extends CursorAdapter implements SectionIndexer" <---this one is getting marked as an error twice.
My MainActivityNext.java
package testing.android.application.three;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Set;
import android.os.Bundle;
import android.support.v4.widget.CursorAdapter;
import android.widget.AlphabetIndexer;
import android.widget.ArrayAdapter;
import android.widget.SectionIndexer;
import android.app.Activity;
import android.app.ListActivity;
import android.content.Context;
import android.database.Cursor;
public class MainActivityNext extends ListActivity{
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main_activity_next);
}
public class MyAdapter extends CursorAdapter implements SectionIndexer
{
// All valid characters. May want to include numbers, etc if they show up
// in your sort column
private final static String ALPHABET = "abcdefghijklmnopqrstuvwxyz";
private AlphabetIndexer mIndexer = null;
public MyAdapter(Context context, Cursor c, int flags)
{
super(context, c, flags);
// Assumes your cursor is non-null,
// otherwise do this in swapCursor if mIndexer==null
final int sortColumnIndex = c.getColumnIndex(YOUR_SORT_COLUMN_NAME);
mIndexer = new AlphabetIndexer(c, sortColumnIndex,
ALPHABET);
}
public Cursor swapCursor(Cursor newCursor)
{
super.swapCursor(newCursor);
// Make sure the AlphabetIndexer knows about the new Cursor
mIndexer.setCursor(newCursor);
return newCursor;
}
public int getPositionForSection(int section)
{
// AlphabetIndexer does all the hard work
return mIndexer.getPositionForSection(section);
}
public int getSectionForPosition(int position)
{
// AlphabetIndexer does all the hard work
return mIndexer.getSectionForPosition(position);
}
public Object[] getSections()
{
// AlphabetIndexer does all the hard work
return mIndexer.getSections();
}
}
}
According to your Logcat:
Unable to start activity ComponentInfo{testing.android.application.three/testing.android.application.three.MainActivityNext}:
java.lang.RuntimeException: Your content must have a ListView whose id
attribute is 'android.R.id.list'
Whenever you use or extend a listactivity the view you build for it must have a listview with that id.
<?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"
android:paddingLeft="8dp"
android:paddingRight="8dp">
<ListView android:id="#android:id/list"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#00FF00"
android:layout_weight="1"
android:drawSelectorOnTop="false"/>
<TextView android:id="#android:id/empty"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#FF0000"
android:text="No data"/>
</LinearLayout>
See http://developer.android.com/reference/android/app/ListActivity.html for more info on it.
as in log:
our content must have a ListView whose id attribute is
'android.R.id.list'
means you will need to use #id/android:list id in xml for ListView. do it as:
<ListView android:id="#id/android:list"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
..../>
If your activity extends the ListActivity then you have to set the id of the xml of your ListView like below
< ListView
android:id="#android:id/list"
...

Categories