I am creating a ViewFlipper that flips thru WebViews: I have no problems running the app if I place the WebViews within the main.xml. Since I will be using a multiple count of Web views, I decided to break them up into separate XML files. When I do this using the include android:id="#+id/myWebView001" layout="#layout/pg001" within the ViewFlipper of the main.xml, I get an force close when the app starts.
Please look thru the following code and if you have any suggestions for this to work correctly, it will greatly appreciate it. Thnx again!!
main.xml:
<?xml version="1.0" encoding="utf-8"?>
<ViewFlipper xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/ViewFlipper"
android:layout_width="fill_parent" android:layout_height="fill_parent" >
<include android:id="#+id/myWebView001" layout="#layout/pg001" />
</ViewFlipper>
main.java:
package com.aero.ac4313;
import android.app.Activity;
import android.os.Bundle;
public class main extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//set your content view, this will be your layout
setContentView(R.layout.main);
}
}
pg001.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout>
<WebView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/myWebView001" android:layout_width="fill_parent"
android:layout_height="fill_parent" />
</LinearLayout>
Pg001.java:
package com.aero.ac4313;
import android.app.Activity;
import android.os.Bundle;
import android.webkit.WebView;
public class Pg001 extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//set your content view, this will be your layout
setContentView(R.layout.pg001);
WebView mWebView = null;
mWebView = (WebView) findViewById(R.id.myWebView001);
mWebView.getSettings().setJavaScriptEnabled(true);
mWebView.loadUrl("file:///android_asset/pg001.html");
}
}
The error is obviously simple. Your main activity class is null. I believe that he added Pg001.class without linking it with the main activity class. If you did add it on the manifest file then try again.
Related
When adding an (empty activity), the class generates with the appropriate layout file but in the setContentview(R.layout.activity_home) it gives an error.
This is my (empty) class:
package com.test.learnlogin;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
public class HomeActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_home);
}
}
And this is the layout file to refer to:
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
</androidx.constraintlayout.widget.ConstraintLayout>
I am assuming that the name activity_home inside setContentView is of red color.
if yes and you are sure that activity_home file exists in Res>Layout then please Restart Your Android Studio, that All
You'll have to import R to remove that error.
import com.test.R // assuming your_package_name=com.test
at the top.
I'm making an app, now i want to make a button with an image and some text on it.
But every time I click the button my app crashes, android studio does compile however.
This is how i my xml code to make the layout of the screen.
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<RelativeLayout
android:id="#+id/buttonStartTest"
style="#android:style/Widget.Button"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
tools:ignore="MissingConstraints">
<ImageView... />
<TextView.../>
</RelativeLayout>
</android.support.constraint.ConstraintLayout>
java code:
package com.example.user.ballbounceapp;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.support.v7.app.AppCompatActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.RelativeLayout;
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
RelativeLayout StartTest = findViewById(R.id.buttonStartTest);
StartTest.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
TestActivity();
}
});
}
public void TestActivity(){
Intent intent = new Intent(MainActivity.this,TestActivity.class);
startActivity(intent);
}
};
You may have forgot to declare your TestActivity inside your manifest.
More about activities in manifest here : https://developer.android.com/guide/topics/manifest/activity-element
So you will need something like this :
<activity android:name="TestActivity">
If the crash persists, the problem might be in your TestActivity 's OnCreate method.
There's nothing fancy or complicated here. I am just looking to open a new, blank, activity from when I press on the textview. From the projects completed files, I seem to have done it as it wants me to. However, every time I click on the textview to start the intent to open the other activity, it crashes. I cannot seem to find the cause either. In a separate app I've made that send a text string from an edit text field via a button, seems to work fine and that has some other more detailed code to perform that, so I am not entirely sure where the error is arising here.
MainActivity.java
package com.example.android.miwok;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Set the content of the activity to use the activity_main.xml layout file
setContentView(R.layout.activity_main);
}
public void openNumbersList(View view) {
Intent i = new Intent(this, NumbersActivity.class);
startActivity(i);
}
}
NumbersActivity.java
package com.example.android.miwok;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.TextView;
public class NumbersActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_numbers);
}
}
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<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:background="#color/tan_background"
android:orientation="vertical"
tools:context="com.example.android.miwok.MainActivity">
<TextView
android:id="#+id/numbers"
style="#style/CategoryStyle"
android:background="#color/category_numbers"
android:onClick="openNumbersList"
android:text="#string/category_numbers" />
<TextView
android:id="#+id/family"
style="#style/CategoryStyle"
android:background="#color/category_family"
android:text="#string/category_family" />
<TextView
android:id="#+id/colors"
style="#style/CategoryStyle"
android:background="#color/category_colors"
android:text="#string/category_colors" />
<TextView
android:id="#+id/phrases"
style="#style/CategoryStyle"
android:background="#color/category_phrases"
android:text="#string/category_phrases" />
</LinearLayout>
In logcat there's an error showing when the app crashes:
02-20 20:10:26.762 7582-7582/com.example.android.miwok E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.android.miwok, PID: 7582
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.android.miwok/com.example.android.miwok.NumbersActivity}: android.view.InflateException: Binary XML file line #0: Binary XML file line #0: Error inflating class TextView
In your xml layout for the Text view with id numbers add
android:clickable="true"
Only if this attribute is set to true the text view's onclick handler will be called
And also add height and width to all your textviews.. like this
android:height="wrap_content"
android:width="wrap_content"
This must have caused the app crash
There is a post on how to set click listeners to textviews here .
"Error inflating class TextView" means that your xml in the next activity is faulty. Try removing all the views from the 2nd activity's layout, double check the layout and then try it. If it is just the intent launch you wanna confirm, this should work.
I need to be able to touch a TextView and have it open up another page, however, when I run the app and touch the TextView the app crashes.
Here's the code:
<?xml version="1.0" encoding="utf-8"?>
<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:padding= "10dp"
android:background="#drawable/background"
android:orientation="vertical"
tools:context="com.example.android.practiceapp.MainActivity">
<TextView
android:id="#+id/numbers"
style="#style/CategoryStyle"
android:layout_marginBottom="16dp"
android:background="#drawable/numbers"
android:text="#string/category_numbers"
android:onClick="openNumbersList"/>
Here's the java code:
package com.example.android.practiceapp;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void openNumbersList(View view) {
Intent i = new Intent(this, Numbers.class);
startActivity(i);
}
}
Make sure you have the Numbers activity exists and is in your Android.manifest.
All activities must be represented by elements in the manifest file. Any that are not declared there will not be seen by the system and will never be run.
First, you are not initialized your TextView
textView = (TextView)findViewById(R.id.numbers)
Second, as others suggested, add Numbers activity in your Android.manifest
You did not add the clickable attribute, without it, the click handler isn't called. So, add it...
...
<TextView
android:id="#+id/numbers"
style="#style/CategoryStyle"
android:layout_marginBottom="16dp"
android:background="#drawable/numbers"
android:text="#string/category_numbers"
android:onClick="openNumbersList"
android:clickable="true"/>
...
thanks for the responses, i actually ran the debugger and it turns out i had to change my java code in the numbers file from "extends AppCompatActivity" to "extends Activity" like the MainActivity java file
Try inserting
MainActivity.this
instead of
this
like so
Intent i = new Intent(MainActivity.this, Numbers.class);
I'm trying to create a WebView app that shows a meteor web app using Tiago Scolari's sample.
When i load the apk in my phone i see the background changes but the log in button doesn't show.
Anyone has a clue how to make this work?
--Edit:
Adding -
webSettings.setJavaScriptCanOpenWindowsAutomatically(true);
webSettings.setDomStorageEnabled(true);
- shows me the log in button and gets me to the facebook log in.
After logging in using facebook i'm presented with a white screen.
Any more advice?
Some code:
main.xml (layout):
<?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"
android:layout_height="fill_parent">
<WebView android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/mainWebView">
</WebView>
</LinearLayout>
AndroidMobileAppSample.java (java wrapper):
package tscolari.mobile_sample;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
public class AndroidMobileAppSampleActivity extends Activity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
WebView mainWebView = (WebView) findViewById(R.id.mainWebView);
WebSettings webSettings = mainWebView.getSettings();
webSettings.setJavaScriptEnabled(true);
webSettings.setJavaScriptCanOpenWindowsAutomatically(true);
webSettings.setDomStorageEnabled(true);
mainWebView.setWebViewClient(new MyCustomWebViewClient());
mainWebView.setScrollBarStyle(View.SCROLLBARS_INSIDE_OVERLAY);
mainWebView.loadUrl("http://pastime.meteor.com/");
}
private class MyCustomWebViewClient extends WebViewClient {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return false;
}
}
}
oke I just tested it on my 2.3.4 android and yea there is no facebooklogin. Its a WebView problem. Changed the code and still the login dosent appear...