Does scrollview work in fragments? - java

I have an employeeList fragment which upon access crashes the app. There are no squiggly lines in code, neither any alarms in the stack trace. I have a hunch that scrollView might be causing this. Here's the code
ManageFragment.java
package com.teslaqubitsins.fasih.teslahcm;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.app.AlertDialog;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import java.util.ArrayList;
import static android.content.Context.MODE_PRIVATE;
import static android.database.sqlite.SQLiteDatabase.openOrCreateDatabase;
public class ManageFragment extends Fragment {
EmployeeDatasource mEmployeeDatasource;
View rootView;
public ManageFragment() {
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
super.onCreateView(inflater, container, savedInstanceState);
rootView = inflater.inflate(R.layout.fragment_manage, container, false);
mEmployeeDatasource = new EmployeeDatasource(getActivity());
final ArrayList<Employee> employeeArrayList = mEmployeeDatasource.getList();
ListView mListView = (ListView) getActivity().findViewById(R.id.employeeList_listView);
EmployeeAdapter mEmployeeAdapter = new EmployeeAdapter(this.getActivity(), R.layout.row_employee_list, employeeArrayList);
mListView.setAdapter(mEmployeeAdapter);
mListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
EmployeeDetailFragment detailedemployee = new EmployeeDetailFragment();
Employee item = employeeArrayList.get(i);
EmployeeDetailFragment.mEmployee = item;
FragmentTransaction ft = getFragmentManager().beginTransaction();
ft.replace(R.id.content, detailedemployee);
ft.addToBackStack(null);
ft.commit();
}
});
return rootView;
}
}
fragment_manage.xml
<FrameLayout 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.teslaqubitsins.fasih.teslahcm.ManageFragment">
<!-- TODO: Update blank fragment layout -->
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<ListView
android:id="#+id/employeeList_listView"
android:layout_width="match_parent"
android:layout_height="match_parent">
</ListView>
</LinearLayout>
</FrameLayout>
row_employee_list.xml
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent" android:layout_height="match_parent">
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:padding="2dp">
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="7"
android:orientation="vertical">
<ImageView
android:id="#+id/row_employee_imageView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:adjustViewBounds="true"
android:scaleType="fitXY"
android:src="#drawable/ic_stub" />
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="3"
android:orientation="vertical"
android:padding="5dp">
<TextView
android:id="#+id/row_employee_name"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:maxLines="1"
android:text="Employee Name"
android:textSize="20sp" />
<TextView
android:id="#+id/row_employee_salary"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_margin="5dp"
android:maxLines="1"
android:text="120 PKR" />
</LinearLayout>
</LinearLayout>
</FrameLayout>
Please someone help me sort out the issue.
LOGS
04-24 15:17:31.223 1314: 1314 E/ ] Couldn't opendir /data/app/vmdl1759858640.tmp: No such file or
directory 04-24 15:17:32.773 2184-2184/com.google.android.googlequicksearchbox:search W/LocationOracle: Starting background requests 04-24 15:17:33.123 2048-2298/com.google.android.gms.persistent W/GCoreFlp: No location to return for getLastLocation() 04-24 15:17:33.124 2048-2298/com.google.android.gms.persistent W/GCoreFlp: No location to return for getLastLocation() 04-24 15:17:35.478 2048-2960/com.google.android.gms.persistent I/GCoreUlr: Starting service, intent=Intent { act=com.google.android.location.reporting.ACTION_UPDATE_WORLD cmp=com.google.android.gms/com.google.android.location.reporting.service.DispatchingService (has extras) }, extras=Bundle[{receiverAction=android.intent.action.BOOT_COMPLETED}] 04-24 15:17:36.013 2048-2986/com.google.android.gms.persistent I/GCoreUlr: WorldUpdater received intent Intent { act=com.google.android.location.reporting.ACTION_UPDATE_WORLD cmp=com.google.android.gms/com.google.android.location.reporting.service.DispatchingService (has extras) } with receiverAction android.intent.action.BOOT_COMPLETED 04-24 15:17:36.533 2048-2990/com.google.android.gms.persistent I/GCoreUlr: Starting service, intent=Intent { act=com.google.android.location.reporting.ACTION_UPDATE_WORLD cmp=com.google.android.gms/com.google.android.location.reporting.service.DispatchingService (has extras) }, extras=Bundle[{receiverAction=com.google.android.location.internal.server.ACTION_RESTARTED}] 04-24 15:17:36.575 2048-2365/com.google.android.gms.persistent I/GCoreUlr: Starting service, intent=Intent { act=com.google.android.location.reporting.ACTION_UPDATE_WORLD cmp=com.google.android.gms/com.google.android.location.reporting.service.DispatchingService (has extras) }, extras=Bundle[{receiverAction=com.google.android.location.reporting.PHENOTYPE_FLAGS_CHANGED}] 04-24 15:17:37.325 3106-3106/? W/dex2oat: /system/bin/dex2oat
--runtime-arg -classpath --runtime-arg & --instruction-set=x86 --instruction-set-features=smp,ssse3,-sse4.1,-sse4.2,-avx,-avx2,-lock_add,-popcnt
--runtime-arg -Xrelocate --boot-image=/system/framework/boot.art --runtime-arg -Xms64m --runtime-arg -Xmx512m --instruction-set-variant=x86 --instruction-set-features=default --dex-file=/data/user/0/com.google.android.gms/app_fb/f.apk --oat-fd=51 --oat-location=/data/user/0/com.google.android.gms/app_fb/f.dex --compiler-filter=speed 04-24 15:17:37.325 3106-3106/? I/dex2oat: /system/bin/dex2oat
--dex-file=/data/user/0/com.google.android.gms/app_fb/f.apk --oat-fd=51 --oat-location=/data/user/0/com.google.android.gms/app_fb/f.dex --compiler-filter=speed 04-24 15:17:37.780 2048-2298/com.google.android.gms.persistent W/GCoreFlp: No location to return for getLastLocation() 04-24 15:17:37.780 2048-2298/com.google.android.gms.persistent W/GCoreFlp: No location to return for getLastLocation() 04-24 15:17:37.920 2048-2986/com.google.android.gms.persistent I/GCoreUlr: GMS FLP location and AR updates requested: {"description":"stationary","newRequest":true,"samplePeriodMs":2160000,"sampleReason":"stationary","sampleSource":"internal","timestampMs":1493029056848} 04-24 15:17:38.057 2048-2986/com.google.android.gms.persistent I/GCoreUlr: WorldUpdater received intent Intent { act=com.google.android.location.reporting.ACTION_UPDATE_WORLD cmp=com.google.android.gms/com.google.android.location.reporting.service.DispatchingService (has extras) } with receiverAction com.google.android.location.internal.server.ACTION_RESTARTED 04-24 15:17:38.064 2048-2986/com.google.android.gms.persistent I/GCoreUlr: WorldUpdater:com.google.android.location.internal.server.ACTION_RESTARTED: Ensuring that reporting is active for [account#7#] 04-24 15:17:38.069 2048-2986/com.google.android.gms.persistent I/GCoreUlr: WorldUpdater received intent Intent { act=com.google.android.location.reporting.ACTION_UPDATE_WORLD cmp=com.google.android.gms/com.google.android.location.reporting.service.DispatchingService (has extras) } with receiverAction com.google.android.location.reporting.PHENOTYPE_FLAGS_CHANGED 04-24 15:17:38.103 2048-2986/com.google.android.gms.persistent I/GCoreUlr: WorldUpdater:com.google.android.location.reporting.PHENOTYPE_FLAGS_CHANGED: Ensuring that reporting is active for [account#7#] 04-24 15:17:38.430 2048-2048/com.google.android.gms.persistent E/ActivityThread: Service com.google.android.location.places.service.PlaceDetectionAsyncService has leaked IntentReceiver amth#cd0bee8 that was originally registered here. Are you missing a call to unregisterReceiver()?
android.app.IntentReceiverLeaked: Service com.google.android.location.places.service.PlaceDetectionAsyncService has leaked IntentReceiver amth#cd0bee8 that was originally registered here. Are you missing a call to unregisterReceiver()?
at android.app.LoadedApk$ReceiverDispatcher.<init>(LoadedApk.java:1159)
at android.app.LoadedApk.getReceiverDispatcher(LoadedApk.java:946)
at android.app.ContextImpl.registerReceiverInternal(ContextImpl.java:1302)
at android.app.ContextImpl.registerReceiver(ContextImpl.java:1282)
at android.content.ContextWrapper.registerReceiver(ContextWrapper.java:593)
at android.content.ContextWrapper.registerReceiver(ContextWrapper.java:593)
at android.content.ContextWrapper.registerReceiver(ContextWrapper.java:593)
at android.content.ContextWrapper.registerReceiver(ContextWrapper.java:593)
at amrb.run(:com.google.android.gms:4414)
at android.os.Handler.handleCallback(Handler.java:751)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:154)
at android.os.HandlerThread.run(HandlerThread.java:61) 04-24 15:17:38.545 2048-2048/com.google.android.gms.persistent E/ActivityThread: Service com.google.android.location.places.service.PlaceDetectionAsyncService has leaked IntentReceiver amth#b6a0290 that was originally registered here. Are you missing a call to unregisterReceiver()?
android.app.IntentReceiverLeaked: Service com.google.android.location.places.service.PlaceDetectionAsyncService has leaked IntentReceiver amth#b6a0290 that was originally registered here. Are you missing a call to unregisterReceiver()?
at android.app.LoadedApk$ReceiverDispatcher.<init>(LoadedApk.java:1159)
at android.app.LoadedApk.getReceiverDispatcher(LoadedApk.java:946)
at android.app.ContextImpl.registerReceiverInternal(ContextImpl.java:1302)
at android.app.ContextImpl.registerReceiver(ContextImpl.java:1282)
at android.content.ContextWrapper.registerReceiver(ContextWrapper.java:593)
at android.content.ContextWrapper.registerReceiver(ContextWrapper.java:593)
at android.content.ContextWrapper.registerReceiver(ContextWrapper.java:593)
at android.content.ContextWrapper.registerReceiver(ContextWrapper.java:593)
at amrb.run(:com.google.android.gms:4414)
at android.os.Handler.handleCallback(Handler.java:751)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:154)
at android.os.HandlerThread.run(HandlerThread.java:61) 04-24 15:17:42.411 2048-2298/com.google.android.gms.persistent W/GCoreFlp: No location to return for getLastLocation() 04-24 15:17:42.411 2048-2298/com.google.android.gms.persistent W/GCoreFlp: No location to return for getLastLocation()
MainActivity.java
package com.teslaqubitsins.fasih.teslahcm;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.design.widget.BottomNavigationView;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.app.AppCompatActivity;
import android.view.MenuItem;
import android.view.Window;
import android.view.WindowManager;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
private TextView mTextMessage;
private static final String TAG = MainActivity.class.getSimpleName();
private BottomNavigationView bottomNavigation;
private Fragment fragment;
private FragmentManager fragmentManager;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.activity_main);
bottomNavigation = (BottomNavigationView)findViewById(R.id.navigation1);
bottomNavigation.inflateMenu(R.menu.menu);
fragmentManager = getSupportFragmentManager();
bottomNavigation.setOnNavigationItemSelectedListener(new BottomNavigationView.OnNavigationItemSelectedListener() {
#Override
public boolean onNavigationItemSelected(#NonNull MenuItem item) {
int id = item.getItemId();
switch (id){
case R.id.bb_menu_home:
fragment = new HomeFragment();
break;
case R.id.bb_menu_contact:
fragment = new ContactFragment();
break;
case R.id.bb_menu_manage:
fragment = new ManageFragment();
break;
case R.id.bb_menu_queries:
fragment = new QueriesFragment();
break;
case R.id.bb_menu_careers:
fragment = new CareersFragment();
break;
default:
fragment = new HomeFragment();
}
final FragmentTransaction transaction = fragmentManager.beginTransaction();
transaction.replace(R.id.content, fragment).commit();
return true;
}
});
}
}

