unable to change webview height programmatically - java

I set a webview on my main layout and I am unable to resize it programmatically. Webview is in the "main" layout.
If do the same with his contentainer "layoutA", works but not a solution, I need to set only webview view. Thank you.
protected void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
final RelativeLayout LayoutA = (RelativeLayout)findViewById(R.id.aLayout);
webviewA = (WebView) findViewById(R.id.webviewA);
Handler handler = new Handler();
final Runnable r = new Runnable()
{
public void run()
{
LayoutParams params = (LayoutParams) webviewA.getLayoutParams();
params.height -= 150;
}
};
handler.postDelayed(r, 1000);
logcat,
07-06 11:23:21.204: E/AndroidRuntime(13795): FATAL EXCEPTION: main
07-06 11:23:21.204: E/AndroidRuntime(13795): java.lang.ClassCastException: android.widget.RelativeLayout$LayoutParams
07-06 11:23:21.204: E/AndroidRuntime(13795): at com.xxxx.projecte1.webPush$1.run(webPush.java:109)
07-06 11:23:21.204: E/AndroidRuntime(13795): at android.os.Handler.handleCallback(Handler.java:587)
07-06 11:23:21.204: E/AndroidRuntime(13795): at android.os.Handler.dispatchMessage(Handler.java:92)
07-06 11:23:21.204: E/AndroidRuntime(13795): at android.os.Looper.loop(Looper.java:138)
07-06 11:23:21.204: E/AndroidRuntime(13795): at android.app.ActivityThread.main(ActivityThread.java:3701)
07-06 11:23:21.204: E/AndroidRuntime(13795): at java.lang.reflect.Method.invokeNative(Native Method)
07-06 11:23:21.204: E/AndroidRuntime(13795): at java.lang.reflect.Method.invoke(Method.java:507)
07-06 11:23:21.204: E/AndroidRuntime(13795): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:878)
07-06 11:23:21.204: E/AndroidRuntime(13795): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:636)
07-06 11:23:21.204: E/AndroidRuntime(13795): at dalvik.system.NativeStart.main(Native Method)

The problem is in imported package plz check packege for your case it should be if it not present than add it instead of other pkg like import android.widget.RelativeLayout etc...
import android.view.ViewGroup.LayoutParams;

Call invalidate() on your WebView.
See http://developer.android.com/reference/android/view/View.html ( Drawing Section )
Greets Flo

You have imported the RelativeLayout.LayoutParams in your activity class.
Try to do it with
public void run()
{
ViewGroup.LayoutParams params = (ViewGroup.LayoutParams) webviewA.getLayoutParams();
params.height -= 150;
}

Related

Android NumberPicker NullPointer why?

i am try to implement a numberPicker to select minute values.
But i am getting a NullPointer Exception at this line:
minutePicker = (NumberPicker) findViewById(R.id.minuten_picker);
Following Code:
public class MainActivity extends Activity {
NumberPicker minutePicker;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Auswahl Minuten zum starten / Stoppen aller
minutePicker = new NumberPicker(MainActivity.this);
minutePicker = (NumberPicker) findViewById(R.id.minuten_picker);
minutePicker.setMaxValue(30);
minutePicker.setOnValueChangedListener(new NumberPicker.OnValueChangeListener() {
#Override
public void onValueChange(NumberPicker picker, int oldVal, int newVal) {
abschaltzeit = minutePicker.getValue();
}
});
minutePicker.setValue(0);
minutePicker.setWrapSelectorWheel(false);
}
}
XML:
<NumberPicker
android:id="#+id/minuten_picker"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_row="6"
android:layout_column="0"
android:paddingLeft="20dp" />
Log:
09-25 11:00:09.749 10687-10687/de.carsten.awesome.app E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: de.carsten.awesome.app, PID: 10687
java.lang.RuntimeException: Unable to start activity ComponentInfo{de.carsten.awesome.app/de.carsten.awesome.app.MainActivity}: java.lang.NullPointerException
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2184)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2233)
at android.app.ActivityThread.access$800(ActivityThread.java:135)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5001)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.NullPointerException
at de.carsten.awesome.app.MainActivity.onCreate(MainActivity.java:83)
at android.app.Activity.performCreate(Activity.java:5231)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2148)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2233)
at android.app.ActivityThread.access$800(ActivityThread.java:135)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5001)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
at dalvik.system.NativeStart.main(Native Method)
minutePicker = new NumberPicker(MainActivity.this);
minutePicker = (NumberPicker) findViewById(R.id.minuten_picker);
You're creating a NumberPicker programmatically and then overwriting the reference with whatever findViewById() returns. It returns null if your activity_main layout does not contain a minuten_picker.
Choose only the other: either create it prorgrammatically or find it from a view hierarchy you inflated.
If you choose the programmatic way new NumberPicker(), remember to add it to some layout in your activity view hierarchy, e.g. with setContentView()
If you choose the inflation way, make sure you have the view in your XML layout file.
I'm guessing the NPE you're seeing is actually on the following line where you're trying to invoke a method on the minutePicker and it's null.
I apologize.
I moved the Code from the MainActivity in the creating Fragment and it works with rootView.findView.
Sorry but i am new with the Fragement Konzept.
Thanks a lot for your help !

