So here are my codes :
public class AboutActivity extends Activity{
WebView aboutwebview;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.edit_profile_settings);
aboutwebview = (WebView) findViewById(R.id.aboutwebview);
aboutwebview.setHorizontalScrollBarEnabled(false);
aboutwebview.loadUrl("file:///android_asset/pickld.html");
}
}
and here is the xml file :
<?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:orientation="vertical" >
<LinearLayout
android:id="#+id/header"
android:layout_width="match_parent"
android:layout_height="45dp"
android:background="#7fad33"
android:gravity="center"
android:orientation="horizontal" >
<TextView
android:id="#+id/tv0"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:text="Our Team"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textColor="#fff" />
</LinearLayout>
<WebView
android:id="#+id/aboutwebview"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
and im pretty sure that the html file is on the assets folder, i also put a permission on the manifest file, but when i run the application, im getting a force close error its says that the problem is on this line :
aboutwebview.loadUrl("file:///android_asset/pickld.html");
im wondering what could be the problem.
here's the logcat error :
If this line causes NPE
aboutwebview.loadUrl("file:///android_asset/pickld.html");
means your aboutwebview is null. Either you refer to the wrong id or you set the wrong layout to your activity with setContentView(R.layout.edit_profile_settings);. Make sure you do that right to avod NPE.
Related
I am working on an Android project and I creating an activity that should use a dialog theme, but it's not displaying correctly.
Below is my activities layout XML
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<TextView android:id="#+id/dialog_title"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textAppearance="#android:style/TextAppearance.DialogWindowTitle"/>
<ScrollView
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView android:id="#+id/dialog_message"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
</ScrollView>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
style="#android:style/DeviceDefault.ButtonBar.AlertDialog">
<Button android:id="#+id/btnCopyToClipbard"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Copy to Clipboard"
style="#android:style/Widget.DeviceDefault.Button.Borderless"/>
<Button android:id="#+id/btnClose"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Close"
style="#android:style/Widget.DeviceDefault.Button.Borderless"/>
</LinearLayout>
</LinearLayout>
Below is how my activity is defined
<activity
android:name=".CustomAlertDialogActivity"
android:theme="#style/Theme.AppCompat.Light.Dialog">
</activity>
The activity class is as follows
public class CustomAlertDialogActivity extends BaseActionBarActivity
{
TextView txtDialogTitle;
TextView txtDialogMessage;
Bundle bundle;
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.custom_alert_dialog);
txtDialogTitle = (TextView)findViewById(R.id.dialog_title);
txtDialogMessage = (TextView)findViewById(R.id.dialog_message);
bundle = getIntent().getExtras();
if (bundle != null)
{
txtDialogTitle.setText("An error occurred");
txtDialogMessage.setText("No error was specified");
return;
}
txtDialogTitle.setText(bundle.getString(CustomAlertDialog.DIALOG_TITLE));
txtDialogMessage.setText(bundle.getString(CustomAlertDialog.DIALOG_MESSAGE));
}
}
BaseActionBarActivity is my own class which extends AppCompatActivity
Below is how my alert dialog is shown
In case it makes any difference this activity is within a library project which is being included into my app.
This is how the your Activity looks for me:
(Android 6.0 and Android 4.4 respectively)
To test, I just copied your code into an empty project and, as you see, it works (even though it requires some UI polishing).
My best guess is that you unintentionally do setTheme(), etc. somewhere in super-class BaseActionBarActivity. So as a step 1, replace BaseActionBarActivity with AppCompatActivity and see, if it changes anything.
I read about the new SwitchCompat that has been introduced to implement the Switch widget in Android 5.0. I tried using the same but I am not able to see the drawable thumb image as seen in below image.
My XML code is as follows,
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="5dp">
<android.support.v7.widget.SwitchCompat
android:id="#+id/sampleSwitch"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:showText="false"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginTop="20dp"
android:text="#string/action" />
<TextView
android:id="#+id/switchStatus"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="#+id/sampleSwitch"
android:layout_marginTop="22dp"
android:text="#string/status"
android:textAppearance="?android:attr/textAppearanceMedium" />
</RelativeLayout>
I am able to see the thumb image in preview design (graphical layout tab in eclipse) for the above layout but when I run my code I dont see the image.
Preview design
This is the exception I get
java.lang.NullPointerException: Attempt to invoke virtual method 'boolean android.graphics.drawable.Drawable.getPadding(android.graphics.Rect)' on a null object reference
Please can someone help solve the problem?
This is a known issue and you should provide the thumb and track:
android:thumb="#drawable/thumb"
android:track="#drawable/bg"
or
SwitchCompat switchCompat = (SwitchCompat)findViewById(R.id.sampleSwitch);
switchCompat.setThumbResource(R.drawable.apptheme_switch_thumb_holo_light);
switchCompat.setTrackResource(R.drawable.apptheme_switch_track_holo_light);
you can use this link to customize it.
Layout:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="5dp">
<android.support.v7.widget.SwitchCompat
android:id="#+id/sampleSwitch"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginTop="20dp"
android:textOff="OFF"
android:textOn="ON"
android:text="Toggle Me"
android:clickable="true"
android:checked="true" />
</RelativeLayout>
And the codes
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
SwitchCompat switchCompat = (SwitchCompat)findViewById(R.id.sampleSwitch);
switchCompat.setThumbResource(R.drawable.apptheme_switch_thumb_holo_light);
switchCompat.setTrackResource(R.drawable.apptheme_switch_track_holo_light);
}
}
It's a bug with corrupt file in drawable-hdpi on AppCompat
https://code.google.com/p/android/issues/detail?id=78262
To fix it, juste override it with this 2 files
https://github.com/lopespm/quick-fix-switchcompat-resources
Add it on your directory drawable-hdpi
XML
<android.support.v7.widget.SwitchCompat
android:id="#+id/dev_switch_show_dev_only"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
And nothing was necessary on Java
I'm developing an Android 3.1. and above application.
On one activity I generate its layout dynamically. I'm using formdetail.xml as contentview:
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.formdetail);
Bundle extras = getIntent().getExtras();
if (extras != null)
{
currentFormId = extras.getString("FormId");
}
}
formdetail.xml :
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/detailLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
<Button
android:id="#+id/downloadFormsButton"
android:enabled="false"
android:layout_alignParentLeft="true"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:text="#string/download_forms_button" />
<TextView
android:id="#+id/formErrorMsg"
android:layout_alignParentRight="true"
android:layout_alignParentBottom="true"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="10dp"
android:textSize="16dp" >
</TextView>
</RelativeLayout>
</LinearLayout>
downloadFormsButton and formErrorMsg must be always in form.
But using that xml I get this warning:
This RelativeLayout layout or its LinearLayout parent is useless
I need linearLayout to add TableLayouts programmatically.
How can I solve this warning?
Lint is correct in it's warning, your LinearLayout isn't doing anything useful, since its only child is a RelativeLayout. Remove the LinearLayout:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/detailLayout"
android:layout_width="match_parent"
android:layout_height="match_parent">
<Button android:id="#+id/downloadFormsButton"
android:enabled="false"
android:layout_alignParentLeft="true"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:text="#string/download_forms_button" />
<TextView
android:id="#+id/formErrorMsg"
android:layout_alignParentRight="true"
android:layout_alignParentBottom="true"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="10dp"
android:textSize="16dp" />
</RelativeLayout>
You either ignore it or right click your project. Click Build Path->Configure Build Path.... Under Android Lint Preferences look for UselessParent and set it's severity to ignore or click Ignore All.
EDIT:
I take that last part back. I think Ignore All will wipe all Lint warnings and errors.
I was also having the same issue with my app layout.
I don't know how and it is right way or not, but this get resolved after setting background attribute to both layouts.
Now there is no warning and working perfect. Hope it will also work for you.
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:background="#drawable/bg"
android:layout_height="match_parent" >
<FrameLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_marginBottom="10dp"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:background="#drawable/bg2"
android:layout_marginTop="10dp" >
<ImageView
android:id="#+id/img_BookImage"
android:layout_width="200dp"
android:layout_height="200dp"
android:scaleType="fitXY"
android:contentDescription="#string/India"
android:src="#drawable/india" />
</FrameLayout>
</FrameLayout>
That is just an warning from lint. Lint doesn't know you would later append views to this layout and it will warn you that you added an unnecessary extra view(increasing your layout depth).
You should just ignore the warning(If it really bothers you here is a link on how to remove warnings from lint).
I have my main.xml up and in the XML code I have:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#ffffff">
<ImageView android:id="#+id/imageView1" android:layout_height="wrap_content" android:src="#drawable/logo" android:layout_width="wrap_content" android:layout_alignParentTop="true" android:layout_centerHorizontal="true" android:layout_marginTop="116dp"></ImageView>
<TextView android:layout_height="wrap_content" android:text="Administration 0.0.1" android:textAppearance="?android:attr/textAppearanceSmall" android:id="#+id/textView1" android:layout_width="wrap_content" android:layout_below="#+id/imageView1" android:layout_centerHorizontal="true"></TextView>
</RelativeLayout>
But when I run the app in an emulator, the default theme still shows with the black background. I can't figure out why it's doing this.
Any ideas?
What background color are you expecting? It shows up correctly for me if you're expecting white. This is what the screen looks like (I have substituted your image with the android icon)
Does your onCreate() look like this -
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
I declared a ImageView on my layout (XML) but when I try to grab it by the ID I'm getting a TextField!?! Then my code throw a ClassCastException.
Here is my layout code (splash.xml):
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
style="#android:style/Theme.Light" android:layout_height="wrap_content" android:background="#drawable/bg">
<ImageView android:layout_height="wrap_content" android:layout_width="match_parent" android:src="#drawable/splash" android:layout_marginTop="20pt" android:id="#+id/splashLogo"></ImageView>
<TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="#string/app_name" android:id="#+id/splashTitle" android:textStyle="bold" android:textSize="#dimen/title_size" android:layout_gravity="center" android:lineSpacingExtra="#dimen/spacer" android:gravity="center"></TextView>
<TextView android:id="#+id/splashCopyleft" android:textSize="#dimen/version_size" android:lineSpacingExtra="#dimen/version_spacing" android:layout_width="wrap_content" android:gravity="center" android:layout_height="wrap_content" android:text="#string/app_version_info" android:layout_gravity="center" android:textColor="#color/version"></TextView>
</LinearLayout>
Here is my Java (Activity) code:
// imports
public class SplashActivity extends Activity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.splash);
// Grab a ClassCastException here
ImageView logo = (ImageView) findViewById(R.id.splashLogo);
// Never get here :'(
Log.i("GOT", logo.toString());
}
}
Sometimes Eclipse shifts resource ids. Try cleaning your project and building it again.
First, clean your project and then build it.
If the problem persists, close Eclipse and re-open it.