I'm a new android programmer and recently, I'm getting this error in the logcat:
03-22 15:33:49.395 13639-13639/com.abaco.awser E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.abaco.awser, PID: 13639
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.abaco.awser/com.abaco.awser.MainActivity}: java.lang.NullPointerException
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2429)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2493)
at android.app.ActivityThread.access$800(ActivityThread.java:166)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1283)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5584)
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:1268)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1084)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.NullPointerException
at com.abaco.awser.MainActivity.onCreate(MainActivity.java:23)
at android.app.Activity.performCreate(Activity.java:5447)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1094)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2393)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2493)
at android.app.ActivityThread.access$800(ActivityThread.java:166)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1283)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5584)
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:1268)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1084)
at dalvik.system.NativeStart.main(Native Method)
Here's my MainActivity.java:
package com.abaco.awser;
import android.content.pm.ActivityInfo;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Window;
import android.view.WindowManager;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
public class MainActivity extends AppCompatActivity {
#Override
public void onBackPressed() {
}
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
WebView myWebView = (WebView) findViewById(R.id.activity_main_webview);
myWebView.setWebViewClient(new WebViewClient());
myWebView.loadUrl("http://primopizza.com.br/eloja");
WebSettings webSettings = myWebView.getSettings();
webSettings.setJavaScriptEnabled(true);
setContentView(R.layout.activity_main);
}
}
And here's my activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.abaco.abawser.MainActivity">
<WebView
android:id="#+id/activity_main_webview"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</RelativeLayout>
Can you please help me solving this error? I already tried everything.
You are getting NPE because your myWebView is null
place this
WebView myWebView = (WebView)findViewById(R.id.activity_main_webview);
after
setContentView(R.layout.activity_main);
Because you can actually retrive your views only after setting content View
Related
I am not getting the up button(in fact activity_order activity doesn't start).It crashes when i click the icon (+) in MainActivity appbar
I have this intuition that there is something wrong with
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
This is the second activity
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.Toolbar;
public class activity_order extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_order);
Toolbar toolbar=(Toolbar) findViewById(R.id.toolbar_main);
setSupportActionBar(toolbar);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
}
}
This is the main/parent activity
import android.content.Intent;
import android.support.v4.view.MenuItemCompat;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.ShareActionProvider;
import android.support.v7.widget.Toolbar;
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);
Toolbar toolbar= findViewById(R.id.toolbar_main);
setSupportActionBar(toolbar);
}
#Override
public boolean onCreateOptionsMenu(Menu menu)
{
getMenuInflater().inflate(R.menu.menu_main,menu);
return super.onCreateOptionsMenu(menu);
}
#Override
public boolean onOptionsItemSelected(MenuItem item)
{
if(R.id.action_create_order==item.getItemId()) {
Intent intent = new Intent(this, activity_order.class);
startActivity(intent);
return true;
}
return super.onOptionsItemSelected(item);
}
}
Error Log
2019-06-18 22:51:49.374 4127-4147/com.example.orderpizza I/Adreno: QUALCOMM build : f47ef91, I09d312ff84
Build Date : 07/24/17
OpenGL ES Shader Compiler Version: XE031.09.00.04
Local Branch :
Remote Branch : refs/tags/AU_LINUX_ANDROID_LA.UM.5.5.R1.07.01.02.269.046
Remote Branch : NONE
Reconstruct Branch : NOTHING
2019-06-18 22:51:49.383 4127-4147/com.example.orderpizza I/OpenGLRenderer: Initialized EGL, version 1.4
2019-06-18 22:51:49.383 4127-4147/com.example.orderpizza D/OpenGLRenderer: Swap behavior 1
2019-06-18 22:51:49.418 4127-4127/com.example.orderpizza W/art: Before Android 4.1, method int android.support.v7.widget.DropDownListView.lookForSelectablePosition(int, boolean) would have incorrectly overridden the package-private method in android.widget.ListView
2019-06-18 22:51:52.150 4127-4127/com.example.orderpizza V/BoostFramework: BoostFramework() : mPerf = com.qualcomm.qti.Performance#7d39c7c
2019-06-18 22:51:52.269 4127-4127/com.example.orderpizza I/Timeline: Timeline: Activity_launch_request time:28140110 intent:Intent { cmp=com.example.orderpizza/.activity_order }
2019-06-18 22:51:52.317 4127-4127/com.example.orderpizza D/AndroidRuntime: Shutting down VM
2019-06-18 22:51:52.318 4127-4127/com.example.orderpizza E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.orderpizza, PID: 4127
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.orderpizza/com.example.orderpizza.activity_order}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.support.v7.app.ActionBar.setDisplayHomeAsUpEnabled(boolean)' on a null object reference
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2757)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2818)
at android.app.ActivityThread.-wrap12(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1557)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:163)
at android.app.ActivityThread.main(ActivityThread.java:6393)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:933)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:823)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.support.v7.app.ActionBar.setDisplayHomeAsUpEnabled(boolean)' on a null object reference
at com.example.orderpizza.activity_order.onCreate(activity_order.java:16)
at android.app.Activity.performCreate(Activity.java:6858)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1119)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2710)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2818)
at android.app.ActivityThread.-wrap12(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1557)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:163)
at android.app.ActivityThread.main(ActivityThread.java:6393)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:933)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:823)
2019-06-18 22:51:52.337 4127-4127/com.example.orderpizza I/Process: Sending signal. PID: 4127 SIG: 9
Please help as I am not getting why is it showing a nullPointerException.
You probably is not using the support library, if you're importing android.app.ActionBar you should user getActionBar() instead. Or just import android.support.v7.app.ActionBar.
To your safety, always check if getSupportActionBar() != null as well
i think your activity_order layout don't have any Toolbar xml with id toolbar_main
please recheck that...
you should at least have a appbarlayout and a toolbar
<android.support.design.widget.AppBarLayout
android:id="#+id/my_appbarLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:elevation="0dp"
android:theme="#style/AppTheme.AppBarOverlay"
app:elevation="0dp">
<android.support.v7.widget.Toolbar
android:id="#+id/toolbar_main"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:layout_margin="0dp"
app:layout_scrollFlags="snap"
app:navigationIcon="#drawable/ic_arrow_back_black_24dp"
app:popupTheme="#style/AppTheme.PopupOverlay"
app:theme="#style/ThemeToolbar.default" />
</android.support.design.widget.AppBarLayout>
I just developed my first android web browser project,
but it didn't go well.
I found there's some crash on my MainActivity,
But I can't find it.
This is my java code, from my MainActivity.
package org.thunderdot.station;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
EditText editText_search = (EditText) findViewById(R.id.EditText_Uri);
WebView webView = (WebView) findViewById(R.id.webview);
ImageButton button_search = (ImageButton) findViewById(R.id.ImageButton_Search);
ImageButton button_more = (ImageButton) findViewById(R.id.ImageButton_More);
WebSettings webSettings = webView.getSettings();
String default_url = "https://www.google.com";
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
webView.setWebViewClient(new WebViewClient());
webSettings.setJavaScriptEnabled(true);
webSettings.setLoadsImagesAutomatically(true);
webView.loadUrl(default_url);
}
public void onClick_button_search() {
String url = editText_search.getText().toString();
if(url.isEmpty()) {
Toast.makeText(getApplicationContext(), "Please Input URL First!", Toast.LENGTH_SHORT).show();
}
else {
webView.loadUrl(url);
}
}
And Here's my activity_main.xml source.
<?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="org.thunderdot.station.MainActivity"
android:focusable="true"
android:focusableInTouchMode="true">
<LinearLayout
android:layout_width="368dp"
android:layout_height="60dp"
tools:layout_editor_absoluteY="8dp"
tools:layout_editor_absoluteX="8dp"
android:gravity="center"
android:id="#+id/LinerLayout_Search" >
<EditText
android:id="#+id/EditText_Uri"
android:layout_width="210dp"
android:layout_height="50dp"
android:hint="Input Url or Text"
android:inputType="textUri"
android:onClick="onClick_button_search"/>
<Space
android:layout_width="5dp"
android:layout_height="0dp" />
<ImageButton
android:layout_width="60dp"
android:layout_height="60dp"
android:src="#mipmap/ic_launcher"
android:id="#+id/ImageButton_Search"/>
<Space
android:layout_width="5dp"
android:layout_height="0dp" />
<ImageButton
android:layout_width="60dp"
android:layout_height="60dp"
android:src="#mipmap/ic_launcher"
android:id="#+id/ImageButton_More"/>
</LinearLayout>
<WebView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="#id/LinerLayout_Search"
android:id="#+id/webview"> </WebView>
</RelativeLayout>
And here's my logcat, too.
05-31 12:20:14.241 2709-2709/org.thunderdot.station I/art: Not late-enabling -Xcheck:jni (already on)
05-31 12:20:14.241 2709-2709/org.thunderdot.station W/art: Unexpected CPU variant for X86 using defaults: x86
05-31 12:20:14.431 2709-2709/org.thunderdot.station W/System: ClassLoader referenced unknown path: /data/app/org.thunderdot.station-1/lib/x86
05-31 12:20:14.441 2709-2709/org.thunderdot.station I/InstantRun: starting instant run server: is main process
05-31 12:20:14.542 2709-2709/org.thunderdot.station W/art: Before Android 4.1, method android.graphics.PorterDuffColorFilter android.support.graphics.drawable.VectorDrawableCompat.updateTintFilter(android.graphics.PorterDuffColorFilter, android.content.res.ColorStateList, android.graphics.PorterDuff$Mode) would have incorrectly overridden the package-private method in android.graphics.drawable.Drawable
05-31 12:20:14.661 2709-2729/org.thunderdot.station I/OpenGLRenderer: Initialized EGL, version 1.4
05-31 12:20:14.661 2709-2729/org.thunderdot.station D/OpenGLRenderer: Swap behavior 1
05-31 12:20:14.661 2709-2729/org.thunderdot.station W/OpenGLRenderer: Failed to choose config with EGL_SWAP_BEHAVIOR_PRESERVED, retrying without...
05-31 12:20:14.661 2709-2729/org.thunderdot.station D/OpenGLRenderer: Swap behavior 0
05-31 12:21:41.277 2709-2709/org.thunderdot.station D/AndroidRuntime: Shutting down VM
--------- beginning of crash
05-31 12:21:41.278 2709-2709/org.thunderdot.station E/AndroidRuntime: FATAL EXCEPTION: main
Process: org.thunderdot.station, PID: 2709
java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{org.thunderdot.station/org.thunderdot.station.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'android.view.Window$Callback android.view.Window.getCallback()' on a null object reference
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2567)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2726)
at android.app.ActivityThread.-wrap12(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1477)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6119)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'android.view.Window$Callback android.view.Window.getCallback()' on a null object reference
at android.support.v7.app.AppCompatDelegateImplBase.<init>(AppCompatDelegateImplBase.java:120)
at android.support.v7.app.AppCompatDelegateImplV9.<init>(AppCompatDelegateImplV9.java:151)
at android.support.v7.app.AppCompatDelegateImplV11.<init>(AppCompatDelegateImplV11.java:31)
at android.support.v7.app.AppCompatDelegateImplV14.<init>(AppCompatDelegateImplV14.java:55)
at android.support.v7.app.AppCompatDelegateImplV23.<init>(AppCompatDelegateImplV23.java:33)
at android.support.v7.app.AppCompatDelegateImplN.<init>(AppCompatDelegateImplN.java:33)
at android.support.v7.app.AppCompatDelegate.create(AppCompatDelegate.java:201)
at android.support.v7.app.AppCompatDelegate.create(AppCompatDelegate.java:185)
at android.support.v7.app.AppCompatActivity.getDelegate(AppCompatActivity.java:519)
at android.support.v7.app.AppCompatActivity.findViewById(AppCompatActivity.java:190)
at org.thunderdot.station.MainActivity.<init>(MainActivity.java:14)
at java.lang.Class.newInstance(Native Method)
at android.app.Instrumentation.newActivity(Instrumentation.java:1078)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2557)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2726)
at android.app.ActivityThread.-wrap12(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1477)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6119)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776)
And here's the java source that calls MainActivity.
package org.thunderdot.station;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class TitleScreen extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_title_screen);
Button button = (Button)findViewById(R.id.Button);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent intent = new Intent(view.getContext(), MainActivity.class);
startActivity(intent);
}
});
}
}
package com.example.cyber.sayac_say;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.content.SharedPreferences;
import android.preference.PreferenceManager;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.RelativeLayout;
public class MainActivity extends AppCompatActivity implements SharedPreferences.OnSharedPreferenceChangeListener {
int count;
Button btn;
SharedPreferences ayarlar = null;
RelativeLayout arkaplan;
Boolean ses_durumu, titresim_durumu;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
ayarlar = PreferenceManager.getDefaultSharedPreferences(MainActivity.this);
ayarlariYukle();
btn = (Button) findViewById(R.id.button);
arkaplan = (RelativeLayout) findViewById(R.id.rl);
btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
count++;
btn.setText(""+count);
}
});
}
hi , this is my codes. And when I used Shared Preferences so I have this error.But when I erase shared preferences lines so error is gone.
The problem is about Shared Prefenrences.I definitely sure.
What can I do before crazy:)?
03-22 14:16:23.972 6613-6613/com.example.cyber.sayac_say
E/AndroidRuntime: FATAL EXCEPTION: main java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.cyber.sayac_say/com.example.cyber.sayac_say.MainActivity}: java.lang.NullPointerException
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
at android.app.ActivityThread.access$600(ActivityThread.java:141)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:5041)
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:793)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.NullPointerException
at com.example.cyber.sayac_say.MainActivity.ayarlariYukle(MainActivity.java:76)
at com.example.cyber.sayac_say.MainActivity.onCreate(MainActivity.java:36)
at android.app.Activity.performCreate(Activity.java:5104)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
at android.app.ActivityThread.access$600(ActivityThread.java:141)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:5041)
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:793)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
at dalvik.system.NativeStart.main(Native Method)
I'm trying to make a simple web browser for my android development class and every time I try to run the app I get the message "Unfortunately, Navegador has stopped".
I followed my teacher's code and everyone in my class could run it without problems. I'm starting to think the problem might be on my computer.
I'm using Eclipse. Some variables on the code are in spanish.
Here's my activity_main.xml
Basically it has 4 buttons
Atras (Backward)
Adelante (Forward)
Actualizar (Refresh)
Ir (Go to website)
<Button
android:id="#+id/btAtras"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="top"
android:layout_weight="3"
android:text="Atras"
/>
<Button
android:id="#+id/btAdelante"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="top"
android:layout_weight="3"
android:text="Adelante"
/>
<Button
android:id="#+id/btActualizar"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="top"
android:layout_weight="3"
android:text="Actualizar"
/>
Here's my MainActivity.java
package com.curso.navegador;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.webkit.WebView;
import android.widget.Button;
import android.widget.EditText;
public class MainActivity extends Activity implements OnClickListener{
EditText navegador;
Button ir, atras, adelante, actualizar, borrar;
WebView sitio;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
sitio = (WebView) findViewById (R.id.wvNavegador);
sitio.setWebViewClient(new ViewClient());
sitio.getSettings().setJavaScriptEnabled(true);
sitio.getSettings().setLoadWithOverviewMode(true);
sitio.getSettings().setUseWideViewPort(true);
navegador = (EditText) findViewById (R.id.wvNavegador);
ir =(Button) findViewById (R.id.btIr);
atras =(Button) findViewById (R.id.btAtras);
adelante =(Button) findViewById (R.id.btAdelante);
actualizar = (Button) findViewById (R.id.btActualizar);
ir.setOnClickListener(this);
atras.setOnClickListener(this);
adelante.setOnClickListener(this);
actualizar.setOnClickListener(this);
borrar.setOnClickListener(this);
sitio.loadUrl("");
}
#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 onClick(View v) {
// TODO Auto-generated method stub
switch(v.getId()){
case R.id.btIr:
String web = navegador.getText().toString();
sitio.loadUrl(web);
break;
case R.id.btAtras:
if(sitio.canGoBack())
sitio.goBack();
break;
case R.id.btAdelante:
if(sitio.canGoForward())
sitio.goForward();
break;
case R.id.btActualizar:
sitio.reload();
break;
}
}
}
And we added this additional ViewClient.java class
package com.curso.navegador;
import android.webkit.WebView;
import android.webkit.WebViewClient;
public class ViewClient extends WebViewClient{
public boolean shouldOverrideUrlLading(WebView v, String url){
v.loadUrl(url);
return true;
}
}
In addition I added the permission on android manifest
android.permission.INTERNET
03-11 12:07:15.289: E/AndroidRuntime(27901): FATAL EXCEPTION: main
03-11 12:07:15.289: E/AndroidRuntime(27901): Process: com.curso.navegador, PID: 27901
03-11 12:07:15.289: E/AndroidRuntime(27901): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.curso.navegador/com.curso.navegador.MainActivity}: java.lang.ClassCastException: android.webkit.WebView cannot be cast to android.widget.EditText
03-11 12:07:15.289: E/AndroidRuntime(27901): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2426)
03-11 12:07:15.289: E/AndroidRuntime(27901): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2490)
03-11 12:07:15.289: E/AndroidRuntime(27901): at android.app.ActivityThread.-wrap11(ActivityThread.java)
03-11 12:07:15.289: E/AndroidRuntime(27901): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1354)
03-11 12:07:15.289: E/AndroidRuntime(27901): at android.os.Handler.dispatchMessage(Handler.java:102)
03-11 12:07:15.289: E/AndroidRuntime(27901): at android.os.Looper.loop(Looper.java:148)
03-11 12:07:15.289: E/AndroidRuntime(27901): at android.app.ActivityThread.main(ActivityThread.java:5443)
03-11 12:07:15.289: E/AndroidRuntime(27901): at java.lang.reflect.Method.invoke(Native Method)
03-11 12:07:15.289: E/AndroidRuntime(27901): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:728)
03-11 12:07:15.289: E/AndroidRuntime(27901): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)
03-11 12:07:15.289: E/AndroidRuntime(27901): Caused by: java.lang.ClassCastException: android.webkit.WebView cannot be cast to android.widget.EditText
03-11 12:07:15.289: E/AndroidRuntime(27901): at com.curso.navegador.MainActivity.onCreate(MainActivity.java:30)
03-11 12:07:15.289: E/AndroidRuntime(27901): at android.app.Activity.performCreate(Activity.java:6245)
03-11 12:07:15.289: E/AndroidRuntime(27901): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1130)
03-11 12:07:15.289: E/AndroidRuntime(27901): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2379)
03-11 12:07:15.289: E/AndroidRuntime(27901): ... 9 more
It's getting really frustrating because I'm getting this same type of error every time I start a new android application and this didn't happen before and I haven't found a solution yet.
I would appreciate very much your help.
The stack trace is telling you what the problem is. You are trying to cast a WebView to an EditText. Based on what you have provided its this line
navegador = (EditText) findViewById (R.id.wvNavegador);
You are first casting wvNavegador to a webview few lines above and then to an edit text, which is obviously a problem.
This question already exists:
android.view.InflateException: Binary XML file line #16: Error inflating class fragment
Closed 6 years ago.
I made an app, it works well in virtual device and real device, but today my app is throwing exceptions, maybe it is the same with some old errors in here, but I can not find my own answer for this. So, please tell me how to solve it, thanks so much
here is my logcat
FATAL EXCEPTION: main
Process: com.gvc.tvschedule, PID: 1918
android.view.InflateException: Binary XML file line #16: Error inflating class <unknown>
at android.view.LayoutInflater.createView(LayoutInflater.java:620)
at com.android.internal.policy.impl.PhoneLayoutInflater.onCreateView(PhoneLayoutInflater.java:56)
at android.view.LayoutInflater.onCreateView(LayoutInflater.java:669)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:694)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:755)
at android.view.LayoutInflater.inflate(LayoutInflater.java:492)
at android.view.LayoutInflater.inflate(LayoutInflater.java:397)
at android.widget.SimpleAdapter.createViewFromResource(SimpleAdapter.java:121)
at android.widget.SimpleAdapter.getView(SimpleAdapter.java:114)
at android.widget.AbsListView.obtainView(AbsListView.java:2255)
at android.widget.ListView.measureHeightOfChildren(ListView.java:1263)
at android.widget.ListView.onMeasure(ListView.java:1175)
at android.view.View.measure(View.java:16497)
at android.widget.RelativeLayout.measureChild(RelativeLayout.java:689)
at android.widget.RelativeLayout.onMeasure(RelativeLayout.java:473)
at android.view.View.measure(View.java:16497)
at android.widget.RelativeLayout.measureChildHorizontal(RelativeLayout.java:719)
at android.widget.RelativeLayout.onMeasure(RelativeLayout.java:455)
at android.view.View.measure(View.java:16497)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5125)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
at android.view.View.measure(View.java:16497)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5125)
at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1404)
at android.widget.LinearLayout.measureVertical(LinearLayout.java:695)
at android.widget.LinearLayout.onMeasure(LinearLayout.java:588)
at android.view.View.measure(View.java:16497)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5125)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2291)
at android.view.View.measure(View.java:16497)
at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:1912)
at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1109)
at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1291)
at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:996)
at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:5600)
at android.view.Choreographer$CallbackRecord.run(Choreographer.java:761)
at android.view.Choreographer.doCallbacks(Choreographer.java:574)
at android.view.Choreographer.doFrame(Choreographer.java:544)
at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:747)
at android.os.Handler.handleCallback(Handler.java:733)
at android.os.Handler.dispatchMessage(Handler.java:95)
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.reflect.InvocationTargetException
at java.lang.reflect.Constructor.constructNative(Native Method)
at java.lang.reflect.Constructor.newInstance(Constructor.java:423)
at android.view.LayoutInflater.createView(LayoutInflater.java:594)
... 48 more
Caused by: android.content.res.Resources$NotFoundException: Resource is not a Drawable (color or path): TypedValue{t=0x12/d=0x0 a=2 r=0x7f0b0068}
at android.content.res.Resources.loadDrawable(Resources.java:2073)
at android.content.res.TypedArray.getDrawable(TypedArray.java:602)
at android.widget.TextView.<init>(TextView.java:806)
at android.widget.TextView.<init>(TextView.java:618)
... 51
here under is Layout XML: getprogram_main.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<RelativeLayout
android:id="#+id/relativeLayoutProgram"
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:layout_alignParentLeft="true"
android:layout_below="#+id/relativeLayoutProgram"
android:orientation="vertical"
android:layout_marginTop="40dp">
<ListView
android:id="#android:id/list"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true">
</ListView>
<!-- #android:id/list or #id/android:list -->
</RelativeLayout>
</RelativeLayout>
and: view_program_entry.xml
<?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="wrap_content"
android:orientation="horizontal" >
<TextView
android:id="#+id/programtime"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="20sp"
android:paddingTop="15sp"
android:paddingLeft="6sp"
android:textStyle="bold"/>
<TextView
android:id="#+id/programtitle"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textSize="25sp"
android:textStyle="bold"
android:drawableLeft="#+id/programtime" />
<!-- android:background="#color/blue2" -->
</LinearLayout>
and Java : ProgramPickerActivity.java
package com.gvc.tvschedule;
import java.util.ArrayList;
import java.util.HashMap;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import com.gvc.service.DBController;
import com.loopj.android.http.AsyncHttpClient;
import com.loopj.android.http.AsyncHttpResponseHandler;
import com.loopj.android.http.RequestParams;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.ProgressDialog;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.TextView;
import android.widget.Toast;
public class ProgramPickerActivity extends Activity {
// DB Class to perform DB related operations
DBController controller = new DBController(this);
// Progress Dialog Object
ProgressDialog prgDialog;
HashMap<String, String> queryValues;
private static String titleTextFromView;
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.getprogram_main);
ArrayList<HashMap<String, String>> programList = controller.generalProgram(MainActivity.getNameOfChannel(), DatePickerActivity.getDate());
if (programList.size() != 0) {
// Set the User Array list in ListView
ListAdapter adapter = new SimpleAdapter(getApplicationContext(), programList, R.layout.view_program_entry, new String[] {
"ptime", "ptitle" }, new int[] { R.id.programtime, R.id.programtitle });
ListView myList = (ListView) findViewById(android.R.id.list);
myList.setAdapter(adapter);
myList.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
// TODO Auto-generated method stub
TextView titleVIew = (TextView) view.findViewById(R.id.programtitle);
titleTextFromView = titleVIew.getText().toString();
createPopupWindownForDescription(titleTextFromView);
}
});
}
prgDialog = new ProgressDialog(this);
prgDialog.setMessage("loading...");
prgDialog.setCancelable(false);
}
private void createPopupWindownForDescription(String pTitle){
String content = controller.getDescription(pTitle);
AlertDialog.Builder builder = new AlertDialog.Builder(ProgramPickerActivity.this);
builder.setTitle("Content");
builder.setMessage(content);
builder.show();
}
#Override
protected void onStop() {
// TODO Auto-generated method stub
super.onStop();
if(prgDialog!=null)
prgDialog.dismiss();
}
public static String getTitleTextFromView(){
return titleTextFromView;
}
}
I think the trouble is related with the listview, because, when no data put on listview, it is fine, but with data, it threw the errors.
I believe your current problem is
<TextView
android:id="#+id/programtitle"
...
android:drawableLeft="#+id/programtime" /> // HERE
You are referencing the other TextView but you should be referencing a Drawable. My guess is that you want to put it to the left of the other TextView?
If this is the case, the LinearLayout will lay them out from left to right by default so you just need to put them in the order that you want them to appear. Also, not a problem but since they are left to right by default, there's no need for
android:orientation="horizontal"
It's not showing when you don't have any items in your ListView because the layout is never inflated so the error is never caught.
Docs
android:id="#+id/list"
use that, you're welcome
PS: Change
ListView myList = (ListView) findViewById(android.R.id.list);
for:
ListView myList = (ListView) findViewById(R.id.list);