Skip to content Skip to sidebar Skip to footer

Android App Suspends/crashes When Add New Code Inside Oncreate

I'm doing a simple news and videos with tabs app but I'm having problems when I add a onClickListener to change to another page, right after I add the code it the app suspends afte

Solution 1:

The problem in this line:

setContentView(R.layout.news_content);

Once you set conentView you cannot change it again with a button.

To use set content view again, you must open new activity or reopen the current activity.

Edit:

in xml your view is type of LinearLayout.

In your code its should be like this:

LinearLayoutnewLink= (LinearLayout) findViewById(R.id.news1);

instead of this:

View newLink = (View) findViewById(R.id.news1);

Edit2:

now when im looking on your main activity xml , you are using it wrong! You must put the view pager as a second view and not as a root.

like this:

<?xml version="1.0" encoding="utf-8"?><RelativeLayoutandroid:id="@+id/RelativeLayout1"xmlns:android="http://schemas.android.com/apk/res/android"android:orientation="vertical"android:layout_width="match_parent"android:layout_height="match_parent"><android.support.v4.view.ViewPagerxmlns:android="http://schemas.android.com/apk/res/android"android:id="@+id/pager"android:layout_width="match_parent"android:layout_height="match_parent" /></RelativeLayout>

Edit3:

Why would you put a click listener to a linear layout? also in your main activity xml i cannot find any textview.. you cannot refer to a textview which is outside of your current activity. If you look in your tab1.xml you will find that the textview you are trying to set is there.

Edit4:

Example for using method findViewById inside fragment: Button b =(Button) android.findViewById (R.id.yourbutton) Make sure to write this after your View android = inflater.inflate(R.layout.tab1, container, false);

Post a Comment for "Android App Suspends/crashes When Add New Code Inside Oncreate"