Replace:
FragmentTransaction ft = getFragmentManager().beginTransaction();
With:
FragmentTransaction ft = getChildFragmentManager().beginTransaction();

Related

What to do about "No view found for id..." in Android Activity with Fragments

I want my app to show this screen to a user in large-land mode so he can give input, press the button, and see output all on the same screen.
For any other mode, I want the two panes shown above to be shown one at a time, in this order, where the second screen appears when the user presses the button, after which pressing the back button returns to the "input" screen on the left:
What I have done works up to the point of showing correct output on the second screen but then I get "No view found for id" container and then an exception occurs
Here's xml for first screen above:
device(large-land).xml
<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:id ="#+id/linear_layout_container_in_large_land_device_xml"
android:weightSum ="3"
>
<fragment
android:name="com.dslomer64.fragments.InputFragment" android:layout_weight ="2" android:layout_width="wrap_content" android:layout_height="match_parent"
android:id ="#+id/inputFragment"
tools:layout ="#layout/input_fragment"
/>
<fragment
android:name="com.dslomer64.fragments.OutputFragment" android:layout_weight ="1" android:layout_width="wrap_content" android:layout_height="match_parent"
android:id ="#+id/outputFragment"
tools:layout ="#layout/output_fragment"
/>
</LinearLayout>
device.xml (for non-large-land)
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent"
android:id="#+id/container"
/>
input_fragment.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent">
<TextView
android:id ="#+id/tvInMainFragment" android:layout_width ="wrap_content" android:layout_height="wrap_content"
android:text="How many?"
/>
<EditText android:layout_width="wrap_content" android:layout_height="wrap_content"
android:id="#+id/etxIn_input_fragment"
android:text="3"
/>
<Button
android:id="#+id/btnIn_input_fragment" android:layout_width="wrap_content" android:layout_height="wrap_content"
android:text="Press to get output in OtherFragment"
/>
</LinearLayout>
output_fragment.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" >
<TextView
android:id="#+id/txvIn_output_fragment" android:layout_width="wrap_content" android:layout_height="wrap_content"
android:text="Output will go here in OutputFragment"
/>
</LinearLayout>
MainActivity.java
public class MainActivity extends Activity
{
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.device);
if (findViewById(R.id.container) != null) {
InputFragment inputFragment;
inputFragment = new InputFragment();
getFragmentManager().beginTransaction()
.add(R.id.container, inputFragment)
.commit();
}
}
}
InputFragment.java
public class InputFragment extends Fragment
{
public View onCreateView(LayoutInflater inflater,
ViewGroup viewGroup,
Bundle savedInstanceState)
{
View view = inflater.inflate(R.layout.input_fragment, viewGroup, false);
Button btn;
btn = (Button)view.findViewById(R.id.btnIn_input_fragment);
btn.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
OutputFragment outputFragment;
outputFragment = new OutputFragment();
if (getFragmentManager().findFragmentById(R.id.inputFragment) == null) {
getActivity().setContentView(R.layout.device);
FragmentTransaction fragmentTransaction = getFragmentManager().beginTransaction();
fragmentTransaction.replace(R.id.container, outputFragment);
fragmentTransaction.addToBackStack(null);
fragmentTransaction.commit();
}
getActivity().setContentView(R.layout.input_fragment);
EditText etx = (EditText) getActivity().findViewById(R.id.etxIn_input_fragment);
fillFragmentOtherWithInfo(etx.getText().toString());
}
});
return view;
}
public void fillFragmentOtherWithInfo(String ks)
{
getActivity().setContentView(R.layout.output_fragment);
TextView tvOther = (TextView)getActivity().findViewById(R.id.txvIn_output_fragment);
String s = "";
for(int i = 0; i < parseInt(ks); i++)
s += ("\n" + (char)(i+65));
tvOther.setText(s);
}
}
What happens is all good up to the point of showing the output on the second screen. It does show correctly, but then I immediately get an exception that is preceded with this message, and I have no idea what to do about it:
03-02 15:59:52.987 7901-7901/com.dslomer64.fragments E/FragmentManager: No view found for id 0x7f070000 (com.dslomer64.fragments:id/container) for fragment OutputFragment{41ecc848 #1 id=0x7f070000}
container is the id of the FrameLayout in device.xml (non-large-land) that is referenced in the .add part of the fragment transaction in MainActivity onCreate. I get the same error if I use .replace instead of .add.
Here is the entire text of the error:
No view found for id 0x7f070000 (com.dslomer64.fragments:id/container) for fragment OutputFragment{41ebf0f0 #1 id=0x7f070000}
Activity state:
Local Activity 41e4d8a8 State:
mResumed=true mStopped=false mFinished=false
mLoadersStarted=true
mChangingConfigurations=false
mCurrentConfig={1.15 311mcc480mnc en_US ldltr sw600dp w600dp h888dp 213dpi lrg port finger -keyb/v/h -nav/h skin=/system/framework/framework-res.apk s.16}
Active Fragments in 41e4d9a0:
#0: InputFragment{41e6d1d8 #0 id=0x7f070000}
mFragmentId=#7f070000 mContainerId=#7f070000 mTag=null
mState=1 mIndex=0 mWho=android:fragment:0 mBackStackNesting=2
mAdded=false mRemoving=true mResumed=false mFromLayout=false mInLayout=false
mHidden=false mDetached=false mMenuVisible=true mHasMenu=false
mRetainInstance=false mRetaining=false mUserVisibleHint=true
mFragmentManager=FragmentManager{41e4d9a0 in MainActivity{41e4d8a8}}
mActivity=com.dslomer64.fragments.MainActivity#41e4d8a8
mSavedViewState=
{2131165188=android.view.AbsSavedState$1#41acfcc0, 2131165189=TextView.SavedState{41ec9d10 start=0 end=0 text=3}, 2131165190=android.view.AbsSavedState$1#41acfcc0}
#1: OutputFragment{41ebf0f0 #1 id=0x7f070000}
mFragmentId=#7f070000 mContainerId=#7f070000 mTag=null
mState=0 mIndex=1 mWho=android:fragment:1 mBackStackNesting=1
mAdded=true mRemoving=false mResumed=false mFromLayout=false mInLayout=false
mHidden=false mDetached=false mMenuVisible=true mHasMenu=false
mRetainInstance=false mRetaining=false mUserVisibleHint=true
mFragmentManager=FragmentManager{41e4d9a0 in MainActivity{41e4d8a8}}
mActivity=com.dslomer64.fragments.MainActivity#41e4d8a8
Added Fragments:
#0: OutputFragment{41ebf0f0 #1 id=0x7f070000}
Back Stack:
#0: BackStackEntry{41e6d288 #0}
mName=null mIndex=0 mCommitted=true
Operations:
Op #0: REPLACE InputFragment{41e6d1d8 #0 id=0x7f070000}
Back Stack Indices:
#0: BackStackEntry{41e6d288 #0}
#1: BackStackEntry{41ebf8d0 #1}
FragmentManager misc state:
mActivity=com.dslomer64.fragments.MainActivity#41e4d8a8
mContainer=android.app.Activity$1#41e4da18
mCurState=5 mStateSaved=false mDestroyed=false
ViewRoot:
mAdded=true mRemoved=false
mConsumeBatchedInputScheduled=false
mPendingInputEventCount=0
mProcessInputEventsScheduled=false
mTraversalScheduled=false
android.view.ViewRootImpl$NativePreImeInputStage: mQueueLength=0
android.view.ViewRootImpl$ImeInputStage: mQueueLength=0
android.view.ViewRootImpl$NativePostImeInputStage: mQueueLength=0
Choreographer:
mFrameScheduled=false
mLastFrameTime=20289147 (31 ms ago)
View Hierarchy:
com.android.internal.policy.impl.PhoneWindow$DecorView{41e50e28 V.E..... R....... 0,0-800,1216}
com.android.internal.widget.ActionBarOverlayLayout{41e51e40 V.ED.... ........ 0,0-800,1216 #1020317 android:id/action_bar_overlay_layout}
android.widget.FrameLayout{41e53548 V.E..... ......I. 0,108-800,1216 #1020002 android:id/content}
android.widget.LinearLayout{41ec5d60 V.E..... ......I. 0,0-800,1108}
android.widget.TextView{41ec6080 V.ED.... ........ 0,0-14,101 #7f070007 app:id/txvIn_output_fragment}
com.android.internal.widget.ActionBarContainer{41e53a68 V.ED.... ........ 0,33-800,108 #1020318 android:id/action_bar_container}
com.android.internal.widget.ActionBarView{41e565d0 V.E..... ........ 0,0-800,75 #1020319 android:id/action_bar}
android.widget.LinearLayout{41e56d40 V.....C. ........ 11,0-248,75}
com.android.internal.widget.ActionBarView$HomeView{41e5abe0 V.E..... ........ 0,0-75,75}
android.widget.ImageView{41e5b0b0 G.ED.... ......I. 0,0-0,0 #102025a android:id/up}
android.widget.ImageView{41e5c1a8 V.ED.... ........ 5,5-69,69 #102002c android:id/home}
android.widget.LinearLayout{41e5d680 V.E..... ........ 75,21-237,54}
android.widget.TextView{41e5db28 V.ED.... ........ 0,0-151,33 #1020265 android:id/action_bar_title}
android.widget.TextView{41e5eb40 G.ED.... ......I. 0,0-0,0 #1020266 android:id/action_bar_subtitle}
com.android.internal.view.menu.ActionMenuView{41ea45b0 V.ED.... ......ID 800,0-800,75}
com.android.internal.widget.ActionBarContextView{41e5f438 G.E..... ......ID 0,0-0,0 #102031a android:id/action_context_bar}
com.android.internal.widget.ActionBarContainer{41e61e58 G.ED.... ......ID 0,0-0,0 #102031b android:id/split_action_bar}
Looper (main, tid 1) {41e42ba0}
(Total messages: 0, idling=false, quitting=false)
id=1: thread exiting with uncaught exception (group=0x41905cf8)
id=1: uncaught exception occurred
.lang.IllegalArgumentException: No view found for id 0x7f070000 (com.dslomer64.fragments:id/container) for fragment OutputFragment{41ebf0f0 #1 id=0x7f070000}
at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:882)
at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:1062)
at android.app.BackStackRecord.run(BackStackRecord.java:698)
at android.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1447)
at android.app.FragmentManagerImpl$1.run(FragmentManager.java:443)
at android.os.Handler.handleCallback(Handler.java:808)
at android.os.Handler.dispatchMessage(Handler.java:103)
at android.os.Looper.loop(Looper.java:193)
at android.app.ActivityThread.main(ActivityThread.java:5299)
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:831)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:647)
at dalvik.system.NativeStart.main(Native Method)
id=1: calling UncaughtExceptionHandler
FATAL EXCEPTION: main
Process: com.dslomer64.fragments, PID: 24665
java.lang.IllegalArgumentException: No view found for id 0x7f070000 (com.dslomer64.fragments:id/container) for fragment OutputFragment{41ebf0f0 #1 id=0x7f070000}
at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:882)
at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:1062)
at android.app.BackStackRecord.run(BackStackRecord.java:698)
at android.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1447)
at android.app.FragmentManagerImpl$1.run(FragmentManager.java:443)
at android.os.Handler.handleCallback(Handler.java:808)
at android.os.Handler.dispatchMessage(Handler.java:103)
at android.os.Looper.loop(Looper.java:193)
at android.app.ActivityThread.main(ActivityThread.java:5299)
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:831)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:647)
at dalvik.system.NativeStart.main(Native Method)
The code below produces the desired activity. WITH Fragments.
MainActivity.java
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.res.Configuration;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
public class MainActivity extends Activity
{
public static boolean PORTRAIT_MODE;
Context mContext;
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
mContext = this;
setContentView(R.layout.main);
PORTRAIT_MODE = (getResources().getConfiguration().orientation == Configuration.ORIENTATION_PORTRAIT );
Button btn = (Button) findViewById(R.id.btn);
btn.setOnClickListener(new View.OnClickListener()
{
#Override
public void onClick(View v)
{
if (PORTRAIT_MODE)
{
EditText inputP = (EditText)findViewById(R.id.input_in_main_portrait);
Intent outputActivity;
outputActivity = new Intent(mContext, OutputActivity.class);
outputActivity.putExtra("numberOfLetters", inputP.getText().toString());
startActivity(outputActivity);
} else
{
EditText inputL = (EditText)findViewById(R.id.input_in_main_portrait);
TextView outputL = (TextView)findViewById(R.id.txvIn_output_fragment);
outputL.setText(DoStuff.createOutput(Integer.parseInt(inputL.getText().toString())));
}
}
});
}
}
DoStuff.java
public class DoStuff
{
public static String createOutput(int k){
String s = "";
for(int i = 0; i < k; i++)
s += "\n" + (char)(i+65);
return s;
}
}
OutputActivity.java
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.widget.TextView;
public class OutputActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
Intent i = getIntent();
String s = i.getStringExtra("numberOfLetters");
Log.w("OutputActivity=====", "`````TRYING to be Displaying " + s + " letters in what? Portrait? " + MainActivity.PORTRAIT_MODE);
setContentView(R.layout.output_fragment);
TextView et = (TextView) findViewById(R.id.txvIn_output_fragment);
et.setText(DoStuff.createOutput(Integer.parseInt(s)));
}
}
InputFragment.java
import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
public class InputFragment extends Fragment{
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup parent, Bundle savedInstanceState) {
super.onCreateView(inflater, parent, savedInstanceState);
return inflater.inflate(R.layout.input_fragment, parent, false);
}
}
OutputFragment.java
import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
public class OutputFragment extends Fragment{
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup parent, Bundle savedInstanceState) {
super.onCreateView(inflater, parent, savedInstanceState);
return inflater.inflate(R.layout.output_fragment, parent, false);
}
}
input_fragment.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent">
<TextView
android:id ="#+id/tvIn_main_portrait" android:layout_width ="wrap_content" android:layout_height="wrap_content"
android:text="How many?"
/>
<EditText android:layout_width="match_parent" android:layout_height="wrap_content"
android:id="#+id/input_in_main_portrait"
android:text="4"
/>
<Button
android:id="#+id/btn" android:layout_width="wrap_content" android:layout_height="wrap_content"
android:text="Press to get output"
/>
</LinearLayout>
output_fragment.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" >
<TextView
android:id="#+id/txvIn_output_fragment" android:layout_width="wrap_content" android:layout_height="wrap_content"
android:text="Output will go here in OutputFragment"
/>
</LinearLayout>
layout\main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent">
<fragment android:layout_width="match_parent" android:layout_height="match_parent"
android:id="#+id/main_fragment"
android:name="com.dslomer64.fragthis.InputFragment"
/>
</LinearLayout>
layout-land\main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="horizontal" android:layout_width="match_parent" android:layout_height="match_parent"
android:layout_weight="3"
>
<fragment android:layout_width="wrap_content" android:layout_height="match_parent"
android:id="#+id/input_fragment"
android:name="com.dslomer64.fragthis.InputFragment"
/>
<fragment android:layout_width="wrap_content" android:layout_height="match_parent"
android:id="#+id/output_fragment"
android:name="com.dslomer64.fragthis.OutputFragment"
/>
</LinearLayout>

Fragment activity not inflating

For my master detail flow app I'm trying to inflate my Continents list view on my tablet but it won't do so and I end up with these errors in my log cat. Does anyone know what I'm doing wrong?
Continents List xml
<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:layout_marginLeft="16dp"
android:layout_marginRight="16dp" android:baselineAligned="false"
android:divider="?android:attr/dividerHorizontal" android:orientation="horizontal"
android:showDividers="middle" tools:context=".ItemListActivity">
<fragment android:id="#+id/continents_list" android:name="com.apptacularapps.md.ContinentsListFragment"
android:layout_width="0dp" android:layout_height="match_parent" android:layout_weight="1"
tools:layout="#android:layout/list_content" />
<FrameLayout android:id="#+id/item_detail_container" android:layout_width="0dp"
android:layout_height="match_parent" android:layout_weight="3" />
</LinearLayout>
ContinentsListActivity.java
import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
public class ContinentsListActivity extends ActionBarActivity
implements ContinentsListFragment.Callbacks {
private boolean mTwoPane;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_continents_list);
FragmentManager fm = getFragmentManager();
FragmentTransaction ft = fm.beginTransaction();
ft.replace(R.id.continents_list_contaniner, new ContinentsListFragment());
ft.commit();
if (findViewById(R.id.item_detail_container) != null) {
mTwoPane = true;
((ContinentsListFragment) getSupportFragmentManager()
.findFragmentById(R.id.continents_list))
.setActivateOnItemClick(true);
}
}
#Override
public void onItemSelected(String id) {
if("1".equals(id)){
if (mTwoPane) {
Bundle arguments = new Bundle();
arguments.putString(ItemDetailFragment.ARG_ITEM_ID, id);
ItemDetailFragment fragment = new ItemDetailFragment();
fragment.setArguments(arguments);
getSupportFragmentManager().beginTransaction()
.replace(R.id.continents_list, fragment)
.commit();
} else {
Intent detailIntent = new Intent(this, ContinentsListActivity.class);
startActivity(detailIntent);
}
}
}
}
ContinentsListFragment.java
package com.apptacularapps.md;
import android.app.Activity;
import android.os.Bundle;
import android.support.v4.app.ListFragment;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.ListView;
public class ContinentsListFragment extends ListFragment {
private static final String STATE_ACTIVATED_POSITION = "activated_position";
private Callbacks mCallbacks = sContinentsCallbacks;
private int mActivatedPosition = ListView.INVALID_POSITION;
public interface Callbacks {
public void onItemSelected(String id);
}
private static Callbacks sContinentsCallbacks = new Callbacks() {
#Override
public void onItemSelected(String id) {
}
};
public ContinentsListFragment() {
}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// TODO: replace with a real list adapter.
setListAdapter(new ArrayAdapter<ContinentsContent.ContinentsItem>(
getActivity(),
android.R.layout.simple_list_item_activated_1,
android.R.id.text1,
ContinentsContent.ITEMS));
}
#Override
public void onViewCreated(View view, Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
if (savedInstanceState != null
&& savedInstanceState.containsKey(STATE_ACTIVATED_POSITION)) {
setActivatedPosition(savedInstanceState.getInt(STATE_ACTIVATED_POSITION));
}
}
#Override
public void onAttach(Activity activity) {
super.onAttach(activity);
// Activities containing this fragment must implement its callbacks.
if (!(activity instanceof Callbacks)) {
throw new IllegalStateException("Activity must implement fragment's callbacks.");
}
mCallbacks = (Callbacks) activity;
}
#Override
public void onDetach() {
super.onDetach();
// Reset the active callbacks interface to the Continents implementation.
mCallbacks = sContinentsCallbacks;
}
#Override
public void onListItemClick(ListView listView, View view, int position, long id) {
super.onListItemClick(listView, view, position, id);
mCallbacks.onItemSelected(ContinentsContent.ITEMS.get(position).id);
}
#Override
public void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
if (mActivatedPosition != ListView.INVALID_POSITION) {
outState.putInt(STATE_ACTIVATED_POSITION, mActivatedPosition);
}
}
public void setActivateOnItemClick(boolean activateOnItemClick) {
getListView().setChoiceMode(activateOnItemClick
? ListView.CHOICE_MODE_SINGLE
: ListView.CHOICE_MODE_NONE);
}
private void setActivatedPosition(int position) {
if (position == ListView.INVALID_POSITION) {
getListView().setItemChecked(mActivatedPosition, false);
} else {
getListView().setItemChecked(position, true);
}
mActivatedPosition = position;
}
}
Logcat
05-20 20:29:42.523 32338-32338/com.apptacularapps.md E/libprocessgroup﹕ failed to make and chown /acct/uid_10056: Read-only file system
05-20 20:29:42.531 32338-32338/com.apptacularapps.md W/Zygote﹕ createProcessGroup failed, kernel missing CONFIG_CGROUP_CPUACCT?
05-20 20:29:42.532 32338-32338/com.apptacularapps.md I/art﹕ Not late-enabling -Xcheck:jni (already on)
05-20 20:29:43.002 32338-32352/com.apptacularapps.md I/art﹕ Background sticky concurrent mark sweep GC freed 3104(261KB) AllocSpace objects, 0(0B) LOS objects, 26% free, 829KB/1135KB, paused 11.699ms total 50.909ms
05-20 20:29:43.006 32338-32359/com.apptacularapps.md D/OpenGLRenderer﹕ Render dirty regions requested: true
05-20 20:29:43.034 32338-32338/com.apptacularapps.md D/﹕ HostConnection::get() New Host Connection established 0xa693a490, tid 32338
05-20 20:29:43.070 32338-32338/com.apptacularapps.md D/Atlas﹕ Validating map...
05-20 20:29:43.227 32338-32359/com.apptacularapps.md D/﹕ HostConnection::get() New Host Connection established 0xa693a8a0, tid 32359
05-20 20:29:43.246 32338-32359/com.apptacularapps.md I/OpenGLRenderer﹕ Initialized EGL, version 1.4
05-20 20:29:43.376 32338-32359/com.apptacularapps.md D/OpenGLRenderer﹕ Enabling debug mode 0
05-20 20:29:43.397 32338-32359/com.apptacularapps.md W/EGL_emulation﹕ eglSurfaceAttrib not implemented
05-20 20:29:43.397 32338-32359/com.apptacularapps.md W/OpenGLRenderer﹕ Failed to set EGL_SWAP_BEHAVIOR on surface 0xa696eba0, error=EGL_SUCCESS
05-20 20:29:46.874 32338-32338/com.apptacularapps.md D/AndroidRuntime﹕ Shutting down VM
05-20 20:29:46.875 32338-32338/com.apptacularapps.md E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: com.apptacularapps.md, PID: 32338
java.lang.IllegalStateException: Activity must implement fragment's callbacks.
at com.apptacularapps.md.ContinentsListFragment.onAttach(ContinentsListFragment.java:89)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:907)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1138)
at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:740)
at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1501)
at android.support.v4.app.FragmentManagerImpl$1.run(FragmentManager.java:458)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5221)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)
your implementation of onAttach, says
if (!(activity instanceof Callbacks)) {
throw new IllegalStateException("Activity must implement fragment's callbacks.");
}
To fix you have to add implements Callbacks to the Activity hosting the Fragment.
Edit:
in the xml of your activity, replace
<fragment android:id="#+id/continents_list" android:name="com.apptacularapps.md.ContinentsListFragment"
android:layout_width="0dp" android:layout_height="match_parent" android:layout_weight="1"
tools:layout="#android:layout/list_content" />
with
<FrameLayout android:id="#+id/continents_list_contaniner"
android:layout_width="0dp" android:layout_height="match_parent" android:layout_weight="1"/>
and onCreate() of the Activity, after setContentView do:
FragmentManager fm = getSupportFragmentManager();
FragmentTransaction ft = fm.beginTransaction();
ft.replace(R.id.continents_list_contaniner, new ContinentsListFragment());
ft.commit();
Edit2:
in your Activity replace
import android.app.FragmentManager;
import android.app.FragmentTransaction;
with
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction

ViewPager & crash, after rotation and swipe, due to a nullpointerexception

So I have a lengthy example, I created just for stack overflow. I can't seem to figure it out. I'm not a native Java programmer so that contributes to some confusion. But, this example requires 10 files, and is not just a code dump.
I've tried debugging this for numerous hours and as a last resort have created as concise of an example as I could of the issue, and have come here for help.
The following are the files, and at the bottom the stack trace. I'm really out of ideas of what to try. It's due to a nullpointerexception, that happens after I rotate the screen then try to slide in the ViewPager. I think should be an easy fix, but it hasn't been so for me.
Below are the java, and xml files, and stack trace. I think they are all necessary because they all are intertwined.
Here is a zip of the files too, if that's useful: https://www.dropbox.com/s/sbv4irar727tp1x/CrashExample.zip?dl=0
main.java
package me.spencernusbaum.crashexample;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import java.util.List;
import java.util.Vector;
/**
* Created by Spencer on 4/26/2015.
*/
public class main extends FragmentActivity {
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.viewpager);
ViewPager pager = (ViewPager) findViewById(R.id.viewpager);
final List<Fragment> fragments = new Vector<Fragment>();
Fragment f2 = new fragment_view();
Fragment f1 = fragment_menu.newInstance(f2, pager);
fragments.add(f1);
fragments.add(f2);
pager.setAdapter(new FragmentPagerAdapter(this.getSupportFragmentManager()) {
#Override
public Fragment getItem(int i) {
return fragments.get(i);
}
#Override
public int getCount() {
return fragments.size();
}
});
}
}
fragment_menu.java
package me.spencernusbaum.crashexample;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
/**
* Created by Spencer on 4/26/2015.
*/
public class fragment_menu extends Fragment {
Fragment fragment;
ViewPager viewPager;
public static final fragment_menu newInstance(Fragment f, ViewPager p) {
fragment_menu result = new fragment_menu();
result.fragment = f;
result.viewPager = p;
return result;
}
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View result = inflater.inflate(R.layout.fragment_menu, null);
result.findViewById(R.id.button1).setOnTouchListener(new View.OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
((fragment_view)fragment).setView(new fragment1());
return false;
}
});
result.findViewById(R.id.button1).setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
((fragment_view)fragment).setView(new fragment1());
viewPager.setCurrentItem(1, true);
}
});
result.findViewById(R.id.button2).setOnTouchListener(new View.OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
((fragment_view)fragment).setView(new fragment2());
return false;
}
});
result.findViewById(R.id.button2).setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
((fragment_view)fragment).setView(new fragment2());
viewPager.setCurrentItem(1, true);
}
});
return result;
}
}
fragment_view.java
package me.spencernusbaum.crashexample;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
/**
* Created by Spencer on 4/26/2015.
*/
public class fragment_view extends Fragment {
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
return inflater.inflate(R.layout.fragment_view, null);
}
public void setView(Fragment f) {
getFragmentManager().beginTransaction().replace(R.id.fragment, f).commit();
}
}
fragment1.java
package me.spencernusbaum.crashexample;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
/**
* Created by Spencer on 4/26/2015.
*/
public class fragment1 extends Fragment {
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
return inflater.inflate(R.layout.fragment1, null);
}
}
fragment2.java
package me.spencernusbaum.crashexample;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
/**
* Created by Spencer on 4/26/2015.
*/
public class fragment2 extends Fragment {
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
return inflater.inflate(R.layout.fragment2, null);
}
}
fragment1.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Fragment1"
android:id="#+id/textView"
android:layout_gravity="center_horizontal" />
</LinearLayout>
fragment2.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Large Text"
android:id="#+id/textView2"
android:layout_gravity="center_horizontal" />
</LinearLayout>
fragment_menu.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<Button
android:id="#+id/button1"
android:text="Fragment 1"
android:layout_weight="1"
android:layout_width="match_parent"
android:layout_height="0dp" />
<Button
android:id="#+id/button2"
android:text="Fragment 2"
android:layout_weight="1"
android:layout_width="match_parent"
android:layout_height="0dp" />
</LinearLayout>
fragment_view.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/fragment"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
</LinearLayout>
viewpager.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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.ViewPager
android:id="#+id/viewpager"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
The Stack Trace:
04-26 12:38:32.368 7635-7635/me.spencernusbaum.crashexample E/InputEventReceiver﹕ Exception dispatching input event.
04-26 12:38:32.368 7635-7635/me.spencernusbaum.crashexample E/MessageQueue-JNI﹕ Exception in MessageQueue callback: handleReceiveCallback
04-26 12:38:32.370 7635-7635/me.spencernusbaum.crashexample E/MessageQueue-JNI﹕ java.lang.NullPointerException: Attempt to invoke virtual method 'void me.spencernusbaum.crashexample.fragment_view.setView(android.support.v4.app.Fragment)' on a null object reference
at me.spencernusbaum.crashexample.fragment_menu$1.onTouch(fragment_menu.java:32)
at android.view.View.dispatchTouchEvent(View.java:8382)
at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2430)
at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2119)
at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2430)
at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2119)
at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2430)
at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2119)
at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2430)
at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2119)
at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2430)
at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2119)
at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2430)
at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2119)
at com.android.internal.policy.impl.PhoneWindow$DecorView.superDispatchTouchEvent(PhoneWindow.java:2314)
at com.android.internal.policy.impl.PhoneWindow.superDispatchTouchEvent(PhoneWindow.java:1692)
at android.app.Activity.dispatchTouchEvent(Activity.java:2739)
at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchTouchEvent(PhoneWindow.java:2275)
at android.view.View.dispatchPointerEvent(View.java:8578)
at android.view.ViewRootImpl$ViewPostImeInputStage.processPointerEvent(ViewRootImpl.java:4021)
at android.view.ViewRootImpl$ViewPostImeInputStage.onProcess(ViewRootImpl.java:3887)
at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:3449)
at android.view.ViewRootImpl$InputStage.onDeliverToNext(ViewRootImpl.java:3502)
at android.view.ViewRootImpl$InputStage.forward(ViewRootImpl.java:3468)
at android.view.ViewRootImpl$AsyncInputStage.forward(ViewRootImpl.java:3578)
at android.view.ViewRootImpl$InputStage.apply(ViewRootImpl.java:3476)
at android.view.ViewRootImpl$AsyncInputStage.apply(ViewRootImpl.java:3635)
at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:3449)
at android.view.ViewRootImpl$InputStage.onDeliverToNext(ViewRootImpl.java:3502)
at android.view.ViewRootImpl$InputStage.forward(ViewRootImpl.java:3468)
at android.view.ViewRootImpl$InputStage.apply(ViewRootImpl.java:3476)
at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:3449)
at android.view.ViewRootImpl.deliverInputEvent(ViewRootImpl.java:5701)
at android.view.ViewRootImpl.doProcessInputEvents(ViewRootImpl.java:5675)
at android.view.ViewRootImpl.enqueueInputEvent(ViewRootImpl.java:5646)
at android.view.ViewRootImpl$WindowInputEventReceiver.onInputEvent(ViewRootImpl.java:5791)
at android.view.InputEventReceiver.dispatchInputEvent(InputEventReceiver.java:185)
at android.os.MessageQueue.nativePollOnce(Native Method)
at android.os.MessageQueue.next(MessageQueue.java:143)
at android.os.Looper.loop(Looper.java:122)
at android.app.ActivityThread.main(ActivityThread.java:5221)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)
04-26 12:38:32.370 7635-7635/me.spencernusbaum.crashexample D/AndroidRuntime﹕ Shutting down VM
04-26 12:38:32.371 7635-7635/me.spencernusbaum.crashexample E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: me.spencernusbaum.crashexample, PID: 7635
java.lang.NullPointerException: Attempt to invoke virtual method 'void me.spencernusbaum.crashexample.fragment_view.setView(android.support.v4.app.Fragment)' on a null object reference
at me.spencernusbaum.crashexample.fragment_menu$1.onTouch(fragment_menu.java:32)
at android.view.View.dispatchTouchEvent(View.java:8382)
at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2430)
at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2119)
at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2430)
at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2119)
at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2430)
at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2119)
at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2430)
at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2119)
at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2430)
at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2119)
at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2430)
at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2119)
at com.android.internal.policy.impl.PhoneWindow$DecorView.superDispatchTouchEvent(PhoneWindow.java:2314)
at com.android.internal.policy.impl.PhoneWindow.superDispatchTouchEvent(PhoneWindow.java:1692)
at android.app.Activity.dispatchTouchEvent(Activity.java:2739)
at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchTouchEvent(PhoneWindow.java:2275)
at android.view.View.dispatchPointerEvent(View.java:8578)
at android.view.ViewRootImpl$ViewPostImeInputStage.processPointerEvent(ViewRootImpl.java:4021)
at android.view.ViewRootImpl$ViewPostImeInputStage.onProcess(ViewRootImpl.java:3887)
at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:3449)
at android.view.ViewRootImpl$InputStage.onDeliverToNext(ViewRootImpl.java:3502)
at android.view.ViewRootImpl$InputStage.forward(ViewRootImpl.java:3468)
at android.view.ViewRootImpl$AsyncInputStage.forward(ViewRootImpl.java:3578)
at android.view.ViewRootImpl$InputStage.apply(ViewRootImpl.java:3476)
at android.view.ViewRootImpl$AsyncInputStage.apply(ViewRootImpl.java:3635)
at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:3449)
at android.view.ViewRootImpl$InputStage.onDeliverToNext(ViewRootImpl.java:3502)
at android.view.ViewRootImpl$InputStage.forward(ViewRootImpl.java:3468)
at android.view.ViewRootImpl$InputStage.apply(ViewRootImpl.java:3476)
at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:3449)
at android.view.ViewRootImpl.deliverInputEvent(ViewRootImpl.java:5701)
at android.view.ViewRootImpl.doProcessInputEvents(ViewRootImpl.java:5675)
at android.view.ViewRootImpl.enqueueInputEvent(ViewRootImpl.java:5646)
at android.view.ViewRootImpl$WindowInputEventReceiver.onInputEvent(ViewRootImpl.java:5791)
at android.view.InputEventReceiver.dispatchInputEvent(InputEventReceiver.java:185)
at android.os.MessageQueue.nativePollOnce(Native Method)
at android.os.MessageQueue.next(MessageQueue.java:143)
at android.os.Looper.loop(Looper.java:122)
at android.app.ActivityThread.main(ActivityThread.java:5221)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)
When the display is rotated, android (by default) destroys the current activity, including all fragments (the current state of attached fragments and the activity is saved via onSaveInstanceState()).
After the rotate, the activity and attached fragments are re-created (again: by default) and previously saved state is restored (passed as Bundle to onCreate()).
For fragments the android runtime invokes their default, no-arguments constructor.
The newInstance() method of your fragment is never invoked for the re-created fragment, hence your fields are not properly initialized and thus null.

