AppCompat - Spinner not working in RelativeLayout - android

I get following error (using the newest support library v23.0 and build SDK 23):
Case 1: Android 4.2.1
java.lang.NullPointerException
at android.widget.RelativeLayout.onMeasure(RelativeLayout.java:523)
at android.view.View.measure(View.java:15612)
at android.support.v7.internal.widget.ListViewCompat.measureHeightOfChildrenCompat(ListViewCompat.java:301)
at android.support.v7.widget.ListPopupWindow.buildDropDown(ListPopupWindow.java:1200)
at android.support.v7.widget.ListPopupWindow.show(ListPopupWindow.java:584)
at android.support.v7.widget.AppCompatSpinner$DropdownPopup.show(AppCompatSpinner.java:766)
at android.support.v7.widget.AppCompatSpinner.performClick(AppCompatSpinner.java:424)
at android.view.View$PerformClick.run(View.java:17439)
at android.os.Handler.handleCallback(Handler.java:725)
at android.os.Handler.dispatchMessage(Handler.java:92)
at android.os.Looper.loop(Looper.java:153)
at android.app.ActivityThread.main(ActivityThread.java:5341)
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:929)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:696)
at dalvik.system.NativeStart.main(Native Method)
Case 2: Android 4.3
java.lang.NullPointerException
at android.widget.RelativeLayout.onMeasure(RelativeLayout.java:578)
at android.view.View.measure(View.java:16831)
at android.support.v7.internal.widget.ListViewCompat.measureHeightOfChildrenCompat(ListViewCompat.java:301)
at android.support.v7.widget.ListPopupWindow.buildDropDown(ListPopupWindow.java:1200)
at android.support.v7.widget.ListPopupWindow.show(ListPopupWindow.java:584)
at android.support.v7.widget.AppCompatSpinner$DropdownPopup.show(AppCompatSpinner.java:766)
at android.support.v7.widget.AppCompatSpinner.performClick(AppCompatSpinner.java:424)
at android.view.View$PerformClick.run(View.java:18786)
at android.os.Handler.handleCallback(Handler.java:730)
at android.os.Handler.dispatchMessage(Handler.java:92)
at android.os.Looper.loop(Looper.java:176)
at android.app.ActivityThread.main(ActivityThread.java:5419)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:525)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1046)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:862)
at dalvik.system.NativeStart.main(Native Method)
Actually I don't know where to start searching. I get this error from crashes and can't reproduce it on my phone. And I don't know where it really comes from.
I searched my code and I don't have a Spinner directly nested in any RelativeLayout...
Does anyone have a hint what can cause this problem?
Changes in my code I made
Mainly I updated from support library and build sdk v22 to v23. And I never had a problem like this before...
Reason for error, demonstrated based on the error in Android 4.2.1:
http://grepcode.com/file/repository.grepcode.com/java/ext/com.google.android/android/4.2.1_r1.2/android/widget/RelativeLayout.java#RelativeLayout
Here in Line 523... That's what I get from the bug report I receive...
523 if (mLayoutParams.height >= 0) {
524 height = Math.max(height, mLayoutParams.height);
525 }
In android 4.3 it's actually the same problem, mLayoutParams is null...

I found the reason.
I'm using many adapters and in one of them I accidently did attach the view to the root view... This appearently is no problem on most devices and android versions, but on some...
convertView = mInflater.inflate(mLayoutRes, null);
Should be
convertView = mInflater.inflate(mLayoutRes, null, false);
if the adapter is used in a Spinner...

Try using the following code:
activity_main.xml
<RelativeLayout xmlns:androclass="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity" >
<Spinner
android:id="#+id/spinner1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="83dp" />
</RelativeLayout>
MainActivity.java:
package com.example.spinner;
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends Activity implements
AdapterView.OnItemSelectedListener {
String[] country = { "India", "USA", "China", "Japan", "Other", };
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//Getting the instance of Spinner and applying OnItemSelectedListener on it
Spinner spin = (Spinner) findViewById(R.id.spinner1);
spin.setOnItemSelectedListener(this);
ArrayAdapter aa = new ArrayAdapter(this,android.R.layout.simple_spinner_item,country);
aa.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spin.setAdapter(aa);
}
#Override
public void onItemSelected(AdapterView<?> arg0, View arg1, int position,long id) {
Toast.makeText(getApplicationContext(),country[position] ,Toast.LENGTH_LONG).show();
}
#Override
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
}

