OK, I'm working on the chapter 9 tutorial in Android Boot Camp and I'm having...actually a few issues. The book was written for the older versions of Android Studio but my class is using the latest version. I've done my best to look up tutorials for the latest version but they've become quite rare.
Chapter 9 covers a Master/Detail flow tutorial that some things have worked in and others have not.
Where I stand now is a TextView/WebView issue.
I tried simply converting the WebView to match TextView but then .loadUrl won't work and when I use WebView I get an "unexpected cast error. Layout tag was TextView." And Android studio won't tell me where the layout tag was declared so I'm currently combing through all the files line by line. I'm not certain if this source layout tag is in an .xml, a .java or if I should be looking in the manifest.
I believe this means I have to change the source layout to WebView though I can't find anything in the chapter itself about it except ensuring I have the correct import.
package com.example.bikeandbarge;
import android.app.Activity;
import android.support.design.widget.CollapsingToolbarLayout;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.webkit.WebView;
import android.widget.TextView;
import com.example.bikeandbarge.dummy.DummyContent;
public class ItemDetailFragment extends Fragment {
public static final String ARG_ITEM_ID = "item_id";
private DummyContent.DummyItem mItem;
public ItemDetailFragment() {
}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (getArguments().containsKey(ARG_ITEM_ID)) {
// Load the dummy content specified by the fragment
// arguments. In a real-world scenario, use a Loader
// to load content from a content provider.
mItem = DummyContent.ITEM_MAP.get(getArguments().getString(ARG_ITEM_ID));
Activity activity = this.getActivity();
CollapsingToolbarLayout appBarLayout = (CollapsingToolbarLayout) activity.findViewById(R.id.toolbar_layout);
if (appBarLayout != null) {
appBarLayout.setTitle(mItem.content);
}
}
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.item_detail, container, false);
// Show the dummy content as text in a TextView.
if (mItem.id.equals("1")) {
rootView = inflater.inflate(R.layout.photos, container, false);
}
if (mItem.id.equals("2")) {
rootView = inflater.inflate(R.layout.tour, container, false);
}
// Can not get this to update to WebView-not certain where the layout tab is textView
//if (mItem.id.equals("3")) {
// ((WebView) rootView.findViewById( R.id.item_detail )).loadUrl( mItem.item_url ); }
//Can replace WebView with TextView but won't recognize .loadUrl without WebView
if (mItem.id.equals("3")) {
((WebView) rootView.findViewById( R.id.item_detail )).loadUrl( mItem.item_url );
}
return rootView;
}
}
I would love for this to run with loadUrl actually working.
Neither WebView nor TextView will allow me to run the program. The apk file simply can't be compiled for me to even test it. I'd like to at least get to a point where I can compile the apk file and attempt to run it.
Open the layout file item_detail.xml(or is it fragment_item_detail.xml inn the book?) and change the <TextView>-element to <WebView>. Then the "Unexpected cast error" should go away.
In your webView and manifest add the following code:
AndroidManifest.xml
<application
....
android:usesCleartextTraffic="true"
android:hardwareAccelerated="true"
.....
>
In Your Layout where the webView is
<WebView
....
android:usesCleartextTraffic="true"
....
></WebView>
Related
I am trying to inflate one of my fragments. However, the fragment does not show up in the autosuggest. So I manually entered the name of the fragment. By doing that the fragment name was showing in red text color and also red squiggly lines showing below all of the class names. However, when I run the application, it runs perfectly without any errors.
I have three fragments: fragment_dynamic_android.xml, fragment_dynamic_windows.xml and fragment_dynamic_ios.xml.
I can able to inflate the fragment_dynamic_android.xml in AndroidFragment.java class on onCreateView method.
But when I try to inflate the fragment_dynamic_windows.xml on WindowsFragment.java's onCreateView method.
The red textcolor is showing on
View rootView = inflater.inflate(R.layout.fragment_dynamic_windows, container, false);
The applications run without any error.
package app.tab.simple.fragmenttut;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
public class WindowsFragment extends Fragment {
#Nullable
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_dynamic_windows, container, false);
return rootView;
}
}
After some frustration, Switched off the pc and went to do some stuff. Came again and opened the project, the error was gone. Not sure why android studio behaved like that.
I am trying to implement an app that plays RTMP url which I have done already using libvlc sdk.
Now I want to split the screen more than one part and want to play rtmp url I am facing problem with spliting the screen any suggestions will be appreciated thanks in advance
To Split Android activity , You should use fragment class which allows you to show multiple task dynamically. You can use fragment class .
Steps :
1. Create Fragment class
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.ViewGroup;
public class yourFragmentName extends Fragment {
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.article_view, container, false);
}
}
Create FrameLayout in your activity where you want to show fragment.
<FrameLayout
android:id="#+id/your_placeholder"
android:layout_width="match_parent"
android:layout_height="match_parent">
</FrameLayout>
In your Main Activity , add this layout to fragment
FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
// Replace the contents of the container with the new fragment
ft.replace(R.id.your_placeholder, new FooFragment());
// or ft.add(R.id.your_placeholder, new FooFragment());
// Complete the changes added above
ft.commit();
For the second fragment also repeat same.
For more explanation refer , https://guides.codepath.com/android/Creating-and-Using-Fragments
With the help of fragments you can slit the screen into multiple part and each one will have its own UI
My Android app is simple. It has only 1 activity. I created two layouts for the same activity: one for the portrait position (inside the res/layout folder) and one for the landscape position (inside the res/layout-land folder). I give the code for both at the end of this question.
I have nothing special inside the myActivity.java file, I just inflate the layout:
package com.example.myApp;
import android.app.Activity;
import android.os.Bundle;
public class MCentralActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_myActivity);
}
}
Everything works fine at this point. If I hold my device in the portrait position, the app will call the appropriate XML. It works like a charm too, in case I decide to hold it in the landscape position.
The problem arises when I decide to add a little bit of code to the aforementioned myActivity.java file; it is still really simple!
package com.example.myApp;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageButton;
public class MCentralActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
ImageButton ibPacientes;
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_myActivity);
ibPacientes = (ImageButton)findViewById(R.id.imageButton_Pacientes);
ibPacientes.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
//NOTHING INSIDE HERE!!
}
});
}
}
After implementing that little code, if I decide to go landscape, the App will stop abruptly saying "Unfortunately myApp has stopped". Interesting enough this won't occur if I don't implement onClickListener!
The exact error given by LogCat is as shown:
03-28 13:39:27.870: E/SurfaceFlinger(157): DRAW orientation 1 viewport:(0, 0, 1920, 1080) frame(0, 0, 1920, 1080)
03-28 13:39:15.360: E/SurfaceFlinger(157): STATE orientation 1 viewport:(0, 0, 1920, 1080) frame(0, 0, 1920, 1080)
03-28 13:39:15.360: I/SurfaceFlinger(157): ######## orientation:1, transformOri:4
Don't think the layout XML files has to do much in my problem, but I will copy and paste them in this link (for portrait) and this other link (for landscape), in order to not to make this question excessively long.
Thanks in advance!
In your landscape XML file you have the ImageButton called imageButton_Users instead of imageButton_Pacientes. Rename it and everything should work fine.
You're trying to find a view that's not there and so your app will crash.
I am currently working on a project in which I was updating a ListView's adapter from ArrayAdapter to a Custom CursorAdapter.
I deleted the R.java intentionally to regenerate the R.java file after inserting a new <string></string> to the res/string.xml file since it was not automatically regenerating. The file did not generate.
I continued to debug, and ran a Project->Clean... in attempt to regenerate the R.java file. This did not work either.
After checking StackOverflow, I also tried changing the version in AndroidManifest.xml in hope to regenerate the file. This also did not work. I've tried a few other hacks in hopes something might have Eclipse regenerate the file. No Luck.
Does anyone have any idea? I am on a tight deadline and this is really holding me back.
Here is the file I was working in when the issue arose:
package com.renaissanceartsmedia.gradingapp.controllers.fragments;
import java.util.ArrayList;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.support.v4.app.ListFragment;
import android.support.v4.util.ArrayMap;
import android.support.v4.widget.CursorAdapter;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListView;
import android.widget.TextView;
import com.renaissanceartsmedia.gradingapp.R;
import com.renaissanceartsmedia.gradingapp.controllers.activities.CourseActivity;
import com.renaissanceartsmedia.gradingapp.model.Course;
import com.renaissanceartsmedia.gradingapp.model.CourseStore;
import com.renaissanceartsmedia.gradingapp.model.GradingAppDatabaseHelper.CourseCursor;
public class CourseListFragment extends ListFragment {
// DEBUG
private static final String TAG = "CourseListFragment";
// Create an ArrayList<String> to store flashcards
ArrayList<Course> mCourses;
ArrayMap<Long, Course> mCoursesById;
// Cursor Object
private CourseCursor mCursor;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Set the Title of the Fragment's Activity
getActivity().setTitle(R.string.course_list);
// Load Cursor to DB
mCursor = CourseStore.get(getActivity()).queryCourses();
// Set the list of FlashcardListFragments
mCourses = CourseStore.get(getActivity()).getCourses();
mCoursesById = CourseStore.get(getActivity()).getCoursesById();
// Create an ArrayAdapter to use for displaying FlashcardListFragments in FlashcardListActivity
/*
ArrayAdapter<Flashcard> adapter = new ArrayAdapter<Flashcard>(
getActivity(),
android.R.layout.simple_list_item_1,
mFlashcards);
*/
// OLD METHOD
//ListItemLayoutAdapter adapter = new ListItemLayoutAdapter(mCourses);
CourseCursorAdapter adapter = new CourseCursorAdapter(getActivity(), mCursor);
// Set the adapter for the list
setListAdapter(adapter);
}
/* Handles a user selection of a FlashcardListFragment
*
*/
#Override
public void onListItemClick(ListView l, View v, int position, long id) {
//Flashcard f = (Flashcard)(getListAdapter()).getItem(position);
// OLD WAY
//Course c = ((ListItemLayoutAdapter)getListAdapter()).getItem(position);
/*
Course c = ((CourseCursorAdapter))getListAdapter());
Log.d(TAG, c.getCourseTitle() + " was clicked");
// Start a new activity using an Intent
Intent openFlashcardDetail = new Intent(getActivity(), CourseActivity.class);
// Add EXTRAS to the intent
//openFlashcardDetail.putExtra(FlashcardFragment.EXTRA_FLASHCARD_ID, f.getId());
openFlashcardDetail.putExtra(Course.EXTRA_COURSE_ID, c.getId());
startActivity(openFlashcardDetail);
*/
}
// OLD METHOD
//class ListItemLayoutAdapter extends ArrayAdapter<Course> {
class CourseCursorAdapter extends CursorAdapter {
// Member Properties
// OLD METHOD
Course mCurrentListObject;
CourseCursor mCourseCursor;
// Constructor
// OLD METHOD
/*
public ListItemLayoutAdapter(ArrayList<Course> itemContent) {
super(getActivity(), 0, itemContent);
}
*/
public CourseCursorAdapter(Context context, CourseCursor cursor) {
super(context, cursor, 0);
mCourseCursor = cursor;
}
// OLD WAY
/*
#Override
public View getView(int position, View convertView, ViewGroup parent) {
//If we weren't given a view, inflate a new view
if (convertView == null) {
convertView = getActivity().getLayoutInflater()
.inflate(R.layout.list_item_layout, null);
}
// Configure the view for this object
mCurrentListObject = getItem(position);
// Make Connections from Layout to Java code
TextView mainTextView = (TextView) convertView.findViewById(R.id.item_main_text);
TextView subTextView = (TextView) convertView.findViewById(R.id.item_sub_text);
// Set the Contents of the Views
mainTextView.setText(setMainText());
subTextView.setText(setSubText());
return convertView;
}
*/
#Override
public View newView(Context context, Cursor cursor, ViewGroup parent) {
// Use a layout Inflater to get a row view
LayoutInflater inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
return inflater.inflate(android.R.layout.simple_list_item_1, parent, false);
}
#Override
public void bindView(View view, Context context, Cursor cursor) {
// Get the course for the current row
Course course = mCourseCursor.getCourse();
// Setup a new TextView
TextView courseTitleTextView = (TextView)view;
String mainText = context.getString(R.string.course_title_hint, course.getCourseTitle());
courseTitleTextView.setText(mainText);
}
public String setMainText() {
return mCurrentListObject.getCourseTitle();
}
public String setSubText() {
return mCurrentListObject.getSubject();
}
}
}
I have solution, I hope this will help you. !!
Step 1) Change Build Target
For Example, if you selected 2.2 as Build Target then select Maximum you have. (like 4.4)
And if you selected 4.4 then select 4.3 as target.
Step 2) Clean Project
It will create R.java again
Thank you.
If you are on Eclipse open your problems view (Window -> Show view -> Problems) and look for an error in your resources or manifest
Try restarting eclipse, sometimes it will do the trick. Check if you are using jdk 7, it only needs jdk 6.
This usually happens because there is an issue somewhere in your XML or resource file names.
Try restarting Eclipse, if it still does not indicate where the problem is, try retracing your steps the last changes you made to XML files before this problem occurred. You will find some kind of syntax error or spelling mistake or invalid file name somewhere. Fix it then clean again.
I know a lot of people asked this question but I'm not sure the solution for my problem is the same.
My code is:
package com.example.goo;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.ScrollView;
import android.widget.TextView;
public class Calendrier extends Activity{
LinearLayout linear;
TextView text;
ScrollView SV;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
SV = new ScrollView(this);
linear = new LinearLayout(this);
linear.setOrientation(LinearLayout.VERTICAL);
text = new TextView(this);
text.setText("This is an example for the Bright Hub !");
SV.addView(linear);
linear.addView(text);
setContentView(linear);
}
}
and the error is:
Caused by: java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first.
You are doing mistake with setContentView because you already added linearLayout in view and you are trying to add second time which cause error,
Try this:
setContentView(SV);
Instead:
setContentView(linear);
Just
setContentView(linear); => setContentView(SV);
Hope it's help
I'm not sure, but I suppose you are getting this error on the last line (setContentView(linear);).
You first add that view linear to the scrollview SV, and then set it as the contentView.
I only know this error to come up when you add one view to another twice, but I suppose setting it as the contentview will work the same: it cannot be both a child of SV AND the root view.
Either set SV in setContentVieW, or don't add linear to that Scrollview