Error java.lang.runtimeexception view is not a sliding drawer

Hi I have been creating an app that implements a cutom sliding navigation drawer. However, I keep on receiving an error shown on the title.
Logs:
10-30 15:38:20.510: E/AndroidRuntime(1863): FATAL EXCEPTION: main
10-30 15:38:20.510: E/AndroidRuntime(1863): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.mobextph.krispykreme/com.mobextph.krispykreme.ParentActivity}: java.lang.IllegalArgumentException: View android.widget.LinearLayout{5321e6b8 V.E..... ......ID 0,0-0,0 #7f090002 app:id/left_drawer} is not a sliding drawer
10-30 15:38:20.510: E/AndroidRuntime(1863): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180)
10-30 15:38:20.510: E/AndroidRuntime(1863): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
10-30 15:38:20.510: E/AndroidRuntime(1863): at android.app.ActivityThread.access$600(ActivityThread.java:141)
10-30 15:38:20.510: E/AndroidRuntime(1863): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
10-30 15:38:20.510: E/AndroidRuntime(1863): at android.os.Handler.dispatchMessage(Handler.java:99)
10-30 15:38:20.510: E/AndroidRuntime(1863): at android.os.Looper.loop(Looper.java:137)
10-30 15:38:20.510: E/AndroidRuntime(1863): at android.app.ActivityThread.main(ActivityThread.java:5041)
10-30 15:38:20.510: E/AndroidRuntime(1863): at java.lang.reflect.Method.invokeNative(Native Method)
10-30 15:38:20.510: E/AndroidRuntime(1863): at java.lang.reflect.Method.invoke(Method.java:511)
10-30 15:38:20.510: E/AndroidRuntime(1863): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
10-30 15:38:20.510: E/AndroidRuntime(1863): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
10-30 15:38:20.510: E/AndroidRuntime(1863): at dalvik.system.NativeStart.main(Native Method)
10-30 15:38:20.510: E/AndroidRuntime(1863): Caused by: java.lang.IllegalArgumentException: View android.widget.LinearLayout{5321e6b8 V.E..... ......ID 0,0-0,0 #7f090002 app:id/left_drawer} is not a sliding drawer
10-30 15:38:20.510: E/AndroidRuntime(1863): at android.support.v4.widget.DrawerLayout.closeDrawer(DrawerLayout.java:1170)
10-30 15:38:20.510: E/AndroidRuntime(1863): at com.mobextph.krispykreme.ParentActivity.changeFragment(ParentActivity.java:141)
10-30 15:38:20.510: E/AndroidRuntime(1863): at com.mobextph.krispykreme.ParentActivity.onCreate(ParentActivity.java:111)
10-30 15:38:20.510: E/AndroidRuntime(1863): at android.app.Activity.performCreate(Activity.java:5104)
10-30 15:38:20.510: E/AndroidRuntime(1863): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
10-30 15:38:20.510: E/AndroidRuntime(1863): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144)
The Main Activity class is:
package com.mobextph.krispykreme;
import java.util.ArrayList;
import com.mobextph.krispykreme.constants.Constants;
import android.R.color;
import android.content.res.Configuration;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.support.v4.app.ActionBarDrawerToggle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v4.view.ViewPager;
import android.support.v4.widget.DrawerLayout;
import android.view.Gravity;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.LinearLayout;
import android.widget.ListView;
public class ParentActivity extends FragmentActivity {
private Fragment changedFragment= null;
private FragmentManager fm;
private DrawerLayout mDrawerLayout;
private LinearLayout linearLayout;
private ListView mDrawerList;
private ActionBarDrawerToggle mDrawerToggle;
// nav drawer title
private CharSequence mDrawerTitle;
// used to store app title
private CharSequence mTitle;
// slide menu items
private String[] navMenuTitles;
private ArrayList<String> navDrawerItems;
private ArrayAdapter<String> adapter;
private String[] menuItem= {"ORDER", "STAMP CARD", "REWARDS", "STORES", "FAVORITES"};
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Drawable d;
// getActionBar().setBackgroundDrawable(new ColorDrawable(getResources().getColor(android.R.color.transparent)));
/*Slider*/
mTitle = mDrawerTitle = getTitle();
navMenuTitles= menuItem;
mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
mDrawerList = (ListView) findViewById(R.id.list_slidermenu);
linearLayout = (LinearLayout) findViewById(R.id.left_drawer);
mDrawerList.setAdapter(new ArrayAdapter<String>(this, R.layout.drawer_list_item, R.id.title, menuItem));
mDrawerList.setOnItemClickListener(new SlideMenuClickListener());
getActionBar().setDisplayHomeAsUpEnabled(true);
getActionBar().setHomeButtonEnabled(true);
mDrawerToggle = new ActionBarDrawerToggle(this, mDrawerLayout,
R.drawable.button_menu, //nav menu toggle icon
R.string.app_name, // nav drawer open - description for accessibility
R.string.app_name // nav drawer close - description for accessibility
) {
public void onDrawerClosed(View view) {
getActionBar().setTitle(mTitle);
// calling onPrepareOptionsMenu() to show action bar icons
invalidateOptionsMenu();
}
public void onDrawerOpened(View drawerView) {
getActionBar().setTitle(mDrawerTitle);
// calling onPrepareOptionsMenu() to hide action bar icons
invalidateOptionsMenu();
}
};
mDrawerLayout.setDrawerListener(mDrawerToggle);
if (savedInstanceState == null) {
// on first time display view for first nav item
// changeFragment(-1);
}
fm = getSupportFragmentManager();
// DatabaseHandler db= new DatabaseHandler(this);
//
// for(int i = 0; i < 2; i++){
// db.insertFavorite(Constants.id[i], Constants.imageItem[i],
// Constants.title[i], Constants.price[i], Constants.desc[i],
// Constants.status[i]);
// }
changeFragment(-1);
}
public void changeFragment(int fragment){
FragmentTransaction transaction = fm.beginTransaction();
changedFragment = null;
switch(fragment){
case -1:
changedFragment = new HomeFragment();
// mDrawerLayout.closeDrawers();
break;
case Constants.ORDER_SCREEN_CLICKED:
changedFragment = new OrderFragment();
break;
case Constants.FAVORITES_SCREEN_CLICKED:
changedFragment = new FavoritesFragment();
break;
}
if (changedFragment != null) {
transaction.replace(R.id.fragment_holder, changedFragment);
transaction
.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE);
transaction.addToBackStack(null);
transaction.commit();
mDrawerList.setItemChecked(fragment, true);
mDrawerList.setSelection(fragment);
mDrawerLayout.closeDrawer(linearLayout);
}
}
private class SlideMenuClickListener implements
ListView.OnItemClickListener {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position,
long id) {
// display view for selected nav drawer item
int fragment = 0;
if(position == 0)
fragment= Constants.ORDER_SCREEN_CLICKED;
else if(position == 4)
fragment= Constants.FAVORITES_SCREEN_CLICKED;
changeFragment(fragment);
}
}
// #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 boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
// int id = item.getItemId();
// if (id == R.id.action_settings) {
// return true;
// }
// return super.onOptionsItemSelected(item);
if(mDrawerToggle.onOptionsItemSelected(item))
return true;
switch(item.getItemId()){
default: return super.onOptionsItemSelected(item);
}
}
/**
* When using the ActionBarDrawerToggle, you must call it during
* onPostCreate() and onConfigurationChanged()...
*/
#Override
protected void onPostCreate(Bundle savedInstanceState) {
super.onPostCreate(savedInstanceState);
// Sync the toggle state after onRestoreInstanceState has occurred.
mDrawerToggle.syncState();
}
#Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
// Pass any configuration change to the drawer toggls
mDrawerToggle.onConfigurationChanged(newConfig);
}
}
The view code that shows a linear layout for the sliding drawer and a listview is:
<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<FrameLayout
android:id="#+id/fragment_holder"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<LinearLayout
android:id="#+id/left_drawer"
android:layout_width="260dp"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="#008265"
android:gravity= "left" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<ImageView
android:layout_width="0dip"
android:layout_height="wrap_content"
android:layout_weight="1"
android:src="#drawable/icon_login"/>
<TextView
android:layout_width="0dip"
android:layout_height="wrap_content"
android:layout_weight="1"
android:layout_gravity="center"
android:text="LOGIN"
android:textSize="15dp"
android:paddingLeft="5dp"
android:paddingRight="0dp"/>
<TextView
android:layout_width="0dip"
android:layout_height="wrap_content"
android:layout_weight="1"
android:layout_gravity="center"
android:text="|"
android:gravity="center"
android:textSize="15dp"/>
<TextView
android:layout_width="0dip"
android:layout_height="wrap_content"
android:layout_weight="1"
android:layout_gravity="center"
android:text="SIGN UP"
android:textSize="15dp"
android:paddingLeft="0dp"
android:paddingRight="5dp"/>
</LinearLayout>
<ListView
android:id="#+id/list_slidermenu"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="start"
android:choiceMode="singleChoice"
android:divider="#color/list_divider"
android:dividerHeight="1dp"
android:listSelector="#drawable/list_selector"
android:gravity= "left" />
</LinearLayout>
</android.support.v4.widget.DrawerLayout>
Try setting
layout_gravity="start" or layout_gravity="left"
in your left_drawer instead of gravity