You catch the exception incase you inflate layout which has only a view (without ViewGroup).
Example
android:layout/simple_spinner_dropdown_item has only CheckedTextView:
<?xml version="1.0" encoding="utf-8"?>
<CheckedTextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#android:id/text1"
style="?android:attr/spinnerDropDownItemStyle"
android:singleLine="true"
android:layout_width="match_parent"
android:layout_height="?android:attr/dropdownListPreferredItemHeight"
android:ellipsize="marquee"/>
You can fix it by add an ViewGroup in layout of your adapter item view :
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<include layout="#android:layout/simple_spinner_dropdown_item"/>
</LinearLayout>

#prom85 solution did not work for me till I passed the parent ViewGroup instead of null:
convertView = mInflater.inflate(mLayoutRes, parent, false);

Related

PopupMenu from PopupWindow in Android

UPDATE
The following works on API 26 but it crashes on API 23.
Does anyone know why this is the case?
Also, any workaround?
I really need your help with my android development.
What I want to do is have PopupWindow.
Inside the popupwindow, there is a button, that shows popupmenu.
Is this technically possible?
I have the following code but it stops working when I click on the button inside popupwindow.
The app shuts down at the line that says "popupMenu.show()"
import android.os.Build;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.PopupMenu;
import android.widget.RelativeLayout;
public class MainActivity extends AppCompatActivity {
private Button mPopupWindowButton;
private Button mButtonPopupMenuOnPopupWindow;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mPopupWindowButton = (Button) findViewById(R.id.btn);
mPopupWindowButton.setOnClickListener(new View.OnClickListener(){
#Override
public void onClick(View view) {
LayoutInflater inflater = (LayoutInflater)getApplicationContext().getSystemService(LAYOUT_INFLATER_SERVICE);
View settingsView = inflater.inflate(R.layout.settings, null);
PopupWindow popupWindow = new PopupWindow(settingsView, RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
if(Build.VERSION.SDK_INT >=21){
popupWindow.setElevation(5.0f);
}
LinearLayout mainLayout = (LinearLayout)findViewById(R.id.mainActivityLayout);
popupWindow.showAtLocation(mainLayout, Gravity.BOTTOM,0,0);
mButtonPopupMenuOnPopupWindow = settingsView.findViewById(R.id.btnPopupMenuOnPopupWindow);
mButtonPopupMenuOnPopupWindow.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
PopupMenu popupMenu = new PopupMenu(MainActivity.this, view);
popupMenu.inflate(R.menu.age_group);
popupMenu.show();
}
});
}
});
}
}
If this is not possible, is there a workaround to it?
Basically, I want a dropdown option inside popupwindow.
Thanks for your time.
UPDATE
This is the crash log
12-31 21:09:49.600 3960-3960/com.codingdaddy.popupwindowexample E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.codingdaddy.popupwindowexample, PID: 3960
android.view.WindowManager$BadTokenException: Unable to add window -- token android.view.ViewRootImpl$W#4f880f0 is not valid; is your activity running?
at android.view.ViewRootImpl.setView(ViewRootImpl.java:567)
at android.view.WindowManagerGlobal.addView(WindowManagerGlobal.java:310)
at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:85)
at android.widget.PopupWindow.invokePopup(PopupWindow.java:1258)
at android.widget.PopupWindow.showAsDropDown(PopupWindow.java:1110)
at android.widget.ListPopupWindow.show(ListPopupWindow.java:658)
at com.android.internal.view.menu.MenuPopupHelper.tryShow(MenuPopupHelper.java:170)
at com.android.internal.view.menu.MenuPopupHelper.show(MenuPopupHelper.java:124)
at android.widget.PopupMenu.show(PopupMenu.java:218)
at com.codingdaddy.popupwindowexample.MainActivity$1$1.onClick(MainActivity.java:50)
at android.view.View.performClick(View.java:5198)
at android.view.View$PerformClick.run(View.java:21147)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5417)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
Yes it is possible please modify your following click listener like the below:- Instead of getBaseContext use Activity.this
mButtonPopupMenuOnPopupWindow = settingsView.findViewById(R.id.btnPopupMenuOnPopupWindow);
mButtonPopupMenuOnPopupWindow.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
PopupMenu popupMenu = new PopupMenu(MainActivity.this, view);
popupMenu.inflate(R.menu.menu);
popupMenu.show();
}
});
Main layout file
<LinearLayout android:id="#+id/main_layout"
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:gravity="center_horizontal"
android:orientation="vertical"
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="lib4.com.stackoverflow.SimpleActivity">
<Button android:id="#+id/btn" android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TAP"/>
</LinearLayout>
Settings layout file
<LinearLayout android:id="#+id/main_layout"
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center_horizontal"
android:orientation="vertical"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin">
<Button android:id="#+id/btnPopupMenuOnPopupWindow" android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="button"/>
</LinearLayout>
Menu resource file
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="#+id/about"
android:title="About"/>
<item android:id="#+id/help"
android:title="Help"/>
</menu>
Please let me know if it works.