how to set listadapter for fragment

I have two framelayout in my main.xml file. I add framelayouts to the class that extends Fragment. my main class extends FragmentActivity and this is Oncreate method of it:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
FragmentManager fm =getFragmentManager();
FragmentTransaction ft=fm.beginTransaction();
Fragment f=new Freg1();
Fragment f2=new Freg1();
ft.add(R.id.frame1, f);
ft.add(R.id.frame2, f2);
ft.commit();
tf=Typeface.createFromAsset(this.getAssets(),"font/Byekan.ttf" );
tv1=(TextView) findViewById(R.id.textView1);
tv1.setTypeface(tf);
Log.i(TAG,"1");
lv1=(ListView) findViewById(R.id.listView1);
lv2=(ListView) findViewById(R.id.listView2);
Log.i(TAG,"2");
List<String> stringList = new ArrayList<String>(Arrays.asList(s1));
Log.i(TAG,"3");
ListAdapter listAdapter = new CustomListAdapter(MainActivity.this , R.layout.custom_list ,stringList);
Log.i(TAG,"4");
lv1.setAdapter(listAdapter);
Log.i(TAG,"5");
lv2.setAdapter(listAdapter);
Log.i(TAG,"6");
}
when i run the codes, it crashed after LOG no4. that mean setAdapter() method do not work. how can i resolve this problem?
this is my logcat resource:
Shutting down VM
threadid=1: thread exiting with uncaught exception (group=0x40a13300)
FATAL EXCEPTION: main
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.taxitabriz/com.example.taxitabriz.MainActivity}: java.lang.NullPointerException
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2059)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2084)
at android.app.ActivityThread.access$600(ActivityThread.java:130)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1195)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:4745)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.NullPointerException
at com.example.taxitabriz.MainActivity.onCreate(MainActivity.java:55)
at android.app.Activity.performCreate(Activity.java:5008)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1079)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2023)
... 11 more
thank for you that help me to resolve problem.
Your ListView lv1 is null as you can see here:
java.lang.RuntimeException: Unable to start activity ComponentInfo{...}: java.lang.NullPointerException
....
com.example.taxitabriz.MainActivity.onCreate(MainActivity.java:55)
The line 55 of MainActivity should be this call: lv1.setAdapter(listAdapter);
Make sure that your listView1 is included within the layout and initialized correctly prior to trying to set an Adapter to it.

NullPointerException: LinearLayout is Null