Android app, dynamic activity window with buttons

I am stuck with an idea.
I am trying to implement a feature in a android application.
I have an edittext and a button, if I enter 3 in edittext
and press the button I want to go to another activity which will be having 3 buttons.
If i enter 10 I wanna go to another activity where to display 10 buttons.
I know how to make such switch, I don't know how to do it dynamically ,
I want to make for values between 0 and 10 and I don't want to have 10 fragments .
Plaese give me solution to achieve it ..
Thanks..
Untill now:
mainactivity.java:
package com.example.instances_temperature;
import com.example.instances_temperature.Tabel;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v7.app.ActionBarActivity;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
public class MainActivity extends ActionBarActivity {
EditText instances;
Button ok;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ok = (Button) findViewById(R.id.ok);
instances = (EditText) findViewById(R.id.instances);
ok.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
if (instances.getText().toString().length() > 0)
{
int value = Integer.parseInt(instances.getText().toString());;
if(value > 10)
Toast.makeText(getApplicationContext(), "Enter a value between 0 and 10!", Toast.LENGTH_SHORT).show();
else
if(value<0)
Toast.makeText(getApplicationContext(), "Enter a value between 0 and 10!", Toast.LENGTH_SHORT).show();
else
if(value >= 0 && value <=10)
{
schimba(v);
}
}
else
{
Toast.makeText(getApplicationContext(), "Enter a value between 0 and 10!", Toast.LENGTH_SHORT).show();
}
}
});
if (savedInstanceState == null) {
getSupportFragmentManager().beginTransaction()
.add(R.id.container, new PlaceholderFragment()).commit();
}
}
#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 boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
/**
* A placeholder fragment containing a simple view.
*/
public static class PlaceholderFragment extends Fragment {
public PlaceholderFragment() {
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_main, container,
false);
return rootView;
}
}
public void schimba(View view){
int value = Integer.parseInt(instances.getText().toString());;
Intent intent = new Intent(this, Tabel.class);
intent.putExtra("max", value);
startActivity(intent);
}
}
activity_main.xml:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.instances_temperature.MainActivity"
tools:ignore="MergeRootFrame" >
<TextView
android:id="#+id/ShowValue"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="139dp"
android:text="Instances No.:"
android:textAppearance="?android:attr/textAppearanceLarge" />
<EditText
android:id="#+id/instances"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:ems="10"
android:inputType="number" >
<requestFocus />
</EditText>
<Button
android:id="#+id/ok"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/instances"
android:layout_centerHorizontal="true"
android:layout_marginTop="48dp"
android:text="Ok"
android:onClick="schimba" />
</RelativeLayout>
tabel.java:
package com.example.instances_temperature;
import android.support.v7.app.ActionBarActivity;
import android.support.v7.app.ActionBar;
import android.support.v4.app.Fragment;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.os.Build;
public class Tabel extends ActionBarActivity {
int i;
int value;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_tabel);
Intent intentObject = getIntent();
value = intentObject.getIntExtra("max", 0);
LinearLayout layout = (LinearLayout)findViewById(R.id.container);
for(i=1;i<=value;i++)
{
LayoutInflater layoutinflate = null;
layoutinflate = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
final View rowview = layoutinflate.inflate( R.layout.ShowMe, null);
TextView showvalue;
showvalue = (TextView) rowview.findViewById(R.id.showid);
showvalue.setText(""+value);
layout.addView(rowview);
}
//showvalue.setText(String.valueOf(getIntent().getExtras().getInt("max")));
if (savedInstanceState == null) {
getSupportFragmentManager().beginTransaction()
.add(R.id.container, new PlaceholderFragment()).commit();
}
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.tabel, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
/**
* A placeholder fragment containing a simple view.
*/
public static class PlaceholderFragment extends Fragment {
public PlaceholderFragment() {
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_tabel,
container, false);
return rootView;
}
}
}
activity_tabel.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="com.example.instances_temperature.Tabel"
tools:ignore="MergeRootFrame" >
</LinearLayout>
LinearLayout code of inflation_layout.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/inflationn"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView
android:id="#+id/ShowMe"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Large Text"
android:textAppearance="?android:attr/textAppearanceLarge" />
</LinearLayout>
LogCat error:
04-24 06:21:04.244: E/AndroidRuntime(269): FATAL EXCEPTION: main
04-24 06:21:04.244: E/AndroidRuntime(269): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.instances_temperature/com.example.instances_temperature.Tabel}: java.lang.NullPointerException
04-24 06:21:04.244: E/AndroidRuntime(269): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663)
04-24 06:21:04.244: E/AndroidRuntime(269): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
04-24 06:21:04.244: E/AndroidRuntime(269): at android.app.ActivityThread.access$2300(ActivityThread.java:125)
04-24 06:21:04.244: E/AndroidRuntime(269): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
04-24 06:21:04.244: E/AndroidRuntime(269): at android.os.Handler.dispatchMessage(Handler.java:99)
04-24 06:21:04.244: E/AndroidRuntime(269): at android.os.Looper.loop(Looper.java:123)
04-24 06:21:04.244: E/AndroidRuntime(269): at android.app.ActivityThread.main(ActivityThread.java:4627)
04-24 06:21:04.244: E/AndroidRuntime(269): at java.lang.reflect.Method.invokeNative(Native Method)
04-24 06:21:04.244: E/AndroidRuntime(269): at java.lang.reflect.Method.invoke(Method.java:521)
04-24 06:21:04.244: E/AndroidRuntime(269): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
04-24 06:21:04.244: E/AndroidRuntime(269): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
04-24 06:21:04.244: E/AndroidRuntime(269): at dalvik.system.NativeStart.main(Native Method)
04-24 06:21:04.244: E/AndroidRuntime(269): Caused by: java.lang.NullPointerException
04-24 06:21:04.244: E/AndroidRuntime(269): at com.example.instances_temperature.Tabel.onCreate(Tabel.java:42)
04-24 06:21:04.244: E/AndroidRuntime(269): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
04-24 06:21:04.244: E/AndroidRuntime(269): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)
04-24 06:21:04.244: E/AndroidRuntime(269): ... 11 more
You can get the value from the editText by...
String value= EditText.getText().toString();
Pass the value to another activity (Activity2) through Intent
Now in Activity2 you write this code in the onCreate()
for(int i=1 ; i<= value ; i++){
Button b = new Button(getApplicationContext());
LinearLayout.addView(b);
}
Answer to your edited question.....
main xml file activity_table:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/layout"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
</LinearLayout>
layout file to be inflated layout_inflate:
<?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="fill_parent"
android:orientation="vertical" >
<TextView
android:id="#+id/ShowValue"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Text" />
</LinearLayout>
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_tabel);
Intent intentObject = getIntent();
value = intentObject.getIntExtra("max", 0);
LinearLayout layout= (LinearLayout)findViewById(R.id.layout)
for(i=0;i<=value;i++)
{
LayoutInflater layoutinflate = null;
layoutinflate = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View rowview = layoutinflate.inflate( R.layout.layout_inflate, null);
TextView showvalue;
showvalue = (TextView) rowview.findViewById(R.id.ShowValue);
showvalue.setText(""+value);
layout.addView(rowview);
}
You can retrieve the EditText value on tha Button click. And pass the Value with the intent.
In that Activity you can build your logic Using Swicth cases. Then you can add the number of Buttons dynamically (eg:- for(i=0;i<yourValue;i++) yourValue is got From intent.)

Categories

Resources