Can't make my Listview appear [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 7 years ago.
Improve this question
I'm working on an android application and cannot seem to make my listView appear when the app is run. I've been toggling with some of the xml but I'm still rather new and android updates quite often so tutorials are constantly becoming outdated.The actual java code seems fine to me I can't see much difference there. I could really use an extra pair of eyes on this , maybe there is something I'm missing.
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.Menu;
import android.view.MenuItem;
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
import android.support.v4.app.Fragment;
import android.os.Bundle;
import android.text.Layout;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.FrameLayout;
import android.widget.ListView;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
/**
* A placeholder fragment containing a simple view.
*/
public class MainActivityFragment extends Fragment {
public MainActivityFragment() {
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
String[] forecastArray = {
"Today, colder than bumblefuck",
"Friday, condsider moving",
"Saturday, not even gonna bother",
"Sunday, Partly Cloudy",
"Monday, Hell Froze over",
"Tuesday, Cloudy with a chance of meatballs",
"Wednesday,Light Showers"
};
List<String> weekForeCast = new ArrayList<String>(
Arrays.asList(forecastArray));
ArrayAdapter<String> bindIt= new ArrayAdapter<String>(getActivity(),R.layout.list_item_forecast,R.id.list_item_forecast_textview,weekForeCast);
ListView listView = (ListView) inflater.inflate(R.layout.fragment_main, container, false).findViewById(R.id.listview_forecast);
listView.setAdapter(bindIt);
return inflater.inflate(R.layout.fragment_main, container, false);
}
}
<FrameLayout 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=".MainActivityFragment">
<ListView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/listview_forecast">
</ListView>
</FrameLayout>
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:minHeight="?android:attr/listPreferredItemHeight"
android:gravity="center_vertical"
android:id ="#+id/list_item_forecast_textview"
android:visibility="visible"
android:enabled="false">
</TextView>
<fragment xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" android:id="#+id/fragment"
android:name="com.alesterlewis.sunshine.app.MainActivityFragment"
tools:layout="#layout/fragment_main" android:layout_width="match_parent"
android:layout_height="match_parent" />
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.alesterlewis.sunshine.app" >
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name=".MainActivity"
android:label="#string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
The problem is that you are inflating a new view while returning from the onCreateView. Thus, the one in which you had set the adapter and for the listview is lost. This is the bug.
First inflate the layout for the fragment in the view
View view = inflater.inflate(R.layout.fragment_main, container, false);
Then initialise its components, here you can set the list view adapter, etc and if you would have had more view, initialised the same.
ArrayAdapter<String> bindIt= new ArrayAdapter<String>(getActivity(),R.layout.list_item_forecast,R.id.list_item_forecast_textview,weekForeCast);
ListView listView = (ListView) view.findViewById(R.id.listview_forecast);
listView.setAdapter(bindIt);
and in the end return the same view that was initially infalated and worked upon!
Did you set an adapter for your list? Maybe your adapter list is empty for some reason.
UPDATE
Try to anticipate the view creation:
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_main, container, false);
String[] forecastArray = {
"Today, colder than bumblefuck",
"Friday, condsider moving",
"Saturday, not even gonna bother",
"Sunday, Partly Cloudy",
"Monday, Hell Froze over",
"Tuesday, Cloudy with a chance of meatballs",
"Wednesday,Light Showers"
};
List<String> weekForeCast = new ArrayList<String>(
Arrays.asList(forecastArray));
ArrayAdapter<String> bindIt= new ArrayAdapter<String>(getActivity(),R.layout.list_item_forecast,R.id.list_item_forecast_textview,weekForeCast);
ListView listView = (ListView) view.findViewById(R.id.listview_forecast);
listView.setAdapter(bindIt);
return view;
}

Android unable to find my onClick method