Please have a look at the following code
private class OpenFileEvent implements OnClickListener
{
#Override
public void onClick(View arg0) {
LinearLayout openFileDialogView = (LinearLayout)findViewById(R.id.open_file_dialog);
// TODO Auto-generated method stub
final Dialog openFileDialog = new Dialog(VoiceNotes.this);
openFileDialog.setTitle("Open File");
openFileDialog.setContentView(R.layout.open_dialog);
//First, list all the available Files
File folder = new File(Environment.getExternalStorageDirectory()+"/Voice/Notes/");
File file = new File(folder.getAbsolutePath());
File[] fileNameList = file.listFiles();
if(fileNameList != null && fileNameList.length>0)
{
for(int i=0;i<fileNameList.length;i++)
{
//Get the sub views first
LayoutInflater inflater = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View openThisFileView = inflater.inflate(R.layout.open_dialog_file, null);
Button openThisFileButton = (Button)openThisFileView.findViewById(R.id.open_this_file_button);
Button appendThisFileButton = (Button)openThisFileView.findViewById(R.id.append_note_this_file);
TextView openThisFileNameTxt = (TextView)openThisFileView.findViewById(R.id.open_this_file_name);
//Set the Text
openThisFileNameTxt.setText(fileNameList[i].getName());
//Set the Listeners
//Add the View
openFileDialogView.addView(openThisFileView);
}
}
//Show the Dialog
openFileDialog.show();
}
}
As soon as this code in running, I get the following error message, which is NullPointerException
11-18 16:24:20.832: E/AndroidRuntime(1019): FATAL EXCEPTION: main
11-18 16:24:20.832: E/AndroidRuntime(1019): java.lang.NullPointerException
11-18 16:24:20.832: E/AndroidRuntime(1019): at com.x.xxx.VoiceNotes$OpenFileEvent.onClick(VoiceNotes.java:227)
11-18 16:24:20.832: E/AndroidRuntime(1019): at android.view.View.performClick(View.java:4204)
11-18 16:24:20.832: E/AndroidRuntime(1019): at android.view.View$PerformClick.run(View.java:17355)
11-18 16:24:20.832: E/AndroidRuntime(1019): at android.os.Handler.handleCallback(Handler.java:725)
11-18 16:24:20.832: E/AndroidRuntime(1019): at android.os.Handler.dispatchMessage(Handler.java:92)
11-18 16:24:20.832: E/AndroidRuntime(1019): at android.os.Looper.loop(Looper.java:137)
11-18 16:24:20.832: E/AndroidRuntime(1019): at android.app.ActivityThread.main(ActivityThread.java:5041)
11-18 16:24:20.832: E/AndroidRuntime(1019): at java.lang.reflect.Method.invokeNative(Native Method)
11-18 16:24:20.832: E/AndroidRuntime(1019): at java.lang.reflect.Method.invoke(Method.java:511)
11-18 16:24:20.832: E/AndroidRuntime(1019): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
11-18 16:24:20.832: E/AndroidRuntime(1019): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
11-18 16:24:20.832: E/AndroidRuntime(1019): at dalvik.system.NativeStart.main(Native Method)
The error is targetting to here
openFileDialogView.addView(openThisFileView);
Since it says that LinearLayout is null, here is the XML file where that layout belongs to.
open_dialog.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/open_file_dialog"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="#ffffff">
</LinearLayout>
This is the same layout which acts as the Content View for the Dialog.
Why I think that LinearLayout is null, and not the layout which got inflated inside the class? Because the below code also generated NullPointerException
openFileDialogView.addView(new Button(Notes.this));
Why I am getting this error?
LinearLayout openFileDialogView = (LinearLayout)findViewById(R.id.open_file_dialog);
Here findViewById() attempts to find the specified view in the activity's view hierarchy (as set by setContentView() but you're inflating a layout with that id only later on. findViewById() returns null and attempting to invoke a method on null causes NPE.
Try moving this line
LinearLayout openFileDialogView = (LinearLayout)findViewById(R.id.open_file_dialog);
after
openFileDialog.setContentView(R.layout.open_dialog);
in your code.

check if checkbox is checked return null pointer

