OK, I'm working on the chapter 9 tutorial in Android Boot Camp and I'm having...actually a few issues. The book was written for the older versions of Android Studio but my class is using the latest version. I've done my best to look up tutorials for the latest version but they've become quite rare.
Chapter 9 covers a Master/Detail flow tutorial that some things have worked in and others have not.
Where I stand now is a TextView/WebView issue.
I tried simply converting the WebView to match TextView but then .loadUrl won't work and when I use WebView I get an "unexpected cast error. Layout tag was TextView." And Android studio won't tell me where the layout tag was declared so I'm currently combing through all the files line by line. I'm not certain if this source layout tag is in an .xml, a .java or if I should be looking in the manifest.
I believe this means I have to change the source layout to WebView though I can't find anything in the chapter itself about it except ensuring I have the correct import.
package com.example.bikeandbarge;
import android.app.Activity;
import android.support.design.widget.CollapsingToolbarLayout;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.webkit.WebView;
import android.widget.TextView;
import com.example.bikeandbarge.dummy.DummyContent;
public class ItemDetailFragment extends Fragment {
public static final String ARG_ITEM_ID = "item_id";
private DummyContent.DummyItem mItem;
public ItemDetailFragment() {
}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (getArguments().containsKey(ARG_ITEM_ID)) {
// Load the dummy content specified by the fragment
// arguments. In a real-world scenario, use a Loader
// to load content from a content provider.
mItem = DummyContent.ITEM_MAP.get(getArguments().getString(ARG_ITEM_ID));
Activity activity = this.getActivity();
CollapsingToolbarLayout appBarLayout = (CollapsingToolbarLayout) activity.findViewById(R.id.toolbar_layout);
if (appBarLayout != null) {
appBarLayout.setTitle(mItem.content);
}
}
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.item_detail, container, false);
// Show the dummy content as text in a TextView.
if (mItem.id.equals("1")) {
rootView = inflater.inflate(R.layout.photos, container, false);
}
if (mItem.id.equals("2")) {
rootView = inflater.inflate(R.layout.tour, container, false);
}
// Can not get this to update to WebView-not certain where the layout tab is textView
//if (mItem.id.equals("3")) {
// ((WebView) rootView.findViewById( R.id.item_detail )).loadUrl( mItem.item_url ); }
//Can replace WebView with TextView but won't recognize .loadUrl without WebView
if (mItem.id.equals("3")) {
((WebView) rootView.findViewById( R.id.item_detail )).loadUrl( mItem.item_url );
}
return rootView;
}
}
I would love for this to run with loadUrl actually working.
Neither WebView nor TextView will allow me to run the program. The apk file simply can't be compiled for me to even test it. I'd like to at least get to a point where I can compile the apk file and attempt to run it.
Open the layout file item_detail.xml(or is it fragment_item_detail.xml inn the book?) and change the <TextView>-element to <WebView>. Then the "Unexpected cast error" should go away.
In your webView and manifest add the following code:
AndroidManifest.xml
<application
....
android:usesCleartextTraffic="true"
android:hardwareAccelerated="true"
.....
>
In Your Layout where the webView is
<WebView
....
android:usesCleartextTraffic="true"
....
></WebView>
I am querying some raws from the database in Android studio. When I have the result table as a cursor, I want to show that cursor in an activity where columns are written at the top and rows are seen one under the other like a normal table. So, this activity's column names will change according to the result query's columns.
Any idea how to implement this, or is there a template I can use? I am new to Android so it might be an easy question for some of you, sorry for that.
So I recently have as well the same question. Then I found a really good tutorial with custom list items.
First, make sure that you save your records from the database into an object.
So first you have to create a row view. So create a simple XML file insert for example the following code and save it as myobject_list_item in your res/layout folder.
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/item"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:padding="5dp">
<TextView
android:id="#+id/column1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_toStartOf="#+id/column2"
android:padding="10dp"
android:paddingLeft="5dp"
android:text="Column1"
android:textAppearance="#android:style/TextAppearance.Material.Large"
android:textSize="20sp" />
<TextView
android:id="#+id/column2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="#+id/gewichtung"
android:layout_alignParentEnd="true"
android:layout_alignParentTop="true"
android:padding="10dp"
android:text="Column2"
android:textAppearance="#android:style/TextAppearance.Material.Large"
android:textSize="20sp" />
</RelativeLayout>
After that you have to create a custom list adapter. So create a new Java file with the name MyObject_ListAdapter and insert the following code:
package net.example.app;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.RelativeLayout;
import android.widget.TextView;
import net.example.app.R;
import java.util.ArrayList;
public class MyObject_ListAdapter extends ArrayAdapter<MyObject> {
// Source:
// http://hmkcode.com/android-custom-listview-items-row/
private ArrayList<MyObject> objects;
private Context context;
public MyObject_ListAdapter(Context context, ArrayList<MyObject> objects) {
super(context, R.layout.myobject_list_item, objects);
this.objects = objects;
this.context = context;
}
public View getView(int position, View convertView, ViewGroup parent) {
// 1. Create inflater
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
// 2. Get rowView from inflater
View rowView = inflater.inflate(R.layout.fach_list_item, parent, false);
// 3. Get the two text view from the rowView
TextView column1 = (TextView) rowView.findViewById(R.id.column1);
TextView column2 = (TextView) rowView.findViewById(R.id.column2);
RelativeLayout item = (RelativeLayout) rowView.findViewById(R.id.item);
// 4. Set the text for textView
column1.setText(objects.get(position).getName());
column2.setText(objects.get(position).getSecondName());
// 5. return rowView
return rowView;
}
}
Add in your activity now an simple ListView and add for this an id like lv.
Then in your Java Activity you can insert the following code:
package net.example.app;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import net.example.app.MyObject;
import net.example.app.MyObject_ListAdapter;
import java.util.ArrayList;
public class MyActivity extends AppCompatActivity {
private ArrayAdapter arrayAdapter;
private ArrayList<MyObject> myObjects = new ArrayList<>();
private SQLiteDatabase db;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.myactivity);
db = openOrCreateDatabase("database.db", MODE_PRIVATE, null);
ListView lv = (ListView) findViewById(R.id.lv);
arrayAdapter = new MyObject_ListAdapter(this, myObjects); //Define the custom list adapter with the activity and arraylist
lv.setAdapter(arrayAdapter); //Connect your listview with the adapter
displayData();
}
private void displayData() {
Cursor c = db.rawQuery("SELECT * FROM my_table", null);
while (c.moveToNext()) { //Loop through all the records
//Now on the variable 'c' there is one record.
int column_a_name = c.getColumnIndex("my_column1"); //Get the index of the column from your table.
String column_a_value = c.getString(column_a_name); //Get the value from the column from the current record.
int column_b_name = c.getColumnIndex("my_column2");
String column_b_value = c.getString(column_b_name);
//Now you can do with the value what you want.
myObjects.add(new MyObject(column_a_value, column_b_value));
}
arrayAdapter.notifyDataSetChanged(); //Notify, that you have changed some data in the array list.
}
}
I hope this tutorial may help you.
When you get a Cursor from a SQL execution, then you can use the following script:
Cursor c = db.rawQuery("SELECT * FROM my_table", null);
while (c.moveToNext()) { //Loop through all the records
//Now on the variable 'c' there is one record.
int column_a_name = c.getColumnIndex("my_column"); //Get the index of the column from your table.
String column_a_value = c.getString(column_a_name); //Get the value from the column from the current record.
//Now you can do with the value what you want.
System.out.println(column_a_value);
}
I hope this might be helpful for you.
Android has different layouts that could be of use for what you state.
You can use TableLayout of GridLayout. At this post you can find some discussion about which one choose: Grid Layout Vs. Table Layout
From Android docs, you can check some examples like: https://developer.android.com/guide/topics/ui/layout/grid.html
and https://developer.android.com/guide/topics/ui/layout/gridview.html
Some additional recommendation:
Use AsyncTask for interact with the database, don't do that from the UI thread.
I choose callback methods so, when the data access is completed, you inform the calling activity who is in charge of showing the data on the screen (using the layout of your choice).
Do not create big long classes doing everything. use one class for the activity, another for the adapter, another for the async task.
Hope this helps you.
I'm developing an Android app that has to simulate a sort of Pokédex.
For now, what I want to do is simply have all 151 Pokémon printed on my device, so I can scroll them up and down.
The problem is that when I try this thing with such as 9 or 12 images there are no problems, but when I load all the 151 images (all .png), Android kills the app because it's draining too much system resources.
I've heard that there are Java methods that can (don't know how) "destroy" an object when it goes out of the display and then recreate it when it returns in the screen. Anyway if you have different suggestions on how to resolve my problem, every idea is welcome!
Here is my MainActivity:
package com.example.thefe.newsmartkedex;
import android.media.AudioManager;
import android.media.SoundPool;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.GridView;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
GridView gridview = (GridView) findViewById(R.id.gridview);
gridview.setAdapter(new ImageAdapter(this));
gridview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View v,
int position, long id) {
Toast.makeText(MainActivity.this, "" + position,
Toast.LENGTH_SHORT).show();
}
});
};
}
And here is my ImageAdapter class I use for Gridview:
package com.example.thefe.newsmartkedex;
import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;
public class ImageAdapter extends BaseAdapter {
private Context mContext;
public ImageAdapter(Context c) {
mContext = c;
}
public int getCount() {
return mThumbIds.length;
}
public Object getItem(int position) {
return null;
}
public long getItemId(int position) {
return 0;
}
// create a new ImageView for each item referenced by the Adapter
public View getView(int position, View convertView, ViewGroup parent) {
ImageView imageView;
if (convertView == null) {
// if it's not recycled, initialize some attributes
imageView = new ImageView(mContext);
imageView.setLayoutParams(new GridView.LayoutParams(200, 200));
imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
imageView.setPadding(8, 8, 8, 8);
} else {
imageView = (ImageView) convertView;
}
imageView.setImageResource(mThumbIds[position]);
return imageView;
}
// references to our images
private Integer[] mThumbIds = {
R.drawable.pkmn1, R.drawable.pkmn2,
R.drawable.pkmn3, R.drawable.pkmn4,
R.drawable.pkmn5, R.drawable.pkmn6,
R.drawable.pkmn7, R.drawable.pkmn8,
R.drawable.pkmn9, R.drawable.pkmn10,
R.drawable.pkmn11, R.drawable.pkmn12,
R.drawable.pkmn13, R.drawable.pkmn14,
R.drawable.pkmn15, R.drawable.pkmn16,
R.drawable.pkmn17, R.drawable.pkmn18,
R.drawable.pkmn19, R.drawable.pkmn20,
R.drawable.pkmn21, R.drawable.pkmn22,
R.drawable.pkmn23, R.drawable.pkmn24,
R.drawable.pkmn25, R.drawable.pkmn26,
R.drawable.pkmn27, R.drawable.pkmn28,
R.drawable.pkmn29, R.drawable.pkmn30,
R.drawable.pkmn31, R.drawable.pkmn32,
R.drawable.pkmn33, R.drawable.pkmn34,
R.drawable.pkmn35, R.drawable.pkmn36,
R.drawable.pkmn37, R.drawable.pkmn38,
R.drawable.pkmn39, R.drawable.pkmn40,
R.drawable.pkmn41, R.drawable.pkmn42,
R.drawable.pkmn43, R.drawable.pkmn44,
R.drawable.pkmn45, R.drawable.pkmn46,
R.drawable.pkmn47, R.drawable.pkmn48,
R.drawable.pkmn49, R.drawable.pkmn50,
R.drawable.pkmn51, R.drawable.pkmn52,
R.drawable.pkmn53, R.drawable.pkmn54,
R.drawable.pkmn55, R.drawable.pkmn56,
R.drawable.pkmn57, R.drawable.pkmn58,
R.drawable.pkmn59, R.drawable.pkmn60,
R.drawable.pkmn61, R.drawable.pkmn62,
R.drawable.pkmn63, R.drawable.pkmn64,
R.drawable.pkmn65, R.drawable.pkmn66,
R.drawable.pkmn67, R.drawable.pkmn68,
R.drawable.pkmn69, R.drawable.pkmn70,
R.drawable.pkmn71, R.drawable.pkmn72,
R.drawable.pkmn73, R.drawable.pkmn74,
R.drawable.pkmn75, R.drawable.pkmn76,
R.drawable.pkmn77, R.drawable.pkmn78,
R.drawable.pkmn79, R.drawable.pkmn80,
R.drawable.pkmn81, R.drawable.pkmn82,
R.drawable.pkmn83, R.drawable.pkmn84,
R.drawable.pkmn85, R.drawable.pkmn86,
R.drawable.pkmn87, R.drawable.pkmn88,
R.drawable.pkmn89, R.drawable.pkmn90,
R.drawable.pkmn91, R.drawable.pkmn92,
R.drawable.pkmn93, R.drawable.pkmn94,
R.drawable.pkmn95, R.drawable.pkmn96,
R.drawable.pkmn97, R.drawable.pkmn98,
R.drawable.pkmn99, R.drawable.pkmn100,
R.drawable.pkmn101, R.drawable.pkmn102,
R.drawable.pkmn103, R.drawable.pkmn104,
R.drawable.pkmn105, R.drawable.pkmn106,
R.drawable.pkmn107, R.drawable.pkmn108,
R.drawable.pkmn109, R.drawable.pkmn110,
R.drawable.pkmn111, R.drawable.pkmn112,
R.drawable.pkmn113, R.drawable.pkmn114,
R.drawable.pkmn115, R.drawable.pkmn116,
R.drawable.pkmn117, R.drawable.pkmn118,
R.drawable.pkmn119, R.drawable.pkmn120,
R.drawable.pkmn121, R.drawable.pkmn122,
R.drawable.pkmn123, R.drawable.pkmn124,
R.drawable.pkmn125, R.drawable.pkmn126,
R.drawable.pkmn127, R.drawable.pkmn128,
R.drawable.pkmn129, R.drawable.pkmn130,
R.drawable.pkmn131, R.drawable.pkmn132,
R.drawable.pkmn133, R.drawable.pkmn134,
R.drawable.pkmn135, R.drawable.pkmn136,
R.drawable.pkmn137, R.drawable.pkmn138,
R.drawable.pkmn139, R.drawable.pkmn140,
R.drawable.pkmn141, R.drawable.pkmn142,
R.drawable.pkmn143, R.drawable.pkmn144,
R.drawable.pkmn145, R.drawable.pkmn146,
R.drawable.pkmn147, R.drawable.pkmn148,
R.drawable.pkmn149, R.drawable.pkmn150,
R.drawable.pkmn151
};
}
Finally, this is the XML file
<?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: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="com.example.thefe.newsmartkedex.MainActivity">
<GridView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/gridview"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:columnWidth="90dp"
android:numColumns="auto_fit"
android:verticalSpacing="10dp"
android:horizontalSpacing="10dp"
android:stretchMode="columnWidth"
android:gravity="center"
/>
</RelativeLayout>
Thanks for help!
First of all, I don't see any violation in your code so let's focus the pictures.
The problem is that when I try this thing with such as 9 or 12 images there are no problems, but when I load all the 151 images (all .png), Android kills the app because it's draining too much system resources.
What do you mean by 'load all the 151 images'? At the same time in the same screen? Or you just make quite a small numbers of them(like 9,12,16 etc.) seen in the view and others out of the screen?
I've heard that there are Java methods that can (don't know how) "destroy" an object when it goes out of the display and then recreate it when it returns in the screen. Anyway if you have different suggestions on how to resolve my problem, every idea is welcome!
You can't simply destroy an object by yourself and the Jvm will handle this for you when your objects are invalid or unused. As to this problem the recreation of objects that you implements in 'getView' seems no harm.
My question is: how many images did you show in one whole screen? And what size of them?
If you can provide your demo here, it will be the best to work on.
Display limited images that can fit your screen and load the other images when scrolling Gridview.
That way system wont have do to do lots of work at once. Your application gets faster also.
You can refer this link:
i want grid view with loading by scroll i have image fetch from sever but i want only 10 images view other can load when scrolling grid view
I know a lot of people asked this question but I'm not sure the solution for my problem is the same.
My code is:
package com.example.goo;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.ScrollView;
import android.widget.TextView;
public class Calendrier extends Activity{
LinearLayout linear;
TextView text;
ScrollView SV;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
SV = new ScrollView(this);
linear = new LinearLayout(this);
linear.setOrientation(LinearLayout.VERTICAL);
text = new TextView(this);
text.setText("This is an example for the Bright Hub !");
SV.addView(linear);
linear.addView(text);
setContentView(linear);
}
}
and the error is:
Caused by: java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first.
You are doing mistake with setContentView because you already added linearLayout in view and you are trying to add second time which cause error,
Try this:
setContentView(SV);
Instead:
setContentView(linear);
Just
setContentView(linear); => setContentView(SV);
Hope it's help
I'm not sure, but I suppose you are getting this error on the last line (setContentView(linear);).
You first add that view linear to the scrollview SV, and then set it as the contentView.
I only know this error to come up when you add one view to another twice, but I suppose setting it as the contentview will work the same: it cannot be both a child of SV AND the root view.
Either set SV in setContentVieW, or don't add linear to that Scrollview
I am rather new to Android programming in general and am having particular difficulty with the xml/java UI shuffle... I have a layout which I would like to use as the view displayed when a custom, view class is instantiated in the activity class. This much works fine by simply calling
setContentView(R.layout.mylayout) ;
in the activity or from the custom view class through a handle to the activity. The trouble comes when I wish to interact with the widgets on the layout-- I've tried getting a handle on the buttons with
myButton = (Button) findViewById(R.id.mybuttonid);
and separately with
Button myButton = new Button(contextHandle);
myButton = (Button) findViewById(R.layout.mybuttonid);
but in both cases whenever I try to call any methods from the assumed myButton object I get a NullPointerException in the logcat report; evidently myButton is not properly instantiated in either case given above. What is the proper way to instantiate components of a view in a case like this that combines xml and java so that they can call methods dynamically?
thanks,
CCJ
EDIT: Thanks all for the replies, but I think up to 8/1/2011 the advice has been mostly targeted at an implementation wherein the widgets are to be instantiated in the activity class; I wish to instantiate widgets from an xml layout in a custom view class-- a class completely separate from the activity class which extends View and implements its own OnClickListener interface. Below is my code:
MyActivity Class:
package com.ccg.myactivity;
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.RadioButton;
public class MyActivity extends Activity implements OnClickListener {
private boolean touched = false;
private RadioButton myRB;
private Button runB;
private CustomView myView;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.mainlayout);
myRB = (RadioButton) findViewById(R.id.testrb);
runB = (Button) findViewById(R.id.goButton);
//set onClick listeners for activity class
runB.setOnClickListener(this);
}
public void onResume(){
super.onResume();
}
public void onClick(View v) {
// do something when the button is clicked
if (myRB.isChecked()){
setContentView(R.layout.mylayout);
myView = new CustomView(this,this); //passing in activity and context
//handles to custom View class
//myView.getAnotherB().setOnClickListener(this); //commented out as we
//don't want to register the custom view's button with the Activty class's
//OnClickListener; instead it should be registered with the custom View class's own
//OnClickListener implementation.
}
else{
Log.d("me","alt click");
}
}
}
CustomView Class:
package com.ccg.myactivity;
import android.app.Activity;
import android.content.Context;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.*;
import android.view.View.OnClickListener;
public class CustomView extends View implements OnClickListener{
private Button anotherB;
private Context contextHandle;
private Activity actHandle;
public CustomView(Context context, Activity act) {
super(context);
contextHandle = context;
actHandle = act;
//anotherB = new Button(contextHandle); //this shouldn't be necessary for
//instantiation from XML widget
initCustomView();
}
public void initCustomView(){
anotherB = (Button) findViewById(R.id.nextbutton);
anotherB.setOnClickListener(this);
}
public Button getAnotherB(){
return anotherB;
}
#Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
Log.d("me", "Got the custom click!");
}
}
mainlayout.xml from which the default view is made:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
android:id="#+id/widget474"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical">
<RadioGroup android:id="#+id/widget30" android:orientation="horizontal"
android:layout_x="2dip" android:layout_y="57dip" android:layout_width="match_parent"
android:layout_height="wrap_content">
<RadioButton android:layout_height="wrap_content" android:id="#+id/testrb"
android:textSize="15sp" android:text="Run" android:layout_width="wrap_content"
android:textColor="#ffff99ff"></RadioButton>
</RadioGroup>
<Button android:layout_width="wrap_content" android:text="#string/RUN"
android:id="#+id/goButton" android:layout_height="wrap_content"
android:layout_x="222dip" android:layout_y="110dip"></Button>
</LinearLayout>
mylayout.xml from which the custom view's layout is created:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
android:id="#+id/widget0"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical">
<Button android:id="#+id/nextbutton" android:layout_height="wrap_content"
android:layout_width="wrap_content" android:text="work!!!"
>
</Button>
</LinearLayout>
okay, if anybody can explain why any method calls from the button object anotherB (anotherB.setOnClickListener(this) above, but also the simpler anotherB.bringToFront()) cause a force close and a nullpointerexception in logcat with the above implementation I would be most appreciative. thanks!
CCJ
I would declare your button outside of onCreate without the contextHandle parameter... The context will be imbedded in your button upon instantiation (as I understand it).
try:
class YOUR_CLASS {
Button myButton;
onCreate() {
myButton = (Button) findViewById(R.id.WHATEVER_YOU_CALLED_IT_IN_XML);
then you can set an onClickListener or other abilities (you can google that, its easy)
myButton.setOnClickListener(myOnClickListener);
myButton.setText("click me!");
}
}
This sometimes happens to me when the import isn't correct. Sometimes Eclipse will fill in the import as:
import android.R;
of course, this will never find your ID. You should either not have an import, or have something like
import com.myco.mytestapp.R;
If you do that, then the first way of doing it is correct:
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.mylayout);
Button b = (Button) findViewById(R.id.mybutton);
}
Okay, thanks to some advice from the android developers google group I think I've found the answer to at least the most pressing concern (the NPE and force close):
I needed to override onFinishInflate in my custom View class; it is at that point that my XML layout child views (like anotherB) are truly instantiated. The class now looks like this
package com.ccg.myactivity;
import android.app.Activity;
import android.content.Context;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.*;
import android.view.View.OnClickListener;
public class CustomView extends View implements OnClickListener{
private Button anotherB;
private Context contextHandle;
private Activity actHandle;
public CustomView(Context context, Activity act) {
super(context);
contextHandle = context;
actHandle = act;
//anotherB = new Button(contextHandle); //this shouldn't be necessary for
//instantiation from XML widget
initCustomView();
}
public void initCustomView(){
anotherB = (Button) findViewById(R.id.nextbutton);
anotherB.setOnClickListener(this);
}
public Button getAnotherB(){
return anotherB;
}
#Override
public void onFinishInflate(){
anotherB.setOnClickListener(this); //it seems any addressing of child
//views of the layout [the widgets] need to be made after the
//framework calls this method.
}
#Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
Log.d("me", "Got the custom click!");
}
}
Now it pulls up the layout properly and does not throw an NPE. Of course, the onClickListener callback still isn't working right (the message 'Got the custom click!' never appears in logcat), but that's another issue...
thanks all
CCJ
Okay, finally had some time to revisit this issue and I believe I've found the answer:
First, before the xml layout or its components can be addressed they need to be inflated. I knew this, but I wasn't sure when exactly they were inflated. It turns out that setContextView (and probably addContextView) trigger xml inflations. In order to have completely modular activity/view classes, I needed to do something like the following:
Activity Class--
package com.ai.ultimap;
import com.ai.ultimap.views.HomeView;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup.LayoutParams;
public class UltiMapActivity extends Activity {
private View hv;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
hv = new HomeView(this);
}
}
Custom View Class-
package com.ai.ultimap.views;
import com.ai.ultimap.R;
import android.app.Activity;
import android.os.Bundle;
import android.view.*;
import android.widget.*;
import android.view.View.OnClickListener;
public class HomeView extends View implements OnClickListener{
private RadioButton twodRB;
private RadioButton threedRB;
private TextView locTV;
private EditText editlocET;
public HomeView(Activity hAct) {
super(hAct);
//THE FOLLOWING LINE INFLATES-- IT (or another function which calls xml inflation)
//MUST COME BEFORE ANY JAVA ADDRESSING OF WIDGETS IN
//THE XML LAYOUT
//Also note that even though you could invoke findViewById from a class extending
//View, in this case you must use hAct.findViewById. I believe this is due to the
//fact that the activity referenced by hAct is the object responsible for inflating
//the xml and thus the widgets need to be instantiated from it.
hAct.setContentView(R.layout.ultimap);
twodRB = (RadioButton) hAct.findViewById(R.id.twodRBV);
threedRB = (RadioButton) hAct.findViewById(R.id.threedRBV);
locTV = (TextView) hAct.findViewById(R.id.locationTV);
editlocET = (EditText) hAct.findViewById(R.id.locationETV);
//After instantiation however they can be freely accessed from java in
//non-activity classes, which is the point; see the next line...
twodRB.setOnClickListener(this);
}
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
locTV.setText("yo");
}
}
This code works properly to load up the pre-defined xml view ultimap.xml and then address the widgets dynamically from Java (completely outside the activity class), changing the text of the location text view from 'Location' to 'yo' when the twodRB radiobutton is clicked!
Hope this helps some googlers :)
-CCJ