I am having an issue where my Button (id i_am_a_problem) which is declared in the fragment's layout XML, is generating an error when the button is clicked. Android tries to call the onClick method public void calculate(View v) but is unable to find it (despite it being declared in MainActivity). Details of my code are below, and the error is after that. Please help me determine why android is unable to find the onClick method. Thanks.
There is really not anything else going on in this code (fresh new project)
MainActivity.java
package supercali.FRAG.alistic;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) { /*snip - irrelevance*/ }
#Override
public boolean onOptionsItemSelected(MenuItem item) { /*snip - irrelevance*/ }
public void calculcate(View v){
TextView tv = (TextView)findViewById(R.id.results);
tv.setText(calculate_value());
}
private String calculate_value(){
return "Abra Kadabra";
}
}
MainActivity's layout just contains a fragment
<fragment
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/fragment"
android:name="supercali.FRAG.alistic.MainActivityFragment"
tools:layout="#layout/fragment_main"
android:layout_width="match_parent"
android:layout_height="match_parent" />
And the layout for that Fragment is:
<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: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=".MainActivityFragment">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/calculate"
android:onClick="calculate"
android:id="#+id/i_am_a_problem"/>
<TextView android:text="#string/results_pending" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:id="#+id/results"/>
</LinearLayout>
The problem Button is just there ^^ with id i_am_a_problem
LogCat:
06-18 09:49:41.280 31642-31642/supercali.FRAG.alistic E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: supercali.FRAG.alistic, PID: 31642
java.lang.IllegalStateException: Could not find method calculate(View) in a parent or ancestor Context for android:onClick attribute defined on view class android.support.v7.widget.AppCompatButton
at android.view.View$DeclaredOnClickListener.resolveMethod(View.java:4441)
at android.view.View$DeclaredOnClickListener.onClick(View.java:4405)
at android.view.View.performClick(View.java:5147)
at android.view.View$PerformClick.run(View.java:21069)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5401)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:725)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:615)
You misspelled the method.
the XML is looking for calculate but your method is calculcate
Your onCLick method is called "calculcate" in the Activity. In the XML it's "calculate".
I faced the same error but my onClick method was overridden from my base activity class and when I override it with editor help its signature became protected and hence was the problem.
Problem was resolved when I made it public.
Write the Method name correctly:
public void calculate(View v){
TextView tv = (TextView)findViewById(R.id.results);
tv.setText(calculate_value());
}
Just both spelling are different....
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/calculate"
android:onClick="calculcate"
android:id="#+id/i_am_a_problem"/>
I just put android:onClick="calculcate"
While you used word "calculcate"
public void calculcate(View v){
.....
}
And you used android:onClick="calculate"
You misspelled calculate. And if that doesn't work, try View view instead of View v.
Actually, In AndroidManifest file to verify name correct or not. AndroidManifest shown read color line if xml file name and class file name not relate.
Base on the problem, onClickListener can't find your class files because of wrong name. Error will only show Onclick error. This problem could confuse that you may set wrong onClickListener but actual problem is you set wrong name in xml file and activity class.
Sometime, you will not see error in xml and activity files because of same android name. In this case you can verify easily what was wrong in AndroidManifest file.

playing video on android webview does not fullscreen