I have this code for an expandable list, I want to have a checkbox in the childgroups of the list view, and check if one of the checkboxes is checked.
The problem is that when I check if the checkbox is checked I get a NULL Pointer Exception.
Can you please tell me whats wrong?
here's my code - I've edited the code to inflate a view of the child_row.xml that holds that checkbox but I still get that null pointer, what am I doing wrong?!?!
package send.Shift;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import android.app.ExpandableListActivity;
import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.ExpandableListView;
import android.widget.SimpleExpandableListAdapter;
import android.widget.TextView;
public class Shifts extends ExpandableListActivity implements
OnCheckedChangeListener {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.list);
SimpleExpandableListAdapter expListAdapter = new SimpleExpandableListAdapter(
this, createGroupList(), R.layout.group_row,
new String[] { "Group Item" }, new int[] { R.id.row_name },
createChildList(), R.layout.child_row,
new String[] { "Sub Item" }, new int[] { R.id.grp_child });
getExpandableListView().setGroupIndicator(
getResources().getDrawable(R.drawable.expander_group));
ExpandableListView EX = (ExpandableListView) findViewById(android.R.id.list);
EX.setAdapter(expListAdapter);
final CheckBox childBox = (CheckBox) findViewById(R.id.childBOX);
final TextView choosenGroup = (TextView) findViewById(R.id.choosen);
LayoutInflater inflater = LayoutInflater.from(Shifts.this);
View view2 = inflater.inflate(R.layout.child_row, null);
childBox.setOnCheckedChangeListener(new OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton buttonView,
boolean isChecked) {
// TODO Auto-generated method stub
if(childBox.isChecked() == true){
choosenGroup.setText("Shift Set");
}
}
});
}
Here is some of the Logcat log:
12-23 07:38:00.644: W/dalvikvm(880): threadid=1: thread exiting with uncaught exception (group=0x40015560)
12-23 07:38:00.654: E/AndroidRuntime(880): FATAL EXCEPTION: main
12-23 07:38:00.654: E/AndroidRuntime(880): java.lang.RuntimeException: Unable to start activity ComponentInfo{send.Shift/send.Shift.Shifts}: java.lang.NullPointerException
12-23 07:38:00.654: E/AndroidRuntime(880): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1647)
12-23 07:38:00.654: E/AndroidRuntime(880): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
12-23 07:38:00.654: E/AndroidRuntime(880): at android.app.ActivityThread.access$1500(ActivityThread.java:117)
12-23 07:38:00.654: E/AndroidRuntime(880): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
12-23 07:38:00.654: E/AndroidRuntime(880): at android.os.Handler.dispatchMessage(Handler.java:99)
12-23 07:38:00.654: E/AndroidRuntime(880): at android.os.Looper.loop(Looper.java:123)
12-23 07:38:00.654: E/AndroidRuntime(880): at android.app.ActivityThread.main(ActivityThread.java:3683)
12-23 07:38:00.654: E/AndroidRuntime(880): at java.lang.reflect.Method.invokeNative(Native Method)
12-23 07:38:00.654: E/AndroidRuntime(880): at java.lang.reflect.Method.invoke(Method.java:507)
12-23 07:38:00.654: E/AndroidRuntime(880): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
12-23 07:38:00.654: E/AndroidRuntime(880): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
12-23 07:38:00.654: E/AndroidRuntime(880): at dalvik.system.NativeStart.main(Native Method)
12-23 07:38:00.654: E/AndroidRuntime(880): Caused by: java.lang.NullPointerException
12-23 07:38:00.654: E/AndroidRuntime(880): at send.Shift.Shifts.onCreate(Shifts.java:41)
12-23 07:38:00.654: E/AndroidRuntime(880): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
12-23 07:38:00.654: E/AndroidRuntime(880): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1611)
12-23 07:38:00.654: E/AndroidRuntime(880): ... 11 more
If you get a nullpointer on a certain line, something on that line is null. If it is the if(childBox.isChecked() line, probably childBox is null. Hard to say without the stack, but most probable cause is the line where you retrieve that checkbox.
final CheckBox childBox = (CheckBox) findViewById(R.id.childBOX);
Might be returning null, and this could be for several reasons. It could be your id is childBox instead of childBOX. Or that it is not in R.layout.list.
The best thing you can do is start debugging. What line is the error. Find the object that is null. Find out why it is null and if that is expected or not.
Instead of checking the childBox.isChecked() you can use the isChecked value. So your code would look like this:
#Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if(isChecked == true){
choosenGroup.setText("Shift Set");
}
}
Check your layout list.xml I think this layout may is missing android:id="#+id/childBOX" for Check Box or android:id="#+id/choosen" for TextView
Check the folowin sample
<CheckBox android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/childBOX"/>
<TextView android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/choosen"/>

The application has stopped unexcepetedly