I am making a webview application which shows pages they have html5 videos in it. when I tap the fullscreen, video dissapears.
when I searching same problems, saw this question, which is same problem with me, and thought 2nd answer (here) fits for me also.
Even I use classes given in the answer, I am still having these errors and application stoped.
05-03 13:41:50.064: E/AndroidRuntime(19292): FATAL EXCEPTION: main
05-03 13:41:50.064: E/AndroidRuntime(19292): java.lang.NullPointerException
05-03 13:41:50.064: E/AndroidRuntime(19292): at com.zapkolik.mayis.VideoEnabledWebChromeClient.onShowCustomView(VideoEnabledWebChromeClient.java:132)
05-03 13:41:50.064: E/AndroidRuntime(19292): at android.webkit.HTML5VideoFullscreen.enterFullscreen(HTML5VideoFullscreen.java:239)
05-03 13:41:50.064: E/AndroidRuntime(19292): at android.webkit.HTML5VideoView.enterFullscreenVideoState(HTML5VideoView.java:544)
05-03 13:41:50.064: E/AndroidRuntime(19292): at android.webkit.HTML5VideoViewProxy$VideoPlayer.enterFullscreenVideo(HTML5VideoViewProxy.java:182)
05-03 13:41:50.064: E/AndroidRuntime(19292): at android.webkit.HTML5VideoViewProxy.handleMessage(HTML5VideoViewProxy.java:479)
05-03 13:41:50.064: E/AndroidRuntime(19292): at android.os.Handler.dispatchMessage(Handler.java:99)
.
.
.
my activity is here:
MainActivity.java
package com.zapkolik.mayis;
import android.os.Bundle;
import android.app.Activity;
import android.app.AlertDialog;
import android.text.method.HideReturnsTransformationMethod;
import android.view.Menu;
import android.view.View;
import android.view.ViewGroup;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.Toast;
public class MainActivity extends Activity {
private VideoEnabledWebView webView;
private VideoEnabledWebChromeClient webChromeClient;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Save the web view
webView = (VideoEnabledWebView) findViewById(R.id.webView);
// Initialize the VideoEnabledWebChromeClient and set event handlers
View nonVideoLayout = findViewById(R.id.nonVideoLayout); // Your own view, read class comments
ViewGroup videoLayout = (ViewGroup) findViewById(R.layout.video_layout); // Your own view, read class comments
View loadingView = getLayoutInflater().inflate(R.layout.loading_video, null);
// Your own view, read class comments
final Activity activity = this;
webChromeClient = new VideoEnabledWebChromeClient(nonVideoLayout, videoLayout, loadingView, webView) // See all available constructors...
{
// Subscribe to standard events, such as onProgressChanged()...
#Override
public void onProgressChanged(WebView view, int progress)
{
activity.setProgress(progress * 1000);
}
};
webChromeClient.setOnToggledFullscreen(new VideoEnabledWebChromeClient.ToggledFullscreenCallback()
{
#Override
public void toggledFullscreen(boolean fullscreen)
{
// AlertDialog alertDialog = new AlertDialog.Builder(activity).create(); //Read Update
// alertDialog.setTitle("hi");
// alertDialog.setMessage("this is my app");
}
});
webView.setWebChromeClient(webChromeClient);
webView.setWebViewClient(new WebViewClient());
// Navigate everywhere you want, this classes have only been tested on YouTube's mobile site
webView.loadUrl("http://m.youtube.com");
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
#Override
public void onBackPressed()
{
// Notify the VideoEnabledWebChromeClient, and handle it ourselves if it doesn't handle it
if (!webChromeClient.onBackPressed())
{
if (webView.canGoBack())
{
webView.goBack();
}
else
{
// Close app (presumably)
super.onBackPressed();
}
}
}
}
and layouts are:
activity_main.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/nonVideoLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity" >
<com.zapkolik.mayis.VideoEnabledWebView
android:id="#+id/webView"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</RelativeLayout>
loading_video.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"
android:layout_margin="#dimen/center"
android:gravity="center|center_vertical"
android:orientation="vertical" >
<ProgressBar
android:id="#+id/progressBar1"
style="?android:attr/progressBarStyleLarge"
android:layout_width="208dp"
android:layout_height="0dip"
android:layout_weight="0.39" />
</LinearLayout>
video_layout.xml
<?xml version="1.0" encoding="utf-8"?>
<VideoView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/videoView1"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
I could not figure out why I am getting errors when I tap the fullscreen button.

Android listView within tabwidget activity gives exception

Using Google's "Hello TabWidget" tutorial found here I was able to build a tabWidget layout, but I'm having a seriously difficult time trying to stick a listview within one of the tabs.
I've also read through a couple threads here about this issue, but they all pertain to assigning the ListView to the specific id "list", which I have done, but I still get a null pointer exception.
I have spent wayyyyyyy to much time on this elementary objective, so I've asked God for help, but I think he's busy right now. Anyone on a lunch break?
the activity in which the ListView is called
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.AdapterView.OnItemClickListener;
public class SettingsActivity extends Activity {
private ListView lv;
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.settings);
String[] settingsList = getResources().getStringArray(R.array.settingsStringArray);
lv = (ListView) findViewById(R.id.list);
lv.setAdapter(new ArrayAdapter<String>(this, R.id.TextView01, settingsList));
lv.setOnItemClickListener
(new OnItemClickListener()
{
public void onItemClick(AdapterView<?> parent, View view,int position, long id)
{
Toast.makeText(getApplicationContext(), ((TextView) view).getText(),Toast.LENGTH_SHORT).show();
}
});
}
}
xml file from res/layout called within setContentView(R.layout.settings)
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<ListView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="#+id/list">
</ListView>
<TextView
android:id="#+id/TextView01"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
</TextView>
</LinearLayout>
Thanks, everyone.
Your TextView01 needs to go into a separate XML file, say textview01. (The name needs to be lowercase, although the only error message you'll see about this will be in the console.) You would then access it as R.layout.textview01.

Categories

Resources