Android tells me that The application has stopped unexcepetedly, but there are no error's in my code.
package com.example.helloandroid;
import android.app.Activity;
import android.os.Bundle;
import android.widget.ImageView;
import android.widget.TableLayout;
public class HelloAndroid extends Activity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
for(int i = 0; i < 6; i++) {
TableLayout tl = (TableLayout) findViewById(R.id.T);
for(int j = 0; j < 6; j++) {
ImageView img = (ImageView) tl.getChildAt(j);
img.setImageResource(R.drawable.w);
}
}
}
}
Logcat says:
03-03 19:25:43.550: WARN/dalvikvm(487): threadid=1: thread exiting with uncaught exception (group=0x40015560)
03-03 19:25:43.580: ERROR/AndroidRuntime(487): FATAL EXCEPTION: main
03-03 19:25:43.580: ERROR/AndroidRuntime(487): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.helloandroid/com.example.helloandroid.HelloAndroid}: java.lang.ClassCastException: android.widget.TableRow
03-03 19:25:43.580: ERROR/AndroidRuntime(487): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1647)
03-03 19:25:43.580: ERROR/AndroidRuntime(487): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
03-03 19:25:43.580: ERROR/AndroidRuntime(487): at android.app.ActivityThread.access$1500(ActivityThread.java:117)
03-03 19:25:43.580: ERROR/AndroidRuntime(487): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
03-03 19:25:43.580: ERROR/AndroidRuntime(487): at android.os.Handler.dispatchMessage(Handler.java:99)
03-03 19:25:43.580: ERROR/AndroidRuntime(487): at android.os.Looper.loop(Looper.java:123)
03-03 19:25:43.580: ERROR/AndroidRuntime(487): at android.app.ActivityThread.main(ActivityThread.java:3683)
03-03 19:25:43.580: ERROR/AndroidRuntime(487): at java.lang.reflect.Method.invokeNative(Native Method)
03-03 19:25:43.580: ERROR/AndroidRuntime(487): at java.lang.reflect.Method.invoke(Method.java:507)
03-03 19:25:43.580: ERROR/AndroidRuntime(487): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
03-03 19:25:43.580: ERROR/AndroidRuntime(487): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
03-03 19:25:43.580: ERROR/AndroidRuntime(487): at dalvik.system.NativeStart.main(Native Method)
03-03 19:25:43.580: ERROR/AndroidRuntime(487): Caused by: java.lang.ClassCastException: android.widget.TableRow
03-03 19:25:43.580: ERROR/AndroidRuntime(487): at com.example.helloandroid.HelloAndroid.onCreate(HelloAndroid.java:17)
03-03 19:25:43.580: ERROR/AndroidRuntime(487): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
03-03 19:25:43.580: ERROR/AndroidRuntime(487): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1611)
03-03 19:25:43.580: ERROR/AndroidRuntime(487): ... 11 more
EDIT: The problem was on the layout:
package com.example.helloandroid;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.TableLayout;
import android.widget.TableRow;
public class HelloAndroid extends Activity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
for(int i = 0; i < 7; i++) {
TableLayout tl = (TableLayout) findViewById(R.id.T);
TableRow tr = (TableRow) tl.getChildAt(i);
for(int j = 0; j < 7; j++) {
ImageView img = (ImageView) tr.getChildAt(j);
img.setImageResource(R.drawable.w);
}
}
}
}
Look at Logcat to see the stack trace which will pinpoint the fault location.
My guess is that findViewById() or getChildAt() is returning null, i.e. your layout isn't quite what you think it is.
TableLayout tl = (TableLayout) findViewById(R.id.T); :
The result of findViewById(R.id.T) is most likely not of type TableLayout (but not null).
Try to print out findViewById(R.id.T).getClass().getName();
That's what this tells me:
Caused by: java.lang.ClassCastException: android.widget.TableRow
at com.example.helloandroid.HelloAndroid.onCreate(HelloAndroid.java:17)
I was getting this error for the reason of wrong encoding for layout.xml. Instead of...
<?xml version="1.0" encoding="iso-8859-1"?>
...it should have been in my case:
<?xml version="1.0" encoding="utf-8"?>
Little hard to figure this one out